diff --git a/src/Log.php b/src/Log.php index ba8642b..f69ef6b 100644 --- a/src/Log.php +++ b/src/Log.php @@ -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')); @@ -138,6 +147,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 @@ -170,6 +181,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))