2020-05-08 12:32:21 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
2021-04-13 18:04:19 +02:00
|
|
|
* https://ldapsaisie.org
|
2020-05-08 12:32:21 +02:00
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2020-09-10 10:38:35 +02:00
|
|
|
LSsession :: loadLSclass('LSlog_staticLoggerClass');
|
|
|
|
|
2020-05-08 12:32:21 +02:00
|
|
|
/**
|
|
|
|
* Logger class for LSlog
|
|
|
|
*
|
2022-12-31 21:15:19 +01:00
|
|
|
* @property-read string $name
|
|
|
|
* @property-read bool $enabled
|
|
|
|
* @property-read string $level
|
2020-05-08 12:32:21 +02:00
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
2020-09-10 10:38:35 +02:00
|
|
|
class LSlog_logger extends LSlog_staticLoggerClass {
|
2020-05-08 12:32:21 +02:00
|
|
|
|
2022-12-31 21:15:19 +01:00
|
|
|
/**
|
|
|
|
* The logger name
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-06-10 18:45:00 +02:00
|
|
|
private $name;
|
|
|
|
|
2022-12-31 21:15:19 +01:00
|
|
|
/**
|
|
|
|
* The handler configuration
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-01-03 12:51:45 +01:00
|
|
|
private $config = array();
|
2021-06-10 18:45:00 +02:00
|
|
|
|
2022-12-31 21:15:19 +01:00
|
|
|
/**
|
|
|
|
* Enabled state
|
|
|
|
* @var bool
|
|
|
|
*/
|
2023-01-03 12:51:45 +01:00
|
|
|
private $enabled = true;
|
2021-06-10 18:45:00 +02:00
|
|
|
|
2022-12-31 21:15:19 +01:00
|
|
|
/**
|
|
|
|
* Logger custom level
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2021-06-10 18:45:00 +02:00
|
|
|
private $level;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $name The logger name
|
|
|
|
* @param array $config The handler configuration (optional, default: array())
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function __construct($name, $config=array()) {
|
|
|
|
$this -> name = $name;
|
|
|
|
$this -> config = $config;
|
|
|
|
$this -> enabled = $this -> getConfig('enabled', true, 'boolean');
|
|
|
|
$this -> level = $this -> getConfig('level');
|
|
|
|
}
|
2020-05-08 12:32:21 +02:00
|
|
|
|
2020-09-08 17:29:10 +02:00
|
|
|
/**
|
|
|
|
* Allow conversion of LSlog_logger to string
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return string The string representation of the LSlog_logger
|
2020-09-08 17:29:10 +02:00
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return "<".get_class($this)." ".$this -> name.">";
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:45:00 +02:00
|
|
|
/**
|
|
|
|
* Get a configuration variable value
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $var The configuration variable name
|
|
|
|
* @param mixed $default The default value to return if configuration variable
|
2021-06-10 18:45:00 +02:00
|
|
|
* is not set (Default : null)
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $cast The type of expected value. The configuration variable
|
2021-06-10 18:45:00 +02:00
|
|
|
* value will be cast as this type. Could be : bool, int,
|
|
|
|
* float or string. (Optional, default : raw value)
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return mixed The configuration variable value
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function getConfig($var, $default=null, $cast=null) {
|
|
|
|
return LSconfig :: get($var, $default, $cast, $this -> config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get logger info
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $key The info name
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return mixed The info value
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function __get($key) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'name':
|
|
|
|
return $this -> name;
|
|
|
|
case 'enabled':
|
|
|
|
return $this -> enabled;
|
|
|
|
case 'level':
|
|
|
|
return $this -> level;
|
|
|
|
}
|
|
|
|
// Unknown key, log warning
|
|
|
|
self :: log_warning("$this -> __get($key): invalid property requested\n".LSlog :: get_debug_backtrace_context());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check level against configured level
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $level The level
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return bool True if a message with this level have to be logged, False otherwise
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function checkLevel($level) {
|
|
|
|
// If no level configured, always log
|
|
|
|
if (!$this -> enabled || !$this -> level)
|
|
|
|
return True;
|
|
|
|
return LSlog :: checkLevel($level, $this -> level);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $level The message level
|
|
|
|
* @param string $message The message
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function logging($level, $message) {
|
|
|
|
if (!$this -> enabled || !$this -> checkLevel($level))
|
|
|
|
return;
|
|
|
|
LSlog :: logging($level, $message, $this -> name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public logging methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level DEBUG
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function trace($message) {
|
|
|
|
$this -> logging('TRACE', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level DEBUG
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function debug($message) {
|
|
|
|
$this -> logging('DEBUG', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level INFO
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function info($message) {
|
|
|
|
$this -> logging('INFO', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level WARNING
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function warning($message) {
|
|
|
|
$this -> logging('WARNING', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level ERROR
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function error($message) {
|
|
|
|
$this -> logging('ERROR', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a message with level FATAL
|
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string $message The message to log
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function fatal($message) {
|
|
|
|
$this -> logging('FATAL', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log an exception
|
|
|
|
*
|
2022-12-31 21:15:19 +01:00
|
|
|
* @param Exception $exception The exception to log
|
2022-12-31 05:52:31 +01:00
|
|
|
* @param string|null $prefix Custom message prefix (optional, see LSlog :: exception())
|
|
|
|
* @param boolean $fatal Log exception as a fatal error (optional, default: true)
|
2021-06-10 18:45:00 +02:00
|
|
|
*
|
2022-12-31 05:52:31 +01:00
|
|
|
* @return void
|
2021-06-10 18:45:00 +02:00
|
|
|
**/
|
|
|
|
public function exception($exception, $prefix=null, $fatal=true) {
|
|
|
|
if (!$this -> enabled)
|
|
|
|
return;
|
|
|
|
LSlog :: exception($exception, $prefix, $fatal, $this -> name);
|
|
|
|
}
|
2020-05-08 12:32:21 +02:00
|
|
|
}
|