[PHP]error_log wel in zend studio, niet in apache.

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik ben bezig geweest met het schtijven van mijn eigen error class. De class werkt prima in zend studio als ik ga debuggen.
Maar als ik de pagina op (een vers geinstalleerde) webserver gooi word er niets in de log file gegooit.

hier mijn code:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php 
/*
* Description:
* Class to handle php and custom errors.
*
* Configuration:
* In Constructor
*
* Usage:
* Create a error object.  At creation, the handler will be activated 
* $eh = new Error(); 
* Any php error will be handled by the object.
*
* throw up a error :
* trigger_error( 'error msg' , E_USER_NOTICE );
*
*/
 
class Error { 

    var $logFilename;   // Private String name (or path) to logfile
    var $mailAddress;   // Private String Email address of webmaster
    var $errorToFile;   // Private boolean flag
    var $warningToFile; // Private boolean flag
    var $noticeToFile;  // Private boolean flag
    var $errorToMail;   // Private boolean flag
    var $warningToMail; // Private boolean flag
    var $noticeToMail;  // Private boolean flag
    
    /* 
    * description: 
    *
    * Visibility: Constructor
    *
    * Args: 
    * none
    *
    * Return: 
    * boolean
    */
    function Error(){
        $this->setMailAddress( 'recipients@email.com' );        
        $this->setFilename( 'Error_log.log' );      
        $this->setFlags( true, true, true, true, true, true ); //Log errors to file?, Log warnings to file?,
 Log notices to file?, Send errors via mail?, Send warnings via mail?, Send notices via mail?
        $this->setHandler();
        return true;
    }   
    
    /*
    * description: actual error handler called by the engine
    *   
    * Visibility: Public
    *
    * Args: 
    * String error_type
    * String error_msg
    * String error_file
    * int error_line
    * String error_context
    *
    * Return: 
    * boolean
    */  
    function errorHandler($error_type, $error_msg, $error_file, $error_line, $error_context){       
        switch( $error_type ){
            case E_ERROR:
            case E_USER_ERROR: 
            if( $this->errorToFile ){
                if( $this->logFilename == '' ){
                    error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file .
 ' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 0); 
                } 
                else{error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file . 
' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 3, $this->logFilename);
                }
            }
            if( $this->errorToMail ){ 
                $this->mailError($error_msg . ' (error type ' . $error_type . ' in ' . $error_file .
 ' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10)); 
            } 
            echo $error_msg,' (error type ',$error_type,' in ',$error_file,'
 on line ',$error_line,') [context: ',$error_context,']<br />'; 
            exit; 
            break; 
            case E_WARNING: 
            case E_USER_WARNING: 
            if( $this->warningToFile ){ 
                if( $this->logFilename == '' ){ 
                    error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file . 
' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 0); 
                }
                else  { 
                    error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file . 
' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 3, $this->logFilename); 
                } 
            } 
            if( $this->warningToMail ){
                $this->mailError($error_msg . ' (error type ' . $error_type . ' in ' . $error_file .
 ' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10)); 
            } 
            echo $error_msg,' (error type ',$error_type,' in ',$error_file,'
 on line ',$error_line,') [context: ',$error_context,']<br />'; 
            break; 
            case E_NOTICE: 
            case E_USER_NOTICE: 
            if( $this->noticeToFile ){ 
                if( $this->logFilename == '' ){
                    error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file .
 ' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 0);
                }
                else{
                    error_log( $error_msg . ' (error type ' . $error_type . ' in ' . $error_file . 
' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10), 3, $this->logFilename);
                }
            }
            if( $this->noticeToMail ){ 
                $this->mailError($error_msg . ' (error type ' . $error_type . ' in ' . $error_file . 
' on line ' . $error_line . ') [context: ' . $error_context . ']' . chr(10)); 
            } 
            echo $error_msg,' (error type ',$error_type,' in ',$error_file,'
 on line ',$error_line,') [context: ',$error_context,']<br />'; 
            break; 
        } 
        return true; 
    }   
    
    /* 
    * description: set the error handler for the php engine
    *
    * Visibility: Public
    *
    * Args: 
    * none
    *
    * Return: 
    * boolean
    */
    function setHandler(){ 
    set_error_handler( array( &$this, 'errorHandler' ) ); 
    return true;
    }
    
    /* 
    * description: restore the default error handler for the php engine
    *
    * Visibility: Public
    *
    * Args: 
    * none
    *
    * Return: 
    * boolean
    */  
    function restoreHandler(){
        restore_error_handler(); 
        return true;
    }
    
    /* 
    * description: set the various flags in order:
    * Log errors to file? 
    * Log warnings to file? 
    * Log notices to file? 
    * Send errors via mail? 
    * Send warnings via mail? 
    * Send notices via mail? 
    *
    * Visibility: Public
    *
    * Args: 
    * boolean errorToFile
    * boolean warningToFile
    * boolean noticeToFile
    * boolean errorToMail
    * boolean warningToMail
    * boolean noticeToMail
    *
    * Return: 
    * boolean
    */  
    function setFlags( $errorToFile, $warningToFile, $noticeToFile, $errorToMail, $warningToMail, $noticeToMail){
        $this->errorToFile = $errorToFile;          //Log errors to file? 
        $this->warningToFile = $warningToFile;      //Log warnings to file? 
        $this->noticeToFile = $noticeToFile;        //Log notices to file? 
        $this->errorToMail = $errorToMail;          //Send errors via mail? 
        $this->warningToMail = $warningToMail;      //Send warnings via mail? 
        $this->noticeToMail = $noticeToMail;        //Send notices via mail? 
        return true;
    }
    
    /* 
    * description: Set the email address of the webmaster (used to send errors to)
    *
    * Visibility: Public
    *
    * Args: 
    * String address    (Email adress)
    *
    * Return: 
    * boolean
    */  
    function setMailAddress( $address ){
        $this->mailAddress = $address;
        return true;
    }
    
    /* 
    * description: Set the filename of the logfile (complete path)
    *
    * Visibility: Public
    *
    * Args: 
    * String filename   (path)
    *
    * Return: 
    * boolean
    */  
    function setFilename( $filename ){
        $this->logFilename = $filename; 
        return true;        
    }
    
    /* 
    * description: prints backtrace of potential error
    *
    * Visibility: Public
    *
    * Args: 
    * none
    *
    * Return: 
    * boolean
    */  
    function showBacktrace(){
        debug_backtrace();
        return true;
    }       
    
    /* 
    * description: Send email to webmaster
    *
    * Visibility: Private
    *
    * Args: 
    * none
    *
    * Return: 
    * boolean
    */
    function mailError( $mail_body ){
        $headers ='From: clsErrorHandler' . "\r\n"; 
        $headers .= 'MIME-Version: 1.0' . "\r\n"; 
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n"; 
        $subject = 'Error Report'; 
        $body = $mail_body; 
        mail( $this->mailAddress, $subject, $body, $headers );
        return true;        
    } 
} 
?>


en de code om een error te veroorzaken:
PHP:
1
2
3
4
5
<?php 
include 'Error.class.php'; 
$e = &new Error; 
trigger_error( 'error msg' , E_USER_NOTICE ); 
?> 


en natuurlijk de output :
Notice: error msg in c:\apache\htdocs\test.php on line 4
ik ben al een tijdje aan t googlen maar het enige waar ik op uit kom zijn codes precies zoals ik het doe. Ook zijn er naar mijn weten geen entry's in php.ini die dit zouden kunnen restricten....

Ook php.net geeft verder wienig info.

