diff --git a/composer.json b/composer.json index e433618..cd617ba 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bufferapp/php-bufflog", "description": "PHP log libraries for Buffer services", - "version": "0.1.1", + "version": "0.1.2", "require": { "php": "^7.1", "monolog/monolog": "^1.20", diff --git a/src/BuffLog/BuffLog.php b/src/BuffLog/BuffLog.php index bb35a61..f6e6171 100644 --- a/src/BuffLog/BuffLog.php +++ b/src/BuffLog/BuffLog.php @@ -9,8 +9,16 @@ class BuffLog { protected static $instance; private static $logger = null; - private static $logOutputMethods = ['debug', 'info', 'notice', 'warning', 'error', 'critical']; - private static $extraAllowedMethods = ['getName', 'pushHandler', 'setHandlers', 'getHandlers', 'pushProcessor', 'getProcessors']; + // default verbosity starting at this level + private static $verbosityLevel = Logger::NOTICE; + + // verbosity can be changed with setting this env var + public static $logLevelEnvVar = "LOG_LEVEL"; + + // we can use strtolower(Logger::getLevels()) instead + private static $logOutputMethods = ['debug', 'info', 'notice', 'warning', 'error', 'critical']; + + private static $extraAllowedMethods = ['getName', 'pushHandler', 'setHandlers', 'getHandlers', 'pushProcessor', 'getProcessors', 'getLevels']; /** * Method to return the Monolog instance @@ -32,7 +40,19 @@ protected static function configureInstance() // define the logger name. This will make it easier for developers // to read and friendlier to identify where come the logs at a glance $logger = new Logger('php-bufflog'); - $handler = new StreamHandler('php://stdout'); + + $logLevelFromEnv = getenv(self::$logLevelEnvVar); + $monologLevels = $logger->getLevels(); + if ($logLevelFromEnv) { + // only if the level exists, we change the verbosity level + if (key_exists($logLevelFromEnv, $monologLevels)) { + self::$verbosityLevel = $monologLevels[$logLevelFromEnv]; + } else { + error_log(self::$logLevelEnvVar . "={$logLevelFromEnv} verbosity level does not exists. Please use: " . implode(', ', array_keys($monologLevels))); + } + } + + $handler = new StreamHandler('php://stdout', self::$verbosityLevel); $handler->setFormatter( new \Monolog\Formatter\JsonFormatter() ); $logger->pushHandler($handler); self::$instance = $logger; @@ -47,24 +67,16 @@ public static function __callStatic($methodName, $args) if (in_array($methodName, self::$logOutputMethods)) { - // @TODO: need to make sure we "output" only the correct level of log - // old version looked like: - // self::setVerbosity(); - // if (self::$currentVerbosity > Logger::WARNING) { - // return; - // } - self::enrichLog(); } // Where the magic happen. We "proxy" functions name with arguments to the Monolog instance return call_user_func_array(array(self::getLogger(), $methodName), $args); } else { - error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it"); } } else { - error_log("BuffLog::$methodName() does not exist"); + error_log("BuffLog::$methodName() method does not exist"); } } @@ -84,7 +96,7 @@ private static function enrichLog() // 'profileID' => $user->getProfileID() // ); - // Add traces information to logs to be able correlate with APM + // Add traces information to be able to correlate logs with APM $ddTraceSpan = \DDTrace\GlobalTracer::get()->getActiveSpan(); $record['context']['dd'] = [ "trace_id" => $ddTraceSpan->getTraceId(),