Skip to content

"Type safety" #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2020
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.5",
"version": "0.1.6",
"require": {
"php": "^7.1",
"monolog/monolog": "^1.20",
Expand Down
7 changes: 5 additions & 2 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Buffer\BuffLog\Bufflog;

// putenv("LOG_VERBOSITY=WARNING");
BuffLog::debug("I am a debug");
BuffLog::debug("I am a debug string", 2, 2);
BuffLog::debug(["I am a"]);
BuffLog::debug("I am a", "test");
BuffLog::debug("I am a debug string", 2, 2);
BuffLog::debug("I am a debug with context", ["my key" => " my value"]);

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

BuffLog::error("I am an error");
BuffLog::error(2);
BuffLog::error("I am an error", ["mean" => "70"]);

BuffLog::critical("I am criticals information with a typo and you shouldn't see me!");
Expand Down
41 changes: 35 additions & 6 deletions src/BuffLog/BuffLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ protected static function configureInstance()
} else {
// local envs don't need tracing
if (getenv("ENVIRONMENT") !== "local") {
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.");
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?");
echo json_encode([
"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?",
"level" => 300,
"level_name" => "WARNING",
"context" => ["bufflog_error" => "no_tracer"]
]);
Comment on lines +48 to +53
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the logger isn't ready yet, we have to do this manually

}
}

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

if (in_array($methodName, self::$logOutputMethods)) {
// Where the magic happen. We "proxy" functions name with arguments to the Monolog instance
return call_user_func_array(array(self::getLogger(), $methodName), $args);

if (self::checkLogParametersType($args)) {
// 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");
self::getLogger()->warning("BuffLog::$methodName() is not supported yet. Add it to the BuffLog whitelist to allow it", ["bufflog_error" => "method_not_supported"]);
}
} else {
error_log("BuffLog::$methodName() method does not exist");
self::getLogger()->warning("BuffLog::$methodName() method does not exist", ["bufflog_error" => "method_not_exist"]);
}

return false;
}

private static function checkLogParametersType($args)
{
if (count($args) > 2) {
self::getLogger()->warning("BuffLog: Malformed logs: Too many parameters", ["bufflog_error" => "incorrect_parameters", "args" => $args]);
return false;
}

if (isset($args[0]) && !is_string($args[0])) {
self::getLogger()->warning("BuffLog: Malformed logs: First parameter must be a string", ["args" => $args, "bufflog_error" => "incorrect_parameters"]);
return false;
}

if (isset($args[1]) && !is_array($args[1])) {
self::getLogger()->warning("BuffLog: Malformed logs: Second parameter must be an array", ["args" => $args, "bufflog_error" => "incorrect_parameters"]);
return false;
}

return true;
}

}