Last active
January 7, 2021 21:39
-
-
Save figaw/a61df1bfab530a4901c3c514e01c2757 to your computer and use it in GitHub Desktop.
Basic logging for PHP in JSON to stdout with Loglevel configurable from environment variables.
This file contains hidden or 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 | |
$LOGLEVEL = 5; | |
if( isset($_SERVER['LOGLEVEL']) && is_numeric(Log::LEVELS[$_SERVER['LOGLEVEL']])){ | |
$LOGLEVEL = Log::LEVELS[$_SERVER['LOGLEVEL']]; | |
} | |
abstract class Log | |
{ | |
const LEVELS = array( | |
"ALL" => 0, | |
"TRACE" => 1, | |
"DEBUG" => 2, | |
"INFO" => 3, | |
"WARN" => 4, | |
"ERROR" => 5 | |
); | |
} | |
function trace($msg){ | |
if($GLOBALS['LOGLEVEL'] > Log::LEVELS['TRACE']) return; | |
formatAndPrint("TRACE", $msg); | |
} | |
function debug($msg){ | |
if($GLOBALS['LOGLEVEL'] > Log::LEVELS['DEBUG']) return; | |
formatAndPrint("DEBUG", $msg); | |
} | |
function info($msg){ | |
if($GLOBALS['LOGLEVEL'] > Log::LEVELS['INFO']) return; | |
formatAndPrint("INFO", $msg); | |
} | |
function warn($msg){ | |
if($GLOBALS['LOGLEVEL'] > Log::LEVELS['WARN']) return; | |
formatAndPrint("WARN", $msg); | |
} | |
function error($msg, $exception = null){ | |
if($GLOBALS['LOGLEVEL'] > Log::LEVELS['ERROR']) return; | |
formatAndPrint("ERROR", $msg, $exception); | |
} | |
function formatAndPrint( $level, $msg, $exception = null) | |
{ | |
$obj = array( | |
"timestamp" => date(DateTime::ISO8601), | |
"level" => $level, | |
"msg" => $msg, | |
); | |
if(isset($exception)) | |
$obj['exception'] = $exception->getMessage(); | |
$flags = JSON_UNESCAPED_SLASHES; | |
if($GLOBALS['DEVELOPMENT']) | |
$flags = $flags | JSON_PRETTY_PRINT; | |
$message = json_encode($obj, $flags); | |
file_put_contents("php://stdout" , $message.PHP_EOL); | |
} | |
/* | |
## Usage | |
### somewhere you load libraries.. | |
`require('./Log.php');` | |
### in the environment | |
LOGLEVEL in ("ALL", "TRACE", "DEBUG", "INFO", "WARN", "ERROR") # default is ERROR | |
DEVELOPMENT in (true, false) # pretty-print json in logs, default is false | |
### put this wherever | |
trace("THIS IS A trace message"); | |
debug("THIS IS A debug message"); | |
info("THIS IS A info message"); | |
warn("THIS IS A warn message"); | |
error("THIS IS A error message"); | |
error("THIS IS A error message", new Exception("With Exception")); | |
## Example output | |
{ | |
"timestamp": "2021-01-07T21:13:21+0000", | |
"level": "WARN", | |
"msg": "THIS IS A warn message" | |
} | |
{ | |
"timestamp": "2021-01-07T21:13:21+0000", | |
"level": "ERROR", | |
"msg": "THIS IS A error message" | |
} | |
{ | |
"timestamp": "2021-01-07T21:13:21+0000", | |
"level": "ERROR", | |
"msg": "THIS IS A error message", | |
"exception": "With Exception" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wanted basic logging in PHP in JSON format, without installing modules. Loglevel is configurable from the environment.
I'm sharing this because from what I could Google it looks like I'm not the only one that could use something like this.
Rules for usage
"Look, Mom, I made a framework!..."
Heavily inspired by a tiny logger I wrote for Java https://gitlab.com/com.figaw.util/loglite
and augmented error_logger,
From: https://stackoverflow.com/a/35400363/1104755