Compare commits

..

2 commits

View file

@ -19,6 +19,12 @@ class Log {
*/
protected static $file_fd = null;
/**
* PHP error_log() fallback if no filepath configured or fail to open log file
* @var bool
*/
protected static $error_log_fallback = true;
/*
* Log Levels
* @var array(string,int)
@ -77,6 +83,9 @@ class Log {
else
self :: $filepath = App::get('log.file_path');
// PHP error_log() fallback
self :: $error_log_fallback = App::get('log.error_log_fallback', true, 'bool');
// Set log level
self :: set_level($level?$level:App::get('log.level'));
@ -124,6 +133,20 @@ class Log {
set_exception_handler(array('EesyPHP\\Log', 'exception'));
}
/**
* Get remote IP address
* @return string
*/
public static function get_remote_addr(){
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER))
return $_SERVER["HTTP_X_FORWARDED_FOR"];
if (array_key_exists('REMOTE_ADDR', $_SERVER))
return $_SERVER["REMOTE_ADDR"];
if (array_key_exists('HTTP_CLIENT_IP', $_SERVER))
return $_SERVER["HTTP_CLIENT_IP"];
return '';
}
/**
* Log a message
* @param string $level The message level (key of self :: $levels)
@ -138,6 +161,8 @@ class Log {
if (self :: $levels[$level] < self :: $levels[self :: $level]) return true;
if(self :: $filepath && is_null(self :: $file_fd)) {
self :: $file_fd = fopen(self :: $filepath, 'a');
if (self :: $file_fd)
self :: error('Fail to open log file (%s)', self :: $filepath);
}
// Extra arguments passed, format message using sprintf
@ -160,7 +185,7 @@ class Log {
$msg = array(
date('Y/m/d H:i:s'),
$_SERVER['REQUEST_URI'],
$_SERVER['REMOTE_ADDR'],
self :: get_remote_addr(),
);
if (Auth::enabled())
$msg[] = (Auth::user()?Auth::user():'anonymous');
@ -170,6 +195,8 @@ class Log {
}
if (self :: $file_fd)
fwrite(self :: $file_fd, $msg);
elseif (self :: $error_log_fallback)
error_log($msg);
if ($level == 'FATAL')
if (!is_null(self :: $fatal_error_handler))