-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
105 lines (82 loc) · 3.28 KB
/
Logger.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
<?php
/**
* Created by JetBrains PhpStorm.
* User: Kathy
* Date: 21/08/14
*/
namespace Foundation;
use Tracy\Dumper;
use Tracy\Helpers;
class Logger {
const LOG_PATH = "./../log";
const DEBUG = 'debug',
INFO = 'info',
WARNING = 'warning',
ERROR = 'error',
EXCEPTION = 'exception',
CRITICAL = 'critical';
public static function log($filename, $value, $priority = self::INFO){
if ($priority == self::DEBUG && !(\Phalcon\DI::getDefault()->getConfigurator()->isDebug())){
return;
}
static::isDirectory();
//$exceptionFile = $value instanceof \Exception ? self::logException($value) : NULL;
// debug_print_backtrace();
//var_dump(debug_backtrace());
$message = self::formatMessage($value);
$file = self::LOG_PATH . '/'.$filename.'.log'; //. strtolower($priority ?: self::INFO) . '.log';
if (!@file_put_contents($file, $message . PHP_EOL . PHP_EOL . file_get_contents($file)/*, FILE_APPEND | LOCK_EX*/)) {
throw new \RuntimeException("Unable to write to log file '$file'. Is directory writable?");
}
/*if (in_array($priority, array(self::ERROR, self::EXCEPTION, self::CRITICAL), TRUE) && $this->email && $this->mailer
&& @filemtime(self::LOG_PATH . '/email-sent') + $this->emailSnooze < time() // @ - file may not exist
&& @file_put_contents(self::LOG_PATH . '/email-sent', 'sent') // @ - file may not be writable
) {
call_user_func($this->mailer, $message, implode(', ', (array) $this->email));
}*/
//return $exceptionFile;
}
public static function debug($filename,$value){
self::log($filename, $value,self::DEBUG);
}
public static function info($filename,$value){
self::log($filename, $value,self::INFO);
}
public static function warning($filename,$value){
self::log($filename, $value,self::WARNING);
}
public static function error($filename,$value){
self::log($filename, $value,self::ERROR);
}
public static function critical($filename,$value){
self::log($filename, $value,self::CRITICAL);
}
private static function isDirectory(){
if (!is_dir(self::LOG_PATH)) {
throw new \RuntimeException("Directory '".realpath(self::LOG_PATH)."' is not found or is not directory.");
}
}
/**
* @return string
*/
private static function formatMessage($value, $exceptionFile = NULL)
{
if ($value instanceof \Exception) {
while ($value) {
$tmp[] = ($value instanceof \ErrorException ? 'Fatal error: ' . $value->getMessage() : get_class($value) . ': ' . $value->getMessage())
. ' in ' . $value->getFile() . ':' . $value->getLine();
$value = $value->getPrevious();
}
$value = implode($tmp, "\ncaused by ");
} elseif (!is_string($value)) {
$value = Dumper::toText($value);
}
$value = trim(preg_replace('#\s*\r?\n\s*#', ' ', $value));
return implode(' ', array(
@date('[Y-m-d H-i-s]'),
$value,
' @ ' . Helpers::getSource(),
$exceptionFile ? ' @@ ' . basename($exceptionFile) : NULL
));
}
}