-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
140 lines (122 loc) · 3.01 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
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
<?php
/**
* Contains set of static logging methods
* Please, use ONLY following priorities:
* - LOG_INFO
* - LOG_WARNING
* - LOG_DEBUG
* - LOG_ERR
*
* @method static void debug(string $msg)
* @method static void info(string $msg)
* @method static void warning(string $msg)
* @method static void err(string $msg)
*/
class Logger {
/**
* @var null|string
*/
private static $uid = null;
public static $logIndex = LOG_LOCAL0;
public static $label = '';
public static $timer = null;
/**
* Log a message
* !WARNING! it log only first 1024 bytes of message
* @see http://php.net/syslog
* @param int $priority prioroty level
* @param string $message
*/
public static function log($priority, $message)
{
if(!self::$uid){ self::_init(); }
syslog($priority, self::$label.$message);
}
/**
* Write big message to local data logs folder, much slower than syslog
* @param $priority
* @param $message
* @param string $section
*/
public static function file($priority, $message, $section='common')
{
// compose path
$dirPath = LOGS_PATH.'/'.$section.'/';
$filePath = $dirPath.$section.'_'.date('Y_m_d').'.log';
// compose message
$client = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '(null)';
$priorityName = 'LOG_UNKNOWN';
switch($priority)
{
case LOG_DEBUG: $priorityName = 'LOG_DEBUG'; break;
case LOG_INFO: $priorityName = 'LOG_INFO'; break;
case LOG_WARNING: $priorityName = 'LOG_WARNING'; break;
case LOG_ERR: $priorityName = 'LOG_ERR'; break;
}
// add text block wrapper for multi line output
if(strpos($message,"\n")!==false)
{
$id = 'B'.date('Ymd_His').'_'.substr(microtime(),2,4);
$message = '<<<'.$id."\n".$message."\n".$id."\n";
}
$message = "[".date('Y-m-d H:i:s')."]\t$client\t$priorityName\t$message";
// write
if(!is_dir($dirPath))
{
umask(0);
if(!mkdir($dirPath, 0777, true))
{
Logger::log(LOG_ERR, "LOG\tCreate dir failed\t[$dirPath]");
error_log($message);
return;
}
}
error_log($message."\n", 3, $filePath);
}
/**
* @static
* @param $priority
* @param $message
* @return mixed
*/
public static function timer($priority, $message)
{
if(empty(self::$timer))
{
self::$timer = microtime(1);
self::log($priority, $message);
return;
}
self::log($priority, sprintf("%0.4f\t",microtime(1)-self::$timer).$message);
self::$timer = microtime(1);
}
/**
* Logging initialization
*/
private static function _init()
{
if(!self::$uid){ self::$uid = uniqid(); }
openlog(self::$uid, LOG_ODELAY, self::$logIndex);
if(!defined('LOGS_PATH'))
{
define('LOGS_PATH','/var/log/sapzxc-logger.log');
}
}
/**
* Shortcut for Logger::log(LOG_*, '')
* @static
* @param $name
* @param $arguments
*/
public static function __callStatic($name, $arguments)
{
$level = 'LOG_'.strtoupper($name);
if(defined($level))
{
$msg = isset($arguments[1])
? $arguments[1]."\t".$arguments[0]
: $arguments[0];
self::log(constant($level), $msg);
}
}
}