Skip to content

Commit 4c5e14a

Browse files
committed
use magic method __callStatic to use all Monolog methods if needed
1 parent 7b1d780 commit 4c5e14a

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Bufflog::error("I am an error");
1717
Bufflog::error("I am an error", ["mean" => "70"]);
1818

19-
Bufflog::critical("I am critical information!");
19+
Bufflog::criticals("I am criticals information with a typo and you shouldn't see me!");
2020
Bufflog::critical("I am critical information!", ["user" => "betrand"]);
2121

2222
Bufflog::critical("I'm critical log, here some extra fancy informations",

src/php-bufflog/BuffLog.php

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Buffer;
33
require_once('vendor/autoload.php');
44

5-
use Monolog\Logger;
5+
use Monolog\Logger as Logger;
66
use Monolog\Handler\StreamHandler;
77

88
/*
@@ -40,7 +40,7 @@ class BuffLog {
4040
static public function getLogger()
4141
{
4242
if (! self::$instance) {
43-
self::configureInstance();
43+
self::configureInstance();
4444
}
4545
return self::$instance;
4646
}
@@ -54,62 +54,46 @@ protected static function configureInstance()
5454
$handler = new StreamHandler('php://stdout');
5555
$handler->setFormatter( new \Monolog\Formatter\JsonFormatter() );
5656
$logger->pushHandler($handler);
57-
5857
self::$instance = $logger;
5958
}
6059

61-
public static function debug($message, $context = [])
60+
// This will be called when a static method in the class doesn't exists
61+
public static function __callStatic($methodName, $args)
6262
{
63-
self::setVerbosity();
64-
if (self::$currentVerbosity > Logger::DEBUG) {
65-
return;
66-
}
67-
self::processLog();
68-
self::getLogger()->addDebug($message, $context);
69-
}
63+
$whitelistOutputMethods = ["debug", 'info', 'notice', 'warning', 'error', 'critical'];
64+
$whitelistExtraMethods = [];
7065

71-
public static function info($message, $context = [])
72-
{
73-
self::setVerbosity();
74-
if (self::$currentVerbosity > Logger::INFO) {
75-
return;
76-
}
66+
if (method_exists(self::getLogger(), $methodName)) {
7767

78-
self::processLog();
79-
self::getLogger()->addInfo($message, $context);
80-
}
68+
if (in_array($methodName, $whitelistOutputMethods)) {
8169

82-
public static function warning($message, $context = [])
83-
{
84-
self::setVerbosity();
85-
if (self::$currentVerbosity > Logger::WARNING) {
86-
return;
87-
}
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+
// }
8876

89-
self::processLog();
90-
self::getLogger()->addWarning($message, $context);
91-
}
77+
self::enrichLog();
78+
self::getLogger()->$methodName($args[0], isset($args[1]) ? $args[1] : []);
9279

93-
public static function error($message, $context = [])
94-
{
95-
self::setVerbosity();
96-
if (self::$currentVerbosity > Logger::ERROR) {
97-
return;
98-
}
80+
} elseif (in_array($methodName, $whitelistExtraMethods)) {
9981

100-
self::processLog();
101-
self::getLogger()->addError($message, $context);
102-
}
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]);
10385

104-
// @TODO: That one might could also create an alert in Datadog?
105-
public static function critical($message, $context = [])
106-
{
107-
self::setVerbosity();
108-
self::processLog();
109-
self::getLogger()->addCritical($message, $context);
86+
} else {
87+
88+
error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog repository to allow it");
89+
90+
}
91+
} else {
92+
error_log("BuffLog::$methodName() does not exist");
93+
}
11094
}
11195

112-
private function processLog()
96+
private function enrichLog()
11397
{
11498
// This should probably implemented as a Monolog Processor
11599
// https://github.com/Seldaek/monolog/tree/master/src/Monolog/Processor

0 commit comments

Comments
 (0)