0, 'DEBUG' => 1, 'INFO' => 2, 'WARNING' => 3, 'ERROR' => 4, 'FATAL' => 5, ); /* * Default log level * @var string */ protected static string $default_level = 'WARNING'; /* * Current log level * @var string|null */ protected static $level = null; /* * Log PHP errors levels (as specified to set_error_handler()) * Default (set in self::init() method): * - In TRACE or DEBUG: E_ALL & ~E_STRICT * - Otherwise: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED */ protected static $php_errors_levels = null; // Custom fatal error handler protected static $fatal_error_handler = null; /* * Initialization * @param string $filepath * @param string|null $level * @param int|null $php_errors_levels * @return void */ public static function init($filepath, $level=null, $php_errors_levels=null) { self :: $filepath = $filepath; // Set default log level (if not defined or invalid) if (is_null($level)) { self :: $level = self :: $default_level; } elseif (!array_key_exists($level, self :: $levels)) { self :: $level = self :: $default_level; self :: warning( "Invalid log level value found in configuration (%s). ". "Set as default (%s).", $level, self :: $default_level); } else { self :: $level = $level; } // Log PHP errors if (!is_null($php_errors_levels)) self :: $php_errors_levels = $php_errors_levels; elseif (in_array(self :: $level, array('DEBUG', 'TRACE'))) self :: $php_errors_levels = E_ALL & ~E_STRICT; else self :: $php_errors_levels = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED; set_error_handler(array('EesyPHP\\Log', 'on_php_error'), self :: $php_errors_levels); // Log uncatched exceptions set_exception_handler(array('EesyPHP\\Log', 'exception')); } /** * Log a message * @param string $level The message level (key of self :: $levels) * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function log($level, $message, ...$extra_args) { global $auth_user, $argv; if (!array_key_exists($level, self :: $levels)) $level = self :: $default_level; if (self :: $levels[$level] < self :: $levels[self :: $level]) return true; if(is_null(self :: $file_fd)) { self :: $file_fd = fopen(self :: $filepath, 'a'); } // Extra arguments passed, format message using sprintf if ($extra_args) { $message = call_user_func_array( 'sprintf', array_merge(array($message), $extra_args) ); } if (php_sapi_name() == "cli") { $msg = implode(' - ', array( date('Y/m/d H:i:s'), basename($argv[0]), $level, $message ))."\n"; } else { $msg = array( date('Y/m/d H:i:s'), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], ); if (isset($auth_user)) $msg[] = ($auth_user['username']?$auth_user['username']:'anonymous'); $msg[] = $level; $msg[] = $message; $msg = implode(' - ', $msg)."\n"; } fwrite(self :: $file_fd, $msg); if ($level == 'FATAL') if (!is_null(self :: $fatal_error_handler)) call_user_func(self :: $fatal_error_handler, $message); elseif (function_exists('fatal_error')) fatal_error($message); else die("\n$message\n\n"); elseif (php_sapi_name() == "cli") echo $msg; return true; } /** * Log a trace message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function trace($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('TRACE', $message), $extra_args) ); } /** * Log a debug message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function debug($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('DEBUG', $message), $extra_args) ); } /** * Log an info message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function info($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('INFO', $message), $extra_args) ); } /** * Log an warning message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function warning($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('WARNING', $message), $extra_args) ); } /** * Log an error message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function error($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('ERROR', $message), $extra_args) ); } /** * Log an fatal message * @param string $message The message to log * @param array $extra_args Extra arguments to use to compute message using sprintf * @return true */ public static function fatal($message, ...$extra_args) { return call_user_func_array( array('EesyPHP\\Log', 'log'), array_merge(array('FATAL', $message), $extra_args) ); } /** * Register a contextual fatal error handler * @param null|callable $handler The fatal error handler (set as null to reset) * @return void */ public static function register_fatal_error_handler($handler) { // @phpstan-ignore-next-line if ($handler && !is_callable($handler)) self :: fatal('Invalid fatal error handler provided: it is not callable !'); self :: $fatal_error_handler = ($handler?$handler:null); } /** * Change of current log file * @param string $file The new log file path * @return bool */ public static function change_filepath($file) { if ($file == self :: $filepath) return True; if (self :: $file_fd) { fclose(self :: $file_fd); self :: $file_fd = null; } self :: $filepath = $file; return True; } /* ******************************************************************************* * Handle exception logging ******************************************************************************* */ /** * Get the current backtrace * @param int $ignore_last The number of last levels to ignore * @return string */ public static function get_debug_backtrace_context($ignore_last=0) { $traces = debug_backtrace(); // Also ignore this function it self $ignore_last++; if (!is_array($traces) || count($traces) <= $ignore_last) return ""; $msg = array(); for ($i=$ignore_last; $i < count($traces); $i++) { $trace = array("#$i"); if (isset($traces[$i]['file'])) $trace[] = $traces[$i]['file'].(isset($traces[$i]['line'])?":".$traces[$i]['line']:""); // @phpstan-ignore-next-line if (isset($traces[$i]['class']) && isset($traces[$i]['function'])) $trace[] = implode(" ", array( $traces[$i]['class'], $traces[$i]['type'], $traces[$i]['function']. "()")); elseif (isset($traces[$i]['function'])) $trace[] = $traces[$i]['function']. "()"; $msg[] = implode(" - ", $trace); } return implode("\n", $msg); } /** * Log an exception * @param Throwable $exception * @param string|null $prefix The prefix of the log message * (optional, default: "An exception occured") * @return void */ public static function exception($exception, $prefix=null) { SentryIntegration :: log($exception); // If more than 2 arguments passed, format prefix message using sprintf if ($prefix && func_num_args() > 2) { $prefix = call_user_func_array( 'sprintf', array_merge(array($prefix), array_slice(func_get_args(), 2)) ); } self :: error( "%s:\n%s\n## %s:%d : %s", ($prefix?$prefix:"An exception occured"), self::get_debug_backtrace_context(1), $exception->getFile(), $exception->getLine(), $exception->getMessage()); } /* ******************************************************************************* * Handle PHP error logging ******************************************************************************* */ /** * Convert PHP error number to the corresponding label * @param int $errno * @return string */ public static function errno2type($errno) { $constants = get_defined_constants(); if (is_array($constants)) foreach($constants as $label => $value) if ($value == $errno && preg_match('/^E_(.*)$/', $label, $m)) return $m[1]; return 'UNKNOWN ERROR #'.$errno; } /** * Log a PHP error * Note: method design to be used as callable by set_error_handler() * @param int $errno The error number * @param string $errstr The error message * @param string $errfile The filename that the error was raised in * @param int $errline The line number where the error was raised * @return false Return false to let the normal error handler continues. */ public static function on_php_error($errno, $errstr, $errfile, $errline) { self :: error( "A PHP error occured : [%s] %s\nFile : %s (line : %d)", self :: errno2type($errno), $errstr, $errfile, $errline ); return False; } } # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab