|
21 | 21 | class BuffLog { |
22 | 22 |
|
23 | 23 | private static $logger = null; |
| 24 | + private static $currentVerbosity = Logger::WARNING; |
| 25 | + private static $verbosityList = [ |
| 26 | + "DEBUG" => Logger::DEBUG, |
| 27 | + "INFO" => Logger::INFO, |
| 28 | + "WARNING" => Logger::WARNING, |
| 29 | + "ERROR" => Logger::ERROR, |
| 30 | + "CRITICAL" => Logger::CRITICAL |
| 31 | + ]; |
24 | 32 |
|
25 | 33 | public static function debug($message, $context = [], $extra = []) |
26 | 34 | { |
| 35 | + self::setVerbosity(); |
| 36 | + if (self::$currentVerbosity > Logger::DEBUG) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
27 | 40 | $logOutput = self::formatLog($message, Logger::DEBUG, $context, $extra); |
28 | 41 | self::getLogger()->debug($logOutput); |
29 | 42 | } |
30 | 43 |
|
31 | 44 | public static function info($message, $context = [], $extra = []) |
32 | 45 | { |
| 46 | + self::setVerbosity(); |
| 47 | + if (self::$currentVerbosity > Logger::INFO) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
33 | 51 | $logOutput = self::formatLog($message, Logger::INFO, $context, $extra); |
34 | 52 | self::getLogger()->info($logOutput); |
35 | 53 | } |
36 | 54 |
|
37 | 55 | public static function warn($message, $context = [], $extra = []) |
38 | 56 | { |
| 57 | + self::setVerbosity(); |
| 58 | + if (self::$currentVerbosity > Logger::WARNING) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
39 | 62 | $logOutput = self::formatLog($message, Logger::WARNING, $context, $extra); |
40 | 63 | self::getLogger()->warn($logOutput); |
41 | 64 | } |
42 | 65 |
|
43 | 66 | public static function error($message, $context = [], $extra = []) |
44 | 67 | { |
| 68 | + self::setVerbosity(); |
| 69 | + if (self::$currentVerbosity > Logger::ERROR) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
45 | 73 | $logOutput = self::formatLog($message, Logger::ERROR, $context, $extra); |
46 | 74 | self::getLogger()->error($logOutput); |
47 | 75 | } |
48 | 76 |
|
49 | 77 | // @TODO: That one might could also create an alert in Datadog? |
50 | 78 | public static function critical($message, $context = [], $extra = []) |
51 | 79 | { |
| 80 | + self::setVerbosity(); |
52 | 81 | $logOutput = self::formatLog($message, Logger::CRITICAL, $context, $extra); |
53 | 82 | self::getLogger()->critical($logOutput); |
54 | 83 | } |
@@ -86,6 +115,15 @@ private static function createLogger() |
86 | 115 | return self::$logger; |
87 | 116 | } |
88 | 117 |
|
| 118 | + private static function setVerbosity() |
| 119 | + { |
| 120 | + $envVerbosity = getenv("LOG_VERBOSITY"); |
| 121 | + if ($envVerbosity !== FALSE && array_key_exists($envVerbosity, self::$verbosityList )) { |
| 122 | + self::$currentVerbosity = getenv("LOG_VERBOSITY"); |
| 123 | + } |
| 124 | + echo "Log verbosity set to " . self::$verbosity; |
| 125 | + } |
| 126 | + |
89 | 127 | public static function getLogger() |
90 | 128 | { |
91 | 129 | if (!isset(self::$logger)) { |
|
0 commit comments