Skip to content

Commit 3ebb9f8

Browse files
committed
use call_user_func_array to proxy every functions with their parameters to Monolog
1 parent 4c5e14a commit 3ebb9f8

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

example.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
]
3030
]
3131
);
32+
33+
echo(BuffLog::getName());

src/php-bufflog/BuffLog.php

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,21 @@
55
use Monolog\Logger as Logger;
66
use Monolog\Handler\StreamHandler;
77

8-
/*
9-
Level of logs we use:
10-
11-
This level require manual action to appear in Datadog Logs
12-
Logger::DEBUG
13-
Logger::INFO
14-
15-
Everything at this level appears by default in Datadog Logs
16-
Logger::WARNING
17-
Logger::ERROR
18-
Logger::CRITICAL
19-
*/
20-
218
class BuffLog {
229

23-
private static $logger = null;
24-
private static $currentVerbosity = Logger::WARNING;
25-
private static $verbosityList = [
10+
protected static $instance;
11+
private static $logger = null;
12+
private static $currentVerbosity = Logger::WARNING;
13+
private static $verbosityList = [
2614
"DEBUG" => Logger::DEBUG,
2715
"INFO" => Logger::INFO,
2816
"WARNING" => Logger::WARNING,
2917
"ERROR" => Logger::ERROR,
3018
"CRITICAL" => Logger::CRITICAL
3119
];
3220

33-
protected static $instance;
21+
private static $logOutputMethods = ['debug', 'info', 'notice', 'warning', 'error', 'critical'];
22+
private static $extraAllowedMethods = ['getName'];
3423

3524
/**
3625
* Method to return the Monolog instance
@@ -41,7 +30,8 @@ static public function getLogger()
4130
{
4231
if (! self::$instance) {
4332
self::configureInstance();
44-
}
33+
34+
}
4535
return self::$instance;
4636
}
4737

@@ -60,33 +50,27 @@ protected static function configureInstance()
6050
// This will be called when a static method in the class doesn't exists
6151
public static function __callStatic($methodName, $args)
6252
{
63-
$whitelistOutputMethods = ["debug", 'info', 'notice', 'warning', 'error', 'critical'];
64-
$whitelistExtraMethods = [];
65-
6653
if (method_exists(self::getLogger(), $methodName)) {
6754

68-
if (in_array($methodName, $whitelistOutputMethods)) {
55+
if (in_array($methodName, array_merge(self::$logOutputMethods, self::$extraAllowedMethods))) {
6956

70-
// @TODO: need to make sure we "output" only the correct level of log
71-
// old version looked like:
72-
// self::setVerbosity();
73-
// if (self::$currentVerbosity > Logger::WARNING) {
74-
// return;
75-
// }
57+
if (in_array($methodName, self::$logOutputMethods)) {
7658

77-
self::enrichLog();
78-
self::getLogger()->$methodName($args[0], isset($args[1]) ? $args[1] : []);
59+
// @TODO: need to make sure we "output" only the correct level of log
60+
// old version looked like:
61+
// self::setVerbosity();
62+
// if (self::$currentVerbosity > Logger::WARNING) {
63+
// return;
64+
// }
7965

80-
} elseif (in_array($methodName, $whitelistExtraMethods)) {
81-
82-
// this might be tricky. we do not know how many arguments the dev will call.
83-
// Might have mutltiple solutions (counting/varargs...), which one would be the right one?
84-
// self::getLogger()->$methodName($args[0]);
66+
self::enrichLog();
67+
}
68+
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
69+
return call_user_func_array(array(self::getLogger(), $methodName), $args);
8570

8671
} else {
8772

88-
error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog repository to allow it");
89-
73+
error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it");
9074
}
9175
} else {
9276
error_log("BuffLog::$methodName() does not exist");

0 commit comments

Comments
 (0)