Last active
August 29, 2015 14:16
-
-
Save vanjor/053aae2d40720ba82c7f to your computer and use it in GitHub Desktop.
Log extension for Codeigniter 3 with 3 updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
// reference to http://tutorialcodeigniter.com/blog/log-level-fix.html | |
class MY_Log extends CI_Log { | |
public function __construct() { | |
parent::__construct(); | |
//Update 1. updated log levels according to the correct order | |
$this -> _levels = array('ERROR' => '1', 'INFO' => '2', 'DEBUG' => '3', 'ALL' => '4'); | |
// http://stackoverflow.com/questions/15242972/codeigniter-how-to-get-realitive-absolute-path-dynamically-just-outside-of-appli | |
// Update 2. the log_path should be absolute path for using shutdown_hook, thus the file dir should be ok | |
if (substr($this -> _log_path, 0, 1) !== '/') { | |
$this -> _log_path = FCPATH . $this -> _log_path; | |
} | |
} | |
/** | |
* Write Log File | Override office log pattern | |
* | |
* Generally this function will be called using the global log_message() function | |
* | |
* @param string the error level | |
* @param string the error message | |
* @param bool whether the error is a native PHP error | |
* @return bool | |
*/ | |
public function write_log($level = 'error', $msg, $php_error = FALSE) { | |
if ($this -> _enabled === FALSE) { | |
return FALSE; | |
} | |
$level = strtoupper($level); | |
if (!isset($this -> _levels[$level]) OR ($this -> _levels[$level] > $this -> _threshold)) { | |
return FALSE; | |
} | |
// Update 3. add rotation | |
$filepath = $this -> _log_path . 'application.' . 'log.' . date('Ymd'); | |
$message = ''; | |
if (!$fp = @fopen($filepath, FOPEN_WRITE_CREATE)) { | |
return FALSE; | |
} | |
$message .= $level . ' ' . (($level == 'INFO') ? ' -' : '-') . ' ' . date($this -> _date_fmt) . ' --> ' . $msg . "\n"; | |
flock($fp, LOCK_EX); | |
fwrite($fp, $message); | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
@chmod($filepath, FILE_WRITE_MODE); | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment