1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Logger\Handlers\StandardFormatter |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/logger |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Logger\Formatters; |
22
|
|
|
|
23
|
|
|
use Psr\Log\LogLevel; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use AppserverIo\Logger\LogMessageInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The default formatter that uses vsprintf() to format the message. |
29
|
|
|
* |
30
|
|
|
* The following arguments are available and passed to vsprintf() |
31
|
|
|
* method in the given order: |
32
|
|
|
* |
33
|
|
|
* 1. date (formatted with the date format passed to the constructor) |
34
|
|
|
* 2. hostname (queried by PHP gethostname() method) |
35
|
|
|
* 3. loglevel |
36
|
|
|
* 4. message |
37
|
|
|
* 5. context (always JSON encoded) |
38
|
|
|
* |
39
|
|
|
* If you want to change the order of the arguments have a look at |
40
|
|
|
* the sprintf() documentation. |
41
|
|
|
* |
42
|
|
|
* @author Tim Wagner <[email protected]> |
43
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
44
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
45
|
|
|
* @link http://github.com/appserver-io/logger |
46
|
|
|
* @link http://www.appserver.io |
47
|
|
|
* @see http://php.net/manual/en/function.sprintf.php |
48
|
|
|
*/ |
49
|
|
|
class StandardFormatter implements FormatterInterface |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The message format to use, timestamp, id, line, message, context |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $messageFormat = '[%s] - %s (%s): %s %s'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The date format to use. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $dateFormat = 'Y-m-d H:i:s'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initializes the handler instance with channel name and log level. |
68
|
|
|
* |
69
|
|
|
* @param string|null $messageFormat The message format, MUST be valid for sprintf |
70
|
|
|
* @param string|null $dateFormat The date format, valid for PHP date() function |
71
|
|
|
*/ |
72
|
10 |
|
public function __construct($messageFormat = null, $dateFormat = null) |
73
|
|
|
{ |
74
|
|
|
// initialize message and date format |
75
|
10 |
|
if ($messageFormat != null && is_string($messageFormat)) { |
|
|
|
|
76
|
|
|
$this->messageFormat = $messageFormat; |
77
|
|
|
} |
78
|
10 |
|
if ($dateFormat != null && is_string($dateFormat)) { |
|
|
|
|
79
|
|
|
$this->dateFormat = $dateFormat; |
80
|
|
|
} |
81
|
10 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Formats and returns a string representation of the passed log message. |
85
|
|
|
* |
86
|
|
|
* @param \AppserverIo\Logger\LogMessageInterface $logMessage The log message we want to format |
87
|
|
|
* |
88
|
|
|
* @return string The formatted string representation for the log messsage |
89
|
|
|
*/ |
90
|
6 |
|
public function format(LogMessageInterface $logMessage) |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
// initialize the parameters for the formatted message |
94
|
|
|
$params = array( |
95
|
6 |
|
date($this->dateFormat), |
96
|
6 |
|
gethostname(), |
97
|
6 |
|
$logMessage->getLevel(), |
98
|
6 |
|
$logMessage->getMessage(), |
99
|
6 |
|
json_encode($logMessage->getContext()) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
// format, trim and return the message |
103
|
6 |
|
return trim(vsprintf($this->messageFormat, $params)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|