|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Logger\LogMessage |
|
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; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Thread-Safe log message implemenation. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link http://github.com/appserver-io/logger |
|
30
|
|
|
* @link http://www.appserver.io |
|
31
|
|
|
*/ |
|
32
|
|
|
class LogMessage implements LogMessageInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initializes the message with the necessary information. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $id The unique-ID of the log message |
|
39
|
|
|
* @param string $level The log level |
|
40
|
|
|
* @param string $message The message to be logged |
|
41
|
|
|
* @param array $context The message context |
|
42
|
|
|
*/ |
|
43
|
8 |
|
public function __construct($id, $level, $message, array $context = array()) |
|
44
|
|
|
{ |
|
45
|
8 |
|
$this->id = $id; |
|
|
|
|
|
|
46
|
8 |
|
$this->level = $level; |
|
|
|
|
|
|
47
|
8 |
|
$this->message = $message; |
|
|
|
|
|
|
48
|
8 |
|
$this->context = $context; |
|
|
|
|
|
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the unique-ID of the log message. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string The unique-ID |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getId() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the log level. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string The log level |
|
65
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel |
|
66
|
|
|
*/ |
|
67
|
6 |
|
public function getLevel() |
|
68
|
|
|
{ |
|
69
|
6 |
|
return $this->level; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the message we want to log. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string The message to be logged |
|
76
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function getMessage() |
|
79
|
|
|
{ |
|
80
|
6 |
|
return $this->message; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the context we're logging. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array The log context |
|
87
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#13-context |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function getContext() |
|
90
|
|
|
{ |
|
91
|
6 |
|
return $this->context; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets the context we want to log. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $context The log context |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#13-context |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setContext(array $context) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->context = $context; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Merges the values of the passed array into the context. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $toMerge The array with the data to merge |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#13-context |
|
114
|
|
|
*/ |
|
115
|
|
|
public function mergeIntoContext(array $toMerge) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->context = array_merge($this->context, $toMerge); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Appends the passed key/value pair to the context. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $key The key to merge the value with |
|
124
|
|
|
* @param mixed $value The value to merge |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
* @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#13-context |
|
128
|
|
|
*/ |
|
129
|
|
|
public function appendToContext($key, $value) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->context[$key] = $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: