Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created September 25, 2019 20:10
Show Gist options
  • Save BlackScorp/f0991a054e128577049e2076aecd85b6 to your computer and use it in GitHub Desktop.
Save BlackScorp/f0991a054e128577049e2076aecd85b6 to your computer and use it in GitHub Desktop.
Code zum Youtube Tutorial https://youtu.be/y-GsYEnguAU
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
define('EMERGENCY', 'emergency');
define('ALERT', 'alert');
define('CRITICAL', 'critical');
define('ERROR', 'error');
define('WARNING', 'warning');
define('NOTICE', 'notice');
define('INFO', 'info');
define('DEBUG', 'debug');
function logger(string $level, callable $handler = null){
static $handlers = [];
if($handler){
return $handlers[$level][]=$handler;
}
if(isset($handlers[$level])){
return $handlers[$level];
}
return [function(){}];
}
function logMessage(string $level, string $message, array $context = []) {
$handlers = logger($level);
$record = [
'message' => $message,
'context' => $context,
'level' => $level,
'datetime' => date('Y-m-d H:i:s')
];
foreach($handlers as $handler){
$handler($record);
}
}
function debug(string $message,array $context = []){
logMessage(DEBUG, $message,$context);
}
function info(string $message,array $context = []){
logMessage(INFO, $message,$context);
}
function notice(string $message,array $context = []){
logMessage(NOTICE, $message,$context);
}
function warning(string $message,array $context = []){
logMessage(WARNING, $message,$context);
}
function error(string $message,array $context = []){
logMessage(ERROR, $message,$context);
}
function critical(string $message,array $context = []){
logMessage(CRITICAL, $message,$context);
}
function emergency(string $message,array $context = []){
logMessage(EMERGENCY, $message,$context);
}
logger(INFO,function(array $record){
var_dump($record);
});
info('Hallo Welt');
debug( 'Hallo Welt');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment