1313namespace Koded \Logging ;
1414
1515use Koded \Logging \Processors \{Cli , Processor };
16+ use DateTimeZone ;
1617use Psr \Log \LoggerTrait ;
1718use Throwable ;
1819
@@ -65,27 +66,27 @@ class Log implements Logger
6566 /**
6667 * @var bool Flag to control the messages processing
6768 */
68- private $ deferred = false ;
69+ private bool $ deferred = false ;
6970
7071 /**
7172 * @var string The date format for the message.
7273 */
73- private $ dateFormat ;
74+ private string $ dateFormat ;
7475
7576 /**
76- * @var string Valid timezone for the message.
77+ * @var DateTimeZone Valid timezone for the message.
7778 */
78- private $ timezone = ' UTC ' ;
79+ private DateTimeZone | bool $ timezone ;
7980
8081 /**
8182 * @var Processor[] Hash with all registered log processors.
8283 */
83- private $ processors = [];
84+ private array $ processors = [];
8485
8586 /**
8687 * @var array List with all accumulated messages.
8788 */
88- private $ messages = [];
89+ private array $ messages = [];
8990
9091 /**
9192 * Creates all requested log processors.
@@ -96,70 +97,48 @@ public function __construct(array $settings)
9697 {
9798 $ this ->deferred = (bool )($ settings ['deferred ' ] ?? false );
9899 $ this ->dateFormat = (string )($ settings ['dateformat ' ] ?? 'd/m/Y H:i:s.u ' );
99- $ this ->timezone = (string )($ settings ['timezone ' ] ?? $ this ->timezone );
100-
100+ if (false === $ this ->timezone = @\timezone_open ((string )($ settings ['timezone ' ] ?? 'UTC ' ))) {
101+ $ this ->timezone = \timezone_open ('UTC ' );
102+ }
101103 foreach ((array )($ settings ['loggers ' ] ?? []) as $ processor ) {
102104 $ this ->attach (new $ processor ['class ' ]($ processor ));
103105 }
104-
105106 if ($ this ->deferred ) {
106- register_shutdown_function ([$ this , 'process ' ]);
107+ \ register_shutdown_function ([$ this , 'process ' ]);
107108 }
108109 }
109110
110111 public function attach (Processor $ processor ): Logger
111112 {
112113 if (0 !== $ processor ->levels ()) {
113- $ this ->processors [spl_object_hash ($ processor )] = $ processor ;
114+ $ this ->processors [\ spl_object_hash ($ processor )] = $ processor ;
114115 }
115-
116116 return $ this ;
117117 }
118118
119119 public function log ($ level , $ message , array $ context = [])
120120 {
121121 try {
122- $ levelname = strtoupper ($ level );
123- $ level = constant ('self :: ' . $ levelname );
124- } catch (Throwable $ e ) {
125- $ levelname = 'LOG ' ;
122+ $ levelName = \ strtoupper ($ level );
123+ $ level = \ constant ('static :: ' . $ levelName );
124+ } catch (Throwable ) {
125+ $ levelName = 'LOG ' ;
126126 $ level = -1 ;
127127 }
128-
129128 $ this ->messages [] = [
130129 'level ' => $ level ,
131- 'levelname ' => $ levelname ,
130+ 'levelname ' => $ levelName ,
132131 'message ' => $ this ->formatMessage ($ message , $ context ),
133- 'timestamp ' => date_create_immutable ('now ' , timezone_open ( $ this ->timezone ) ?: null )->format ($ this ->dateFormat ),
132+ 'timestamp ' => \ date_create_immutable ('now ' , $ this ->timezone ?: null )->format ($ this ->dateFormat ),
134133 ];
135-
136134 $ this ->deferred || $ this ->process ();
137135 }
138136
139- /**
140- * Parses the message as in the interface specification.
141- *
142- * @param string|object $message A string or object that implements __toString
143- * @param array $params [optional] Arbitrary data with key-value pairs replacements
144- *
145- * @return string
146- */
147- private function formatMessage ($ message , array $ params = []): string
148- {
149- $ replacements = [];
150- foreach ($ params as $ k => $ v ) {
151- $ replacements ['{ ' . $ k . '} ' ] = $ v ;
152- }
153-
154- return strtr ((string )$ message , $ replacements );
155- }
156-
157137 public function process (): void
158138 {
159139 foreach ($ this ->processors as $ processor ) {
160140 $ processor ->update ($ this ->messages );
161141 }
162-
163142 $ this ->messages = [];
164143 }
165144
@@ -175,8 +154,24 @@ public function exception(Throwable $e, Processor $processor = null): void
175154
176155 public function detach (Processor $ processor ): Logger
177156 {
178- unset($ this ->processors [spl_object_hash ($ processor )]);
179-
157+ unset($ this ->processors [\spl_object_hash ($ processor )]);
180158 return $ this ;
181159 }
160+
161+ /**
162+ * Parses the message as in the interface specification.
163+ *
164+ * @param object|string $message A string or object that implements __toString
165+ * @param array $params [optional] Arbitrary data with key-value pairs replacements
166+ *
167+ * @return string
168+ */
169+ private function formatMessage (object |string $ message , array $ params = []): string
170+ {
171+ $ replacements = [];
172+ foreach ($ params as $ k => $ v ) {
173+ $ replacements ['{ ' . $ k . '} ' ] = $ v ;
174+ }
175+ return \strtr ((string )$ message , $ replacements );
176+ }
182177}
0 commit comments