1
1
<?php
2
2
/**
3
- * This file has implementation for csrfpDefaultLogger class
3
+ * This file has implementation for csrfpDefaultLogger class.
4
4
*/
5
5
include __DIR__ ."/LoggerInterface.php " ;
6
6
7
- if (!defined ('__CSRF_PROTECTOR_csrfpDefaultLogger_ ' )) {
7
+ if (!defined ('__CSRF_PROTECTOR_DEFAULT_LOGGER_ ' )) {
8
8
// to avoid multiple declaration errors
9
- define ('__CSRF_PROTECTOR_csrfpDefaultLogger_ ' , true );
9
+ define ('__CSRF_PROTECTOR_DEFAULT_LOGGER_ ' , true );
10
10
11
11
class logDirectoryNotFoundException extends \exception {};
12
12
class logFileWriteError extends \exception {};
13
13
14
14
/**
15
- * Default logger class for CSRF Protector
16
- * This is a file based logger class
15
+ * Default logger class for CSRF Protector.
16
+ *
17
+ * This is a file based logger class.
17
18
*/
18
19
class csrfpDefaultLogger implements LoggerInterface {
20
+
19
21
/**
20
22
* Variable: $logDirectory
21
23
* directory for file based logging
@@ -35,12 +37,11 @@ class csrfpDefaultLogger implements LoggerInterface {
35
37
* logDirectoryNotFoundException - if log directory is not found
36
38
*/
37
39
function __construct ($ path ) {
38
- //// Check for relative path
40
+ // Check for relative path
39
41
$ this ->logDirectory = __DIR__ . "/../ " . $ path ;
40
42
41
43
42
- //// If the relative log directory path does not
43
- //// exist try as an absolute path
44
+ // If the relative log directory path does not exist try as an absolute path.
44
45
if (!is_dir ($ this ->logDirectory )) {
45
46
$ this ->logDirectory = $ path ;
46
47
}
@@ -67,21 +68,21 @@ public function log($message, $context = array()) {
67
68
// Append to the log file, or create it if it does not exist create
68
69
$ logFile = fopen ($ this ->logDirectory ."/ " . date ("m-20y " ) . ".log " , "a+ " );
69
70
70
- //throw exception if above fopen fails
71
+ // Throw exception if above fopen fails
71
72
if (!$ logFile ) {
72
73
throw new logFileWriteError ("OWASP CSRFProtector: Unable to write to the log file " );
73
74
}
74
75
75
76
$ context ['timestamp ' ] = time ();
76
77
$ context ['message ' ] = $ message ;
77
78
78
- //convert log array to JSON format to be logged
79
+ // Convert log array to JSON format to be logged
79
80
$ context = json_encode ($ context ) .PHP_EOL ;
80
81
81
- //append log to the file
82
+ // Append log to the file
82
83
fwrite ($ logFile , $ context );
83
84
84
- //close the file handler
85
+ // Close the file handler
85
86
fclose ($ logFile );
86
87
}
87
88
}
0 commit comments