Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Log;
4
 
5
use Closure;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Log\Events\MessageLogged;
10
use Psr\Log\LoggerInterface;
11
use RuntimeException;
12
 
13
class Logger implements LoggerInterface
14
{
15
    /**
16
     * The underlying logger implementation.
17
     *
18
     * @var \Psr\Log\LoggerInterface
19
     */
20
    protected $logger;
21
 
22
    /**
23
     * The event dispatcher instance.
24
     *
25
     * @var \Illuminate\Contracts\Events\Dispatcher|null
26
     */
27
    protected $dispatcher;
28
 
29
    /**
30
     * Any context to be added to logs.
31
     *
32
     * @var array
33
     */
34
    protected $context = [];
35
 
36
    /**
37
     * Create a new log writer instance.
38
     *
39
     * @param  \Psr\Log\LoggerInterface  $logger
40
     * @param  \Illuminate\Contracts\Events\Dispatcher|null  $dispatcher
41
     * @return void
42
     */
43
    public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
44
    {
45
        $this->logger = $logger;
46
        $this->dispatcher = $dispatcher;
47
    }
48
 
49
    /**
50
     * Log an emergency message to the logs.
51
     *
52
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
53
     * @param  array  $context
54
     * @return void
55
     */
56
    public function emergency($message, array $context = []): void
57
    {
58
        $this->writeLog(__FUNCTION__, $message, $context);
59
    }
60
 
61
    /**
62
     * Log an alert message to the logs.
63
     *
64
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
65
     * @param  array  $context
66
     * @return void
67
     */
68
    public function alert($message, array $context = []): void
69
    {
70
        $this->writeLog(__FUNCTION__, $message, $context);
71
    }
72
 
73
    /**
74
     * Log a critical message to the logs.
75
     *
76
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
77
     * @param  array  $context
78
     * @return void
79
     */
80
    public function critical($message, array $context = []): void
81
    {
82
        $this->writeLog(__FUNCTION__, $message, $context);
83
    }
84
 
85
    /**
86
     * Log an error message to the logs.
87
     *
88
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
89
     * @param  array  $context
90
     * @return void
91
     */
92
    public function error($message, array $context = []): void
93
    {
94
        $this->writeLog(__FUNCTION__, $message, $context);
95
    }
96
 
97
    /**
98
     * Log a warning message to the logs.
99
     *
100
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
101
     * @param  array  $context
102
     * @return void
103
     */
104
    public function warning($message, array $context = []): void
105
    {
106
        $this->writeLog(__FUNCTION__, $message, $context);
107
    }
108
 
109
    /**
110
     * Log a notice to the logs.
111
     *
112
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
113
     * @param  array  $context
114
     * @return void
115
     */
116
    public function notice($message, array $context = []): void
117
    {
118
        $this->writeLog(__FUNCTION__, $message, $context);
119
    }
120
 
121
    /**
122
     * Log an informational message to the logs.
123
     *
124
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
125
     * @param  array  $context
126
     * @return void
127
     */
128
    public function info($message, array $context = []): void
129
    {
130
        $this->writeLog(__FUNCTION__, $message, $context);
131
    }
132
 
133
    /**
134
     * Log a debug message to the logs.
135
     *
136
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
137
     * @param  array  $context
138
     * @return void
139
     */
140
    public function debug($message, array $context = []): void
141
    {
142
        $this->writeLog(__FUNCTION__, $message, $context);
143
    }
144
 
145
    /**
146
     * Log a message to the logs.
147
     *
148
     * @param  string  $level
149
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
150
     * @param  array  $context
151
     * @return void
152
     */
153
    public function log($level, $message, array $context = []): void
154
    {
155
        $this->writeLog($level, $message, $context);
156
    }
157
 
158
    /**
159
     * Dynamically pass log calls into the writer.
160
     *
161
     * @param  string  $level
162
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
163
     * @param  array  $context
164
     * @return void
165
     */
166
    public function write($level, $message, array $context = []): void
167
    {
168
        $this->writeLog($level, $message, $context);
169
    }
170
 
171
    /**
172
     * Write a message to the log.
173
     *
174
     * @param  string  $level
175
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
176
     * @param  array  $context
177
     * @return void
178
     */
179
    protected function writeLog($level, $message, $context): void
180
    {
181
        $this->logger->{$level}(
182
            $message = $this->formatMessage($message),
183
            $context = array_merge($this->context, $context)
184
        );
185
 
186
        $this->fireLogEvent($level, $message, $context);
187
    }
188
 
189
    /**
190
     * Add context to all future logs.
191
     *
192
     * @param  array  $context
193
     * @return $this
194
     */
195
    public function withContext(array $context = [])
196
    {
197
        $this->context = array_merge($this->context, $context);
198
 
199
        return $this;
200
    }
201
 
202
    /**
203
     * Flush the existing context array.
204
     *
205
     * @return $this
206
     */
207
    public function withoutContext()
208
    {
209
        $this->context = [];
210
 
211
        return $this;
212
    }
213
 
214
    /**
215
     * Register a new callback handler for when a log event is triggered.
216
     *
217
     * @param  \Closure  $callback
218
     * @return void
219
     *
220
     * @throws \RuntimeException
221
     */
222
    public function listen(Closure $callback)
223
    {
224
        if (! isset($this->dispatcher)) {
225
            throw new RuntimeException('Events dispatcher has not been set.');
226
        }
227
 
228
        $this->dispatcher->listen(MessageLogged::class, $callback);
229
    }
230
 
231
    /**
232
     * Fires a log event.
233
     *
234
     * @param  string  $level
235
     * @param  string  $message
236
     * @param  array  $context
237
     * @return void
238
     */
239
    protected function fireLogEvent($level, $message, array $context = [])
240
    {
241
        // If the event dispatcher is set, we will pass along the parameters to the
242
        // log listeners. These are useful for building profilers or other tools
243
        // that aggregate all of the log messages for a given "request" cycle.
244
        if (isset($this->dispatcher)) {
245
            $this->dispatcher->dispatch(new MessageLogged($level, $message, $context));
246
        }
247
    }
248
 
249
    /**
250
     * Format the parameters for the logger.
251
     *
252
     * @param  \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string  $message
253
     * @return string
254
     */
255
    protected function formatMessage($message)
256
    {
257
        if (is_array($message)) {
258
            return var_export($message, true);
259
        } elseif ($message instanceof Jsonable) {
260
            return $message->toJson();
261
        } elseif ($message instanceof Arrayable) {
262
            return var_export($message->toArray(), true);
263
        }
264
 
265
        return (string) $message;
266
    }
267
 
268
    /**
269
     * Get the underlying logger implementation.
270
     *
271
     * @return \Psr\Log\LoggerInterface
272
     */
273
    public function getLogger()
274
    {
275
        return $this->logger;
276
    }
277
 
278
    /**
279
     * Get the event dispatcher instance.
280
     *
281
     * @return \Illuminate\Contracts\Events\Dispatcher
282
     */
283
    public function getEventDispatcher()
284
    {
285
        return $this->dispatcher;
286
    }
287
 
288
    /**
289
     * Set the event dispatcher instance.
290
     *
291
     * @param  \Illuminate\Contracts\Events\Dispatcher  $dispatcher
292
     * @return void
293
     */
294
    public function setEventDispatcher(Dispatcher $dispatcher)
295
    {
296
        $this->dispatcher = $dispatcher;
297
    }
298
 
299
    /**
300
     * Dynamically proxy method calls to the underlying logger.
301
     *
302
     * @param  string  $method
303
     * @param  array  $parameters
304
     * @return mixed
305
     */
306
    public function __call($method, $parameters)
307
    {
308
        return $this->logger->{$method}(...$parameters);
309
    }
310
}