oftewel... I'm lost :'(

[ Voor 8% gewijzigd door Verwijderd op 13-07-2004 20:47 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Heel vermoeiend maar php triggered alleen een error binnen zijn eigen include als je een object als error handler gebruikt (zover ik weet, kan fout zitten)...

dus die

$this->setHandler();

in je constructor werkt niet

probeer eens
set_error_handler( array( $e, 'errorHandler' ) );
na de
$e = &new Error();

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Helaas, dit maakt geen verschil.. :'(

ook andere scripts die ik vond met google gebruiken deze methode :S

/edit (heb het dus wel ff getest :) )

[ Voor 19% gewijzigd door Verwijderd op 13-07-2004 21:17 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Zoek op je hd naar verschillende php.ini's.
Waarschijnlijk maakt zend studio gebruik van een andere instantie van php.

Acties:
  • 0 Henk 'm!

  • Dutchmega
  • Registratie: September 2001
  • Niet online
Zend Studio maakt Idd gebruik van een aparte PHP. Zoek je php.ini in je Windows map eens en zoek naar logging errors :)

Acties:
  • 0 Henk 'm!

  • Apache
  • Registratie: Juli 2000
  • Laatst online: 16-09 10:29

Apache

amateur software devver

PHP error handler is globaal, niet per include.

Waarschijnlijk ligt het aan het feit dat in de ene php.ini de error reporting level hoger staat dan in de andere en een notice daarom niet werkt.

Doet hij het wel als je een trigger_error('bleh', E_USER_ERROR); doet?

dit kan je trouwens wijzigen door de error reporting leven in je script te verzetten met error_reporting($lvl) (ff bovenaan zetten met E_ALL om te zien of je notice dan wel word gelogged)

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    /**
    * @return void
    * @param array $hiddenfiles
    * @param string $tDir
    * @param integer $errorlvl
    * @desc sets a new errorhandler
    */
    public function __construct($hiddenfiles = array(), $tDir = './t/', $errorlvl = E_ALL){

        error_reporting($errorlvl);
        set_error_handler(array($this, '_errorHandler'));

        $this->secureFiles[]        = $hiddenfiles;
        $this->templateDirectory    = $tDir;
    
    }


Zo doe ik het trouwens in mijn error handler, ksnap trouwens niet waarom je het meteen toekent aan $e en vooral niet de reference naar je class?

een new ErrorHandler; doet het bij mij al.

Trouwens alle functies om verschillende error reporting's te doen in je classe is ook niet zo uitermate efficient.

Ik heb hier er een interface voor waarna je classes kunt schrijven die error reporting doen.

bvb
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
interface errorNotificationDriver {
    
    public function raiseError($errno, $errstr, $errfile, $errline, $vars);
    
}

class errMailNotification implements errorNotificationDriver {

    private $notifyEmail        = 'admin@localhost';
    
    public function __construct($email){

        $this->notifyEmail = $email;
    
    }

    /**
    * @return void
    * @param integer $errno
    * @param string $errstr
    * @param string $errfile
    * @param integer $errline
    * @param mixed $vars
    * @desc emails the errors to the admin in an ascii layout
    */
    public function raiseError($errno, $errstr, $errfile, $errline, $vars){
    
        error_log("ERROR REPORT. @ ".strftime('%d/%m/%y', time())."<br />
                Error: ".$errstr." <br />
                Type: ".ErrorHandler::getErrorByCode($errno)." <br />
                File: ".$errfile." <br />
                Line: ".$errline, 1, $this->notifyEmail);
    
    }

}


En in m'n main error class:
PHP:
1
2
3
4
5
    static public function registerNotificationDriver(errorNotificationDriver $end){
    
        ErrorHandler::$notificationList[] = $end;
    
    }


Waarna als er een error getriggerd word ik de notificationList afloop en alle geregistreerde classen kunnen verwittigen.

Das ook handiger als je met 2 aan een project werkt en naar 2 locaties een email wilt versturen.

File/sql logging werkt hetzelfde, er is zelfs een socket notification class die rechtstreeks errors naar een mini servertje met gui stuurt zodat je een msn style popup krijgt.

[ Voor 6% gewijzigd door Apache op 13-07-2004 23:47 ]

If it ain't broken it doesn't have enough features

Pagina: 1