-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogFormatter.php
75 lines (67 loc) · 1.86 KB
/
LogFormatter.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
<?php
/**
* This file is part of the Apix Project.
*
* (c) Franck Cassedanne <franck at ouarz.net>
*
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
*/
namespace Apix\Log;
/**
* Standard log formatter.
*
* @author Franck Cassedanne <franck at ouarz.net>
*/
class LogFormatter implements LogFormatterInterface
{
/**
* Holds this log separator.
* @var string
*/
public $separator = PHP_EOL;
/**
* Interpolates context values into the message placeholders.
*
* Builds a replacement array with braces around the context keys.
* It replaces {foo} with the value from $context['foo'].
*
* @param string $message
* @param array $context
* @return string
*/
public function interpolate($message, array $context = array())
{
$replaces = array();
foreach ($context as $key => $val) {
if (is_bool($val)) {
$val = '[bool: ' . (int) $val . ']';
} elseif (is_null($val)
|| is_scalar($val)
|| ( is_object($val) && method_exists($val, '__toString') )
) {
$val = (string) $val;
} elseif (is_array($val) || is_object($val)) {
$val = @json_encode($val);
} else {
$val = '[type: ' . gettype($val) . ']';
}
$replaces['{' . $key . '}'] = $val;
}
return strtr($message, $replaces);
}
/**
* Formats the given log entry.
*
* @param LogEntry $log The log entry to format.
* @return string
*/
public function format(LogEntry $log)
{
return sprintf(
'[%s] %s %s',
date('Y-m-d H:i:s', $log->timestamp),
strtoupper($log->name),
self::interpolate($log->message, $log->context)
);
}
}