Skip to content

Commit 095f9af

Browse files
committed
controlling verbosity
1 parent 27303fd commit 095f9af

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/php-bufflog/BuffLog.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,63 @@
2121
class BuffLog {
2222

2323
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+
];
2432

2533
public static function debug($message, $context = [], $extra = [])
2634
{
35+
self::setVerbosity();
36+
if (self::$currentVerbosity > Logger::DEBUG) {
37+
return;
38+
}
39+
2740
$logOutput = self::formatLog($message, Logger::DEBUG, $context, $extra);
2841
self::getLogger()->debug($logOutput);
2942
}
3043

3144
public static function info($message, $context = [], $extra = [])
3245
{
46+
self::setVerbosity();
47+
if (self::$currentVerbosity > Logger::INFO) {
48+
return;
49+
}
50+
3351
$logOutput = self::formatLog($message, Logger::INFO, $context, $extra);
3452
self::getLogger()->info($logOutput);
3553
}
3654

3755
public static function warn($message, $context = [], $extra = [])
3856
{
57+
self::setVerbosity();
58+
if (self::$currentVerbosity > Logger::WARNING) {
59+
return;
60+
}
61+
3962
$logOutput = self::formatLog($message, Logger::WARNING, $context, $extra);
4063
self::getLogger()->warn($logOutput);
4164
}
4265

4366
public static function error($message, $context = [], $extra = [])
4467
{
68+
self::setVerbosity();
69+
if (self::$currentVerbosity > Logger::ERROR) {
70+
return;
71+
}
72+
4573
$logOutput = self::formatLog($message, Logger::ERROR, $context, $extra);
4674
self::getLogger()->error($logOutput);
4775
}
4876

4977
// @TODO: That one might could also create an alert in Datadog?
5078
public static function critical($message, $context = [], $extra = [])
5179
{
80+
self::setVerbosity();
5281
$logOutput = self::formatLog($message, Logger::CRITICAL, $context, $extra);
5382
self::getLogger()->critical($logOutput);
5483
}
@@ -86,6 +115,15 @@ private static function createLogger()
86115
return self::$logger;
87116
}
88117

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+
89127
public static function getLogger()
90128
{
91129
if (!isset(self::$logger)) {

0 commit comments

Comments
 (0)