Skip to content

Instantly share code, notes, and snippets.

@jm42
Created December 21, 2015 17:53
Show Gist options
  • Save jm42/ac120fbdb5379c5c0b36 to your computer and use it in GitHub Desktop.
Save jm42/ac120fbdb5379c5c0b36 to your computer and use it in GitHub Desktop.
Simple logger
<?php
// Smaller and friendlier (?) alternative to https://github.com/badphp/logger
function logger($path, $priority=0) {
return function($level, $msg) use($path, $priority) {
if ($level > $priority) {
return false;
}
$args = array_slice(func_get_args(), 1);
if (count($args) > 1) {
$msg = call_user_func_array('sprintf', $args);
}
return error_log("$msg\n", 3, $path);
};
}
switch ($env = getenv('PHP_ENV') ?: 'dev') {
case 'dev': $log_level = LOG_DEBUG; break;
case 'test': $log_level = LOG_INFO; break;
default: $log_level = LOG_ERR; break;
}
$logger = logger("$env.log", $log_level);
$logger(LOG_CRIT, "The application is unavailable");
$logger(LOG_DEBUG, "The value %d should be %s", 123, '123');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment