Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UMonolog.php
Go to the documentation of this file.
1<?php
2
4
6use Monolog\Handler\StreamHandler;
7use Monolog\Formatter\JsonFormatter;
10
20class UMonolog extends Logger {
26 private $handler;
27
28 public function __construct($name, $level = \Monolog\Logger::INFO, $path = 'logs/app.log') {
29 $this->loggerInstance = new \Monolog\Logger ( $name??'application' );
30 $this->handler = new StreamHandler ( ROOT . DS . $path, $level );
31 $this->handler->setFormatter ( new JsonFormatter () );
32 $this->loggerInstance->pushHandler ( $this->handler );
33 }
34
35 private function createContext($context, $part, $extra = null) {
36 return compact ( 'context', 'part', 'extra' );
37 }
38
39 public function addProcessor($callback) {
40 $this->loggerInstance->pushProcessor ( $callback );
41 }
42
43 public function _log($level, $context, $message, $part, $extra) {
44 $this->loggerInstance->log ( $level, $message, $this->createContext ( $context, $part, $extra ) );
45 }
46
47 public function _info($context, $message, $part, $extra) {
48 $this->loggerInstance->info ( $message, $this->createContext ( $context, $part, $extra ) );
49 }
50
51 public function _warn($context, $message, $part, $extra) {
52 $this->loggerInstance->warning ( $message, $this->createContext ( $context, $part, $extra ) );
53 }
54
55 public function _error($context, $message, $part, $extra) {
56 $this->loggerInstance->error ( $message, $this->createContext ( $context, $part, $extra ) );
57 }
58
59 public function _alert($context, $message, $part, $extra) {
60 $this->loggerInstance->alert ( $message, $this->createContext ( $context, $part, $extra ) );
61 }
62
63 public function _critical($context, $message, $part, $extra) {
64 $this->loggerInstance->critical ( $message, $this->createContext ( $context, $part, $extra ) );
65 }
66
67 public function _asObjects($reverse = true, $maxlines = 10, $contexts = null) {
68 return UFileSystem::getLines ( $this->handler->getUrl (), $reverse, $maxlines, function (&$objects, $line) use ($contexts) {
69 $jso = json_decode ( $line );
70 if ($jso !== null) {
71 if ($contexts === null || self::inContext ( $contexts, $jso->context->context )) {
72 if (isset ( $jso->context->extra )) {
73 if (is_object ( $jso->context->extra )) {
74 $extra = [ ];
75 foreach ( $jso->context->extra as $k => $v ) {
76 if (is_object ( $v )) {
77 $v = json_encode ( $v );
78 }
79 $extra [] = "$k: $v";
80 }
81 $jso->context->extra = $extra;
82 }
83 }
84 LogMessage::addMessage ( $objects, new LogMessage ( $jso->message, $jso->context->context, $jso->context->part, $jso->level, $jso->datetime, $jso->context->extra ) );
85 }
86 }
87 } );
88 }
89
90 public function _clearAll() {
91 $this->handler->close ();
92 UFileSystem::deleteFile ( $this->handler->getUrl () );
93 }
94
95 public function _close() {
96 $this->handler->close ();
97 }
98
99 public function _registerError() {
100 // TODO register error handlers
101 }
102}
103
static addMessage(&$messages, LogMessage $newMessage)
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Logger class for Monolog Ubiquity\log\libraries$UMonolog This class is part of Ubiquity.
Definition UMonolog.php:20
_warn($context, $message, $part, $extra)
Definition UMonolog.php:51
_alert($context, $message, $part, $extra)
Definition UMonolog.php:59
_error($context, $message, $part, $extra)
Definition UMonolog.php:55
_info($context, $message, $part, $extra)
Definition UMonolog.php:47
_log($level, $context, $message, $part, $extra)
Definition UMonolog.php:43
_critical($context, $message, $part, $extra)
Definition UMonolog.php:63
__construct($name, $level=\Monolog\Logger::INFO, $path='logs/app.log')
Definition UMonolog.php:28
createContext($context, $part, $extra=null)
Definition UMonolog.php:35
_asObjects($reverse=true, $maxlines=10, $contexts=null)
Definition UMonolog.php:67
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.