@@ -9,8 +9,14 @@ class BuffLog {
99 protected static $ instance ;
1010 private static $ logger = null ;
1111
12+ // we output every logs starting this level
13+ // It can be changed with setting an environment
14+ private static $ verbosityLevel = Logger::NOTICE ;
15+
16+ // we can use strtolower(Logger::getLevels()) instead
1217 private static $ logOutputMethods = ['debug ' , 'info ' , 'notice ' , 'warning ' , 'error ' , 'critical ' ];
13- private static $ extraAllowedMethods = ['getName ' , 'pushHandler ' , 'setHandlers ' , 'getHandlers ' , 'pushProcessor ' , 'getProcessors ' ];
18+
19+ private static $ extraAllowedMethods = ['getName ' , 'pushHandler ' , 'setHandlers ' , 'getHandlers ' , 'pushProcessor ' , 'getProcessors ' , 'getLevels ' ];
1420
1521 /**
1622 * Method to return the Monolog instance
@@ -47,27 +53,50 @@ public static function __callStatic($methodName, $args)
4753
4854 if (in_array ($ methodName , self ::$ logOutputMethods )) {
4955
50- // @TODO: need to make sure we "output" only the correct level of log
51- // old version looked like:
52- // self::setVerbosity();
53- // if (self::$currentVerbosity > Logger::WARNING) {
54- // return;
55- // }
56+ if (! self ::shouldWriteLogs ($ methodName )) {
57+ return ;
58+ }
5659
5760 self ::enrichLog ();
5861 }
5962 // Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
6063 return call_user_func_array (array (self ::getLogger (), $ methodName ), $ args );
6164
6265 } else {
63-
6466 error_log ("BuffLog:: $ methodName() is not supported yet. Add it to the BuffLog whitelist to allow it " );
6567 }
6668 } else {
6769 error_log ("BuffLog:: $ methodName() does not exist " );
6870 }
6971 }
7072
73+ private static function setVerbosityLevel ()
74+ {
75+ $ logLevelFromEnv = getenv ("LOG_LEVEL " );
76+ $ monologLevels = self ::getLogger ()->getLevels ();
77+ if ($ logLevelFromEnv ) {
78+ // only if the level exists, we change the verbosity level
79+ if (key_exists ($ logLevelFromEnv , $ monologLevels )) {
80+ self ::$ verbosityLevel = $ monologLevels [$ logLevelFromEnv ];
81+ } else {
82+ error_log ("LOG_LEVEL {$ logLevelFromEnv } is not defined, use one of: " . implode (', ' , array_keys ($ monologLevels )));
83+ }
84+ }
85+ }
86+
87+
88+ private static function shouldWriteLogs ($ methodName )
89+ {
90+ // Optimization: we could move this at the singleton initialization
91+ self ::setVerbosityLevel ();
92+
93+ if ( self ::getLogger ()->getLevels ()[strtoupper ($ methodName )] >= self ::$ verbosityLevel ) {
94+ return true ;
95+ }
96+
97+ return false ;
98+ }
99+
71100 private static function enrichLog ()
72101 {
73102 // This should probably implemented as a Monolog Processor
0 commit comments