11<?php
22namespace Buffer ;
3+ require_once ('../../vendor/autoload.php ' );
34
45use Monolog \Logger ;
56use Monolog \Handler \StreamHandler as MonologStreamHandler ;
1920
2021class BuffLog {
2122
22- private $ logger ;
23+ private static $ logger = null ;
2324
24- public function __construct ( )
25+ public static function debug ( $ message )
2526 {
26- $ this ->createLogger ();
27- }
28-
29- public function debug ($ message )
30- {
31- $ logOutput = $ this ->formatLog ($ message , Logger::DEBUG , $ context = [], $ extra = []);
32- $ this ->getLogger ()->debug ($ logOutput );
27+ $ logOutput = self ::formatLog ($ message , Logger::DEBUG , $ context = [], $ extra = []);
28+ self ::getLogger ()->debug ($ logOutput );
3329 }
3430
3531 public function info ($ message )
@@ -44,10 +40,10 @@ public function warn($message)
4440 $ this ->getLogger ()->warn ($ logOutput );
4541 }
4642
47- public function error ($ message )
43+ public static function error ($ message )
4844 {
49- $ logOutput = $ this -> formatLog ($ message , Logger::ERROR , $ context = [], $ extra = []);
50- $ this -> getLogger ()->error ($ logOutput );
45+ $ logOutput = self :: formatLog ($ message , Logger::ERROR , $ context = [], $ extra = []);
46+ self :: getLogger ()->error ($ logOutput );
5147 }
5248
5349 // @TODO: That one might could also create an alert in Datadog?
@@ -67,26 +63,32 @@ private function formatLog($message, $level, $context = [], $extra = [])
6763 "extra " => $ extra
6864 ];
6965
66+ try {
67+ $ output = json_encode ($ output , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
68+ } catch (Exception $ e ) {
69+ error_log ("can't log your message " );
70+ }
71+
7072 return $ output ;
7173 }
7274
73- private function createLogger ()
75+ private static function createLogger ()
7476 {
75- $ logger = new Logger ('php-bufflog ' );
76- $ handler = new MonologStreamHandler ('php://stdout ' , Logger::INFO );
77-
78- $ logger ->pushHandler ($ handler );
77+ self ::$ logger = new Logger ('php-bufflog ' );
78+ $ handler = new MonologStreamHandler ('php://stdout ' );
7979
80- return $ logger ;
80+ self ::$ logger ->pushHandler ($ handler );
81+ return self ::$ logger ;
8182 }
8283
83- private function getLogger ()
84+ public static function getLogger ()
8485 {
85- if (!isset ($ this ->logger )) {
86- $ this ->logger = $ this ->createLogger ();
86+ if (!isset (self ::$ logger )) {
87+ echo "Initializing logger \n" ;
88+ self ::createLogger ();
8789 }
8890
89- return $ this -> logger ;
91+ return self :: $ logger ;
9092 }
9193
9294}
0 commit comments