Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| KLogger | |
0.00% |
0 / 59 |
|
0.00% |
0 / 10 |
1056 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
72 | |||
| __destruct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| LogInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| LogDebug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| LogWarn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| LogError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| LogFatal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| Log | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| WriteFreeFormLine | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
| getTimeLine | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | /* Finally, A light, permissions-checking logging class. |
| 4 | * |
| 5 | * Author : Kenneth Katzgrau < katzgrau@gmail.com > |
| 6 | * Date : July 26, 2008 |
| 7 | * Comments : Originally written for use with wpSearch |
| 8 | * Website : http://codefury.net |
| 9 | * Version : 1.0 |
| 10 | * |
| 11 | * Usage: |
| 12 | * $log = new KLogger ( "log.txt" , KLogger::INFO ); |
| 13 | * $log->LogInfo("Returned a million search results"); //Prints to the log file |
| 14 | * $log->LogFATAL("Oh dear."); //Prints to the log file |
| 15 | * $log->LogDebug("x = 5"); //Prints nothing due to priority setting |
| 16 | */ |
| 17 | |
| 18 | class KLogger |
| 19 | { |
| 20 | |
| 21 | const DEBUG = 1; // Most Verbose |
| 22 | const INFO = 2; // ... |
| 23 | const WARN = 3; // ... |
| 24 | const ERROR = 4; // ... |
| 25 | const FATAL = 5; // Least Verbose |
| 26 | const OFF = 6; // Nothing at all. |
| 27 | |
| 28 | const LOG_OPEN = 1; |
| 29 | const OPEN_FAILED = 2; |
| 30 | const LOG_CLOSED = 3; |
| 31 | |
| 32 | /* Public members: Not so much of an example of encapsulation, but that's okay. */ |
| 33 | public $Log_Status = KLogger::LOG_CLOSED; |
| 34 | public $DateFormat = "Y-m-d G:i:s"; |
| 35 | public $MessageQueue; |
| 36 | |
| 37 | private $log_file; |
| 38 | private $priority = KLogger::INFO; |
| 39 | |
| 40 | private $file_handle; |
| 41 | |
| 42 | public function __construct( $filepath , $priority ) |
| 43 | { |
| 44 | if ( $priority == KLogger::OFF ) return; |
| 45 | |
| 46 | $this->log_file = $filepath; |
| 47 | $this->MessageQueue = array(); |
| 48 | $this->priority = $priority; |
| 49 | |
| 50 | // Check if the directory exists, if not try to create it |
| 51 | $dir = dirname($filepath); |
| 52 | if (!file_exists($dir)) { |
| 53 | // Try to create the directory |
| 54 | if (!@mkdir($dir, 0777, true)) { |
| 55 | $this->Log_Status = KLogger::OPEN_FAILED; |
| 56 | $this->MessageQueue[] = "Could not create directory for log file. Using error_log as fallback."; |
| 57 | error_log("KLogger: Could not create directory for log file: $dir"); |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if ( file_exists( $this->log_file ) ) |
| 63 | { |
| 64 | if ( !is_writable($this->log_file) ) |
| 65 | { |
| 66 | $this->Log_Status = KLogger::OPEN_FAILED; |
| 67 | $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set."; |
| 68 | error_log("KLogger: The log file exists but is not writable: $filepath"); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | // Try to create the file |
| 75 | $handle = @fopen($this->log_file, 'w'); |
| 76 | if ($handle) { |
| 77 | fclose($handle); |
| 78 | } else { |
| 79 | $this->Log_Status = KLogger::OPEN_FAILED; |
| 80 | $this->MessageQueue[] = "Could not create log file. Using error_log as fallback."; |
| 81 | error_log("KLogger: Could not create log file: $filepath"); |
| 82 | return; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( $this->file_handle = fopen( $this->log_file , "a" ) ) |
| 87 | { |
| 88 | $this->Log_Status = KLogger::LOG_OPEN; |
| 89 | $this->MessageQueue[] = "The log file was opened successfully."; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | $this->Log_Status = KLogger::OPEN_FAILED; |
| 94 | $this->MessageQueue[] = "The file could not be opened. Check permissions. Using error_log as fallback."; |
| 95 | error_log("KLogger: Could not open log file for writing: $filepath"); |
| 96 | } |
| 97 | |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | public function __destruct() |
| 102 | { |
| 103 | if ( $this->file_handle ) |
| 104 | fclose( $this->file_handle ); |
| 105 | } |
| 106 | |
| 107 | public function LogInfo($line) |
| 108 | { |
| 109 | $this->Log( $line , KLogger::INFO ); |
| 110 | } |
| 111 | |
| 112 | public function LogDebug($line) |
| 113 | { |
| 114 | $this->Log( $line , KLogger::DEBUG ); |
| 115 | } |
| 116 | |
| 117 | public function LogWarn($line) |
| 118 | { |
| 119 | $this->Log( $line , KLogger::WARN ); |
| 120 | } |
| 121 | |
| 122 | public function LogError($line) |
| 123 | { |
| 124 | $this->Log( $line , KLogger::ERROR ); |
| 125 | } |
| 126 | |
| 127 | public function LogFatal($line) |
| 128 | { |
| 129 | $this->Log( $line , KLogger::FATAL ); |
| 130 | } |
| 131 | |
| 132 | public function Log($line, $priority) |
| 133 | { |
| 134 | if ( $this->priority <= $priority ) |
| 135 | { |
| 136 | $status = $this->getTimeLine( $priority ); |
| 137 | // Convert arrays to string representation |
| 138 | if (is_array($line)) { |
| 139 | $line = json_encode($line); |
| 140 | } elseif (is_object($line)) { |
| 141 | $line = json_encode($line); |
| 142 | } |
| 143 | $this->WriteFreeFormLine ( "$status $line \n" ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public function WriteFreeFormLine( $line ) |
| 148 | { |
| 149 | if ( $this->Log_Status == KLogger::LOG_OPEN && $this->priority != KLogger::OFF ) |
| 150 | { |
| 151 | if (fwrite( $this->file_handle , $line ) === false) { |
| 152 | $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set."; |
| 153 | error_log("KLogger: " . trim($line)); // Use error_log as fallback |
| 154 | } |
| 155 | } |
| 156 | else if ( $this->Log_Status == KLogger::OPEN_FAILED && $this->priority != KLogger::OFF ) |
| 157 | { |
| 158 | // Use error_log as fallback when file is not available |
| 159 | error_log("KLogger: " . trim($line)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private function getTimeLine( $level ) |
| 164 | { |
| 165 | $dateTime = new DateTime('now', new DateTimeZone('America/Chicago')); |
| 166 | $time = $dateTime->format($this->DateFormat ); |
| 167 | switch( $level ) |
| 168 | { |
| 169 | case KLogger::INFO: |
| 170 | return "$time - INFO -->"; |
| 171 | case KLogger::WARN: |
| 172 | return "$time - WARN -->"; |
| 173 | case KLogger::DEBUG: |
| 174 | return "$time - DEBUG -->"; |
| 175 | case KLogger::ERROR: |
| 176 | return "$time - ERROR -->"; |
| 177 | case KLogger::FATAL: |
| 178 | return "$time - FATAL -->"; |
| 179 | default: |
| 180 | return "$time - LOG -->"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
| 187 | ?> |