Created
December 21, 2015 17:53
-
-
Save jm42/ac120fbdb5379c5c0b36 to your computer and use it in GitHub Desktop.
Simple logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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