-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.php
121 lines (101 loc) · 3.42 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
<?php
namespace plough\log;
require_once(__DIR__."/Psr/Log/LoggerInterface.php");
require_once(__DIR__."/Psr/Log/LogLevel.php");
require_once(__DIR__."/Psr/Log/AbstractLogger.php");
require_once("utils.php");
// Global logger instance
$logger;
class Logger extends \Psr\Log\AbstractLogger
{
private $_date_str;
private $_log_to_stdout;
private $_log_dir;
private $_log_path;
private $_log_file;
public function __construct($log_to_stdout)
{
$this->_log_dir = \plough\get_plugin_root() . "/logs";
$this->_log_to_stdout = $log_to_stdout;
}
private function init($date_str)
{
if (!file_exists($this->_log_dir))
mkdir($this->_log_dir);
$this->_date_str = $date_str;
$this->_log_path = $this->_log_dir . "/plough-" . $this->_date_str . ".log";
$this->_log_file = fopen($this->_log_path, "a");
fwrite($this->_log_file, PHP_EOL . \plough\SEPARATOR_LINE . PHP_EOL);
fwrite($this->_log_file, "Initialised Plough plugin log at " . $this->_log_path . PHP_EOL . PHP_EOL);
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = array())
{
$current_datetime = new \DateTime("now", new \DateTimeZone("UTC"));
$current_date_str = $current_datetime->format("Y-m-d");
$current_datetime_str = $current_datetime->format("Y-m-d H:i:s.v");
$current_time_str = $current_datetime->format("H:i:s.v");
if ($this->_date_str != $current_date_str)
$this->init($current_date_str);
$log_line = strtoupper($level) . " " . $message . PHP_EOL;
$line_with_time = $current_time_str . " " . $log_line;
$line_with_datetime = $current_datetime_str . " " . $log_line;
if ($this->_log_to_stdout)
echo $line_with_time;
fwrite($this->_log_file, $line_with_datetime);
}
}
function init($log_to_stdout = false)
{
global $logger;
$logger = new Logger($log_to_stdout);
}
function emergency($message, array $context = array())
{
global $logger;
$logger->emergency($message, $context);
}
function alert($message, array $context = array())
{
global $logger;
$logger->alert($message, $context);
}
function critical($message, array $context = array())
{
global $logger;
$logger->critical($message, $context);
}
function error($message, array $context = array())
{
global $logger;
$logger->error($message, $context);
}
function warning($message, array $context = array())
{
global $logger;
$logger->warning($message, $context);
}
function notice($message, array $context = array())
{
global $logger;
$logger->notice($message, $context);
}
function info($message, array $context = array())
{
global $logger;
$logger->info($message, $context);
}
function debug($message, array $context = array())
{
global $logger;
$logger->debug($message, $context);
}
?>