Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 25 additions & 13 deletions src/BuffLog/BuffLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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");
}
}

Expand All @@ -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(),
Expand Down