Newer
Older
<?php
namespace Library;
/**
* Logger trait to add logging functionality to a class.
* If no logger is set all logging methods, error(), info(), etc. do nothing.
* The context for a log message can be provided with the ndc_push() and ndc_pop methods.
* NDC stands for Nested Diagnostic Context
*
* To use this trait add
* use \Library\Logger;
* at the begin of your class declaration.
*/
trait LoggerTrait {
/**
* PSR-3 compatible logger instance, if not set no logging is done
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* nested diagnostic context, used as context for log messages
* @var array
*/
protected $ndc_context = [];
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* initialize logger
*
* @param array $settings with (optional) keys: name, path, level and format
* @param string $base_dir base direcory for relative paths
* @return $this
*/
public function init_logger(array $settings, string $base_dir = ".") {
$name = isset($settings["name"]) ? $settings["name"] : "default";
$path = isset($settings["path"]) ? $settings["path"] : "php://stderr";
// adjust log path if it is not a php i/o stream or absolute path
// so only adjust relative paths
if (strncmp($path, 'php:', 4) != 0 && strncmp($path, '/', 1)) {
$path = "$base_dir/$path";
}
$level = isset($settings["level"]) ? $settings["level"] : "info";
$format = isset($settings["format"]) ? $settings["format"] : "[%datetime%] %channel% %level_name% %extra% %context%: %message%";
// create the logger
$logger = new \Monolog\Logger($name);
// add stream handler
$stream = new \Monolog\Handler\StreamHandler($path, $level);
$formatter = new \Monolog\Formatter\LineFormatter("$format\n");
$stream->setFormatter($formatter);
$logger->pushHandler($stream);
return $this->set_logger($logger);
}
/**
* get logger instance
* @return \Psr\Log\LoggerInterface or null if not set
*/
public function get_logger() {
return $this->logger;
}
/**
* set logger to send log messages to
* @param \Psr\Log\LoggerInterface $logger
*/
public function set_logger(\Psr\Log\LoggerInterface $logger) {
$this->logger = $logger;
return $this;
}
/**
* debug message
* @param string $message
* @param array $context if not set the ndc is used
*/
public function debug(string $message, array $context = null) {
if (isset($this->logger)) {
$this->logger->debug($message, isset($context) ? $context : $this->ndc_context);
}
/**
* info message
* @param string $message
* @param array $context if not set the ndc is used
*/
public function info(string $message, array $context = null) {
if (isset($this->logger)) {
$this->logger->info($message, isset($context) ? $context : $this->ndc_context);
}
/**
* warning message
* @param string $message
* @param array $context if not set the ndc is used
*/
public function warning(string $message, array $context = null) {
if (isset($this->logger)) {
$this->logger->warning($message, isset($context) ? $context : $this->ndc_context);
}
/**
* error message
* @param string $message
* @param array $context if not set the ndc is used
*/
public function error(string $message, array $context = null) {
if (isset($this->logger)) {
$this->logger->error($message, isset($context) ? $context : $this->ndc_context);
}
/**
* add message to the ndc (nested diagnostic context)
* @param string $message
*/
public function ndc_push(string $message){
$this->ndc_context[] = $message;
}
/**
* remove last message from the ndc (nested diagnostic context)
*/
public function ndc_pop(){
array_pop($this->ndc_context);