Skip to content

Commit 45f8fb1

Browse files
authored
Merge pull request #21 from bufferapp/task/type-safe
"Type safety"
2 parents 9ceb3b1 + 9678047 commit 45f8fb1

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bufferapp/php-bufflog",
33
"description": "PHP log libraries for Buffer services",
4-
"version": "0.1.5",
4+
"version": "0.1.6",
55
"require": {
66
"php": "^7.1",
77
"monolog/monolog": "^1.20",

example.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Buffer\BuffLog\Bufflog;
66

77
// putenv("LOG_VERBOSITY=WARNING");
8-
BuffLog::debug("I am a debug");
8+
BuffLog::debug("I am a debug string", 2, 2);
9+
BuffLog::debug(["I am a"]);
10+
BuffLog::debug("I am a", "test");
11+
BuffLog::debug("I am a debug string", 2, 2);
912
BuffLog::debug("I am a debug with context", ["my key" => " my value"]);
1013

1114
BuffLog::info("I am an info");
@@ -14,7 +17,7 @@
1417
BuffLog::warning("I am a warning");
1518
BuffLog::warning("I am a warning", ["duration" => "40ms"]);
1619

17-
BuffLog::error("I am an error");
20+
BuffLog::error(2);
1821
BuffLog::error("I am an error", ["mean" => "70"]);
1922

2023
BuffLog::critical("I am criticals information with a typo and you shouldn't see me!");

src/BuffLog/BuffLog.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ protected static function configureInstance()
4545
} else {
4646
// local envs don't need tracing
4747
if (getenv("ENVIRONMENT") !== "local") {
48-
error_log("Tip #1: Can't find \DDTrace\GlobalTracer class. Did you install the Datadog APM tracer extension? It will allow you to have logs enriched with traces making troubleshooting easier.");
49-
error_log("Tip #2: If you run a cli mode service (such as a worker), did you set the DD_TRACE_CLI_ENABLED env variable?");
48+
echo json_encode([
49+
"message" => "Can't find \DDTrace\GlobalTracer class. Did you install the Datadog APM tracer extension? It will allow you to have logs enriched with traces making troubleshooting easier. If you run a cli mode service (such as a worker), did you set the DD_TRACE_CLI_ENABLED env variable?",
50+
"level" => 300,
51+
"level_name" => "WARNING",
52+
"context" => ["bufflog_error" => "no_tracer"]
53+
]);
5054
}
5155
}
5256

@@ -110,15 +114,40 @@ public static function __callStatic($methodName, $args)
110114
if (in_array($methodName, array_merge(self::$logOutputMethods, self::$extraAllowedMethods))) {
111115

112116
if (in_array($methodName, self::$logOutputMethods)) {
113-
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
114-
return call_user_func_array(array(self::getLogger(), $methodName), $args);
117+
118+
if (self::checkLogParametersType($args)) {
119+
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
120+
return call_user_func_array(array(self::getLogger(), $methodName), $args);
121+
}
115122
}
116123
} else {
117-
error_log("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it");
124+
self::getLogger()->warning("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it", ["bufflog_error" => "method_not_supported"]);
118125
}
119126
} else {
120-
error_log("BuffLog::$methodName() method does not exist");
127+
self::getLogger()->warning("BuffLog::$methodName() method does not exist", ["bufflog_error" => "method_not_exist"]);
128+
}
129+
130+
return false;
131+
}
132+
133+
private static function checkLogParametersType($args)
134+
{
135+
if (count($args) > 2) {
136+
self::getLogger()->warning("BuffLog: Malformed logs: Too many parameters", ["bufflog_error" => "incorrect_parameters", "args" => $args]);
137+
return false;
121138
}
139+
140+
if (isset($args[0]) && !is_string($args[0])) {
141+
self::getLogger()->warning("BuffLog: Malformed logs: First parameter must be a string", ["args" => $args, "bufflog_error" => "incorrect_parameters"]);
142+
return false;
143+
}
144+
145+
if (isset($args[1]) && !is_array($args[1])) {
146+
self::getLogger()->warning("BuffLog: Malformed logs: Second parameter must be an array", ["args" => $args, "bufflog_error" => "incorrect_parameters"]);
147+
return false;
148+
}
149+
150+
return true;
122151
}
123152

124153
}

0 commit comments

Comments
 (0)