/
Logger.php
167 lines (149 loc) · 4.83 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Core\Services;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface as LoggerContract;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger as Monolog;
use RuntimeException;
/**
* PSR-3 compatible Adapter for Monolog Logger
*
* The message MUST be a string or object implementing __toString().
*
* The message MAY contain placeholders in the form: {foo} where foo will be replaced by the context data in key "foo".
*
* The context array can contain arbitrary data, the only assumption that can be made by implementors is that if an
* Exception instance is given to produce a stack trace, it MUST be in a key named "exception".
*
* @see https://github.com/Seldaek/monolog Monolog Logger on GitHub
* @see http://www.php-fig.org/psr/psr-3/ PSR-3 Specification
*/
class Logger implements LoggerContract
{
/**
* Instance of Monolog Logger
*
* @var Monolog
*/
private $log;
/**
* Create a new Log instance.
*/
public function __construct()
{
$this->log = new Monolog('');
$this->log->pushProcessor(new PsrLogMessageProcessor);
$config = array_merge([
'type' => 'daily',
'level' => 'debug',
'max_files' => 5,
'app_file' => 'app.log',
'cli_file' => 'cli.log',
'permission' => 0664,
], config('logger', []));
$file = storage_path('logs/' . $config[is_console() ? 'cli_file' : 'app_file']);
$levels = $this->log->getLevels();
$level = $levels[strtoupper($config['level'])];
$format = "[%datetime%] %level_name%: %message% %context%\n";
switch ($config['type']) {
case 'single':
$this->log->pushHandler(
$handler = new StreamHandler($file, $level, true, $config['permission'], false)
);
$handler->setFormatter(new LineFormatter($format, null, true, true));
break;
case 'daily':
$this->log->pushHandler(
$handler = new RotatingFileHandler($file, $config['max_files'], $level, true, $config['permission'], false)
);
$handler->setFormatter(new LineFormatter($format, null, true, true));
break;
case 'syslog':
$this->log->pushHandler(
new SyslogHandler(config('app.name', 'Pletfix'), LOG_USER, $level)
);
break;
case 'errorlog':
$this->log->pushHandler(
$handler = new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $level)
);
$handler->setFormatter(new LineFormatter($format, null, true, true));
break;
default:
throw new RuntimeException('Invalid log type in configuration "app.log".');
}
}
/**
* @inheritdoc
*/
public function emergency($message, array $context = [])
{
$this->log->addRecord(Monolog::EMERGENCY, $message, $context);
}
/**
* @inheritdoc
*/
public function alert($message, array $context = [])
{
$this->log->addRecord(Monolog::ALERT, $message, $context);
}
/**
* @inheritdoc
*/
public function critical($message, array $context = [])
{
$this->log->addRecord(Monolog::CRITICAL, $message, $context);
}
/**
* @inheritdoc
*/
public function error($message, array $context = [])
{
$this->log->addRecord(Monolog::ERROR, $message, $context);
}
/**
* @inheritdoc
*/
public function warning($message, array $context = [])
{
$this->log->addRecord(Monolog::WARNING, $message, $context);
}
/**
* @inheritdoc
*/
public function notice($message, array $context = [])
{
$this->log->addRecord(Monolog::NOTICE, $message, $context);
}
/**
* @inheritdoc
*/
public function info($message, array $context = [])
{
$this->log->addRecord(Monolog::INFO, $message, $context);
}
/**
* @inheritdoc
*/
public function debug($message, array $context = [])
{
$this->log->addRecord(Monolog::DEBUG, $message, $context);
}
/**
* @inheritdoc
*/
public function log($level, $message, array $context = [])
{
$map = $this->log->getLevels();
$ulevel = strtoupper($level);
if (!isset($map[$ulevel])) {
throw new InvalidArgumentException('Log Level ' . $level . 'is not defined.');
}
$this->log->addRecord($map[$ulevel], $message, $context);
}
}