Laravel-Log

思考并回答以下问题:

源码

Illuminate\Log\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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace Illuminate\Log;

use Closuore;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Log\Events\MessageLogged;
use Psr\Log\LoggerInterface;
use RuntimeException;

class Logger implements LoggerInterface
{
/**
* The underlying logger implementation.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher|null
*/
protected $dispatcher;

/**
* Create a new log writer instance.
*
* @param \Psr\Log\LoggerInterface $logger
* @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher
* @return void
*/
public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
{
$this->logger = $logger;
$this->dispatcher = $dispatcher;
}

/**
* Log an emergency message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function emergency($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}

/**
* Log a message to the logs.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
{
$this->writeLog($level, $message, $context);
}

/**
* Dynamically pass log calls into the writer.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function write($level, $message, array $context = [])
{
$this->writeLog($level, $message, $context);
}

/**
* Write a message to the log.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function writeLog($level, $message, $context)
{
$this->logger->{$level}($message = $this->formatMessage($message), $context);

$this->fireLogEvent($level, $message, $context);
}

/**
* Register a new callback handler for when a log event is triggered.
*
* @param \Closure $callback
* @return void
*
* @throws \RuntimeException
*/
public function listen(Closure $callback)
{
if (! isset($this->dispatcher))
{
throw new RuntimeException('Events dispatcher has not been set.');
}

$this->dispatcher->listen(MessageLogged::class, $callback);
}

/**
* Fires a log event.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function fireLogEvent($level, $message, array $context = [])
{
// 如果设置了事件分配器,则将参数传递给日志侦听器。
// 这些对于构建概要分析器或其他工具(在给定的“请求”周期内汇总所有日志消息)很有用。
if (isset($this->dispatcher)) {
$this->dispatcher->dispatch(new MessageLogged($level, $message, $context));
}
}

/**
* Format the parameters for the logger.
*
* @param mixed $message
* @return mixed
*/
protected function formatMessage($message)
{
if (is_array($message))
{
return var_export($message, true);
}
elseif ($message instanceof Jsonable)
{
return $message->toJson();
}
elseif ($message instanceof Arrayable)
{
return var_export($message->toArray(), true);
}

return $message;
}

/**
* Get the underlying logger implementation.
*
* @return \Psr\Log\LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}

/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Contracts\Events\Dispatcher
*/
public function getEventDispatcher()
{
return $this->dispatcher;
}

/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function setEventDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Dynamically proxy method calls to the underlying logger.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->logger->{$method}(...$parameters);
}
}

Illuminate\Mail\Events\MessageSending.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
<?php

namespace Illuminate\Mail\Events;

class MessageSending
{
/**
* The Swift message instance.
*
* @var \Swift_Message
*/
public $message;

/**
* The message data.
*
* @var array
*/
public $data;

/**
* Create a new event instance.
*
* @param \Swift_Message $message
* @param array $data
* @return void
*/
public function __construct($message, $data = [])
{
$this->data = $data;
$this->message = $message;
}
}

Illuminate\Log\ParsesLogConfiguration.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
namespace Illuminate\Log;

use InvalidArgumentException;
use Monolog\Logger as Monolog;

trait ParsesLogConfiguration
{
/**
* The Log levels.
*
* @var array
*/
protected $levels = [
'debug' => Monolog::DEBUG,
'info' => Monolog::INFO,
'notice' => Monolog::NOTICE,
'warning' => Monolog::WARNING,
'error' => Monolog::ERROR,
'critical' => Monolog::CRITICAL,
'alert' => Monolog::ALERT,
'emergency' => Monolog::EMERGENCY,
];

/**
* Get fallback log channel name.
*
* @return string
*/
abstract protected function getFallbackChannelName();

/**
* Parse the string level into a Monolog constant.
*
* @param array $config
* @return int
*
* @throws \InvalidArgumentException
*/
protected function level(array $config)
{
$level = $config['level'] ?? 'debug';

if (isset($this->levels[$level])) {
return $this->levels[$level];
}

throw new InvalidArgumentException('Invalid log level.');
}

/**
* Extract the log channel from the given configuration.
*
* @param array $config
* @return string
*/
protected function parseChannel(array $config)
{
return $config['name'] ?? $this->getFallbackChannelName();
}
}

Illuminate\Log\LogServiceProvider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

namespace Illuminate\Log;

use Illuminate\Support\ServiceProvider;

class LogServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('log', function ($app) {
return new LogManager($app);
});
}
}

Illuminate\Log\LogManager.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
<?php

namespace Illuminate\Log;

use Closure;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\FormattableHandlerInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\SlackWebhookHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Handler\WhatFailureGroupHandler;
use Monolog\Logger as Monolog;
use Psr\Log\LoggerInterface;
use Throwable;

class LogManager implements LoggerInterface
{
use ParsesLogConfiguration;

/**
* The application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;

/**
* The array of resolved channels.
*
* @var array
*/
protected $channels = [];

/**
* The registered custom driver creators.
*
* @var array
*/
protected $customCreators = [];

/**
* The standard date format to use when writing logs.
*
* @var string
*/
protected $dateFormat = 'Y-m-d H:i:s';

/**
* Create a new Log manager instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct($app)
{
$this->app = $app;
}

/**
* Register a custom driver creator Closure.
*
* @param string $driver
* @param \Closure $callback
* @return $this
*/
public function extend($driver, Closure $callback)
{
$this->customCreators[$driver] = $callback->bindTo($this, $this);

return $this;
}
}
0%