-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
160 lines (138 loc) · 3.74 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
<?php
namespace Orkan\Filmweb;
use Pimple\Container;
/**
* A logging class
*
* @author Orkan
*/
class Logger
{
/**
* Logger instance
*
* @var \Monolog\Logger
*/
private $logger;
/**
* Dependency Injection Container
*
* @var Container
*/
private $app;
public function __construct( Container $app )
{
$this->app = $app;
// Merge configuration with defaults
$this->app['cfg'] = array_merge( $this->getDefaults(), $this->app['cfg'] );
$cfg = $this->app['cfg'];
// Create the Logger
$this->logger = new \Monolog\Logger( $cfg['log_channel'] ); // %channel%
$logformat = new \Monolog\Formatter\LineFormatter( $cfg['log_format'], $cfg['log_datetime'] );
$logstream = new \Monolog\Handler\RotatingFileHandler( $cfg['log_file'], $cfg['log_keep'] );
$logstream->setFormatter( $logformat );
$this->logger->pushHandler( $logstream ); // DEBUG = 100; log everything, INFO = 200; log above >= 200
$this->logger->setTimezone( new \DateTimeZone( $cfg['log_timezone'] ) );
// Remove sensitive data from log
if ( isset( $this->app['cfg']['logger_mask'] ) ) {
$search = $this->app['cfg']['logger_mask']['search'];
$replace = $this->app['cfg']['logger_mask']['replace'];
$this->logger->pushProcessor( function ( $entry ) use ($search, $replace ) {
$entry['message'] = str_replace( $search, $replace, $entry['message'] );
return $entry;
} );
}
}
/**
* Get default config
*
* @return array Default config
*/
public function getDefaults()
{
/* @formatter:off */
return array(
'log_channel' => basename( __FILE__ ),
'log_file' => dirname( __DIR__ ) . DIRECTORY_SEPARATOR . 'filmweb-api.log',
'log_timezone' => 'UTC', // @see https://www.php.net/manual/en/timezones.php
'is_debug' => false,
/* Leave these for \Monolog defaults or define your own in $cfg */
'log_keep' => 0, // \Monolog\Handler\RotatingFileHandler->maxFiles
'log_datetime' => null, // 'Y-m-d\TH:i:s.uP'
'log_format' => null, // "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
);
/* @formatter:on */
}
/**
* Get the name of last calling function
*
* @return string In format [Namespace\Class->method()] $message
*/
private function backtrace()
{
if ( ! $this->app['cfg']['is_debug'] ) {
return '';
}
$level = 2; // backtrace history (before this class)
// https://www.php.net/manual/en/function.debug-backtrace.php
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $level + 1 );
return isset( $trace[$level] ) ? "[{$trace[$level]['class']}{$trace[$level]['type']}{$trace[$level]['function']}()] " : '';
}
/**
* Call \Monolog\Logger instance only if in debug mode
*
* @param string $message
*/
public function debug( string $message ): void
{
if ( ! $this->app['cfg']['is_debug'] ) {
return;
}
$this->logger->debug( $this->backtrace() . $message );
}
/**
* Call \Monolog\Logger instance
*
* @param string $message
*/
public function error( string $message ): void
{
$this->logger->error( $this->backtrace() . $message );
}
/**
* Call \Monolog\Logger instance
*
* @param string $message
*/
public function warning( string $message ): void
{
$this->logger->warning( $this->backtrace() . $message );
}
/**
* Call \Monolog\Logger instance
*
* @param string $message
*/
public function notice( string $message ): void
{
$this->logger->notice( $message );
}
/**
* Call \Monolog\Logger instance
*
* @param string $message
*/
public function info( string $message ): void
{
$this->logger->info( $message );
}
/**
* Dumb method to cover error type in Filmweb->errorHandler()
*
* @param string $message
*/
public function unknown( string $message ): void
{
$this->logger->info( $message );
}
}