6use Monolog\Handler\StreamHandler;
7use Monolog\Formatter\JsonFormatter;
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 );
36 return compact (
'context',
'part',
'extra' );
40 $this->loggerInstance->pushProcessor ( $callback );
43 public function _log($level, $context, $message, $part, $extra) {
44 $this->loggerInstance->log ( $level, $message, $this->
createContext ( $context, $part, $extra ) );
47 public function _info($context, $message, $part, $extra) {
48 $this->loggerInstance->info ( $message, $this->
createContext ( $context, $part, $extra ) );
51 public function _warn($context, $message, $part, $extra) {
52 $this->loggerInstance->warning ( $message, $this->
createContext ( $context, $part, $extra ) );
55 public function _error($context, $message, $part, $extra) {
56 $this->loggerInstance->error ( $message, $this->
createContext ( $context, $part, $extra ) );
59 public function _alert($context, $message, $part, $extra) {
60 $this->loggerInstance->alert ( $message, $this->
createContext ( $context, $part, $extra ) );
63 public function _critical($context, $message, $part, $extra) {
64 $this->loggerInstance->critical ( $message, $this->
createContext ( $context, $part, $extra ) );
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 );
71 if ($contexts ===
null || self::inContext ( $contexts, $jso->context->context )) {
72 if (isset ( $jso->context->extra )) {
73 if (is_object ( $jso->context->extra )) {
75 foreach ( $jso->context->extra as $k => $v ) {
76 if (is_object ( $v )) {
77 $v = json_encode ( $v );
81 $jso->context->extra = $extra;
84 LogMessage::addMessage ( $objects,
new LogMessage ( $jso->message, $jso->context->context, $jso->context->part, $jso->level, $jso->datetime, $jso->context->extra ) );
91 $this->handler->close ();
92 UFileSystem::deleteFile ( $this->handler->getUrl () );
96 $this->handler->close ();
static addMessage(&$messages, LogMessage $newMessage)
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Logger class for Monolog Ubiquity\log\libraries$UMonolog This class is part of Ubiquity.
_warn($context, $message, $part, $extra)
_alert($context, $message, $part, $extra)
_error($context, $message, $part, $extra)
_info($context, $message, $part, $extra)
_log($level, $context, $message, $part, $extra)
_critical($context, $message, $part, $extra)
__construct($name, $level=\Monolog\Logger::INFO, $path='logs/app.log')
createContext($context, $part, $extra=null)
_asObjects($reverse=true, $maxlines=10, $contexts=null)
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.