Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14346

Custom MY_Log.php

$
0
0
I have just made a MY_Log.php so I can over ride log write function so that it can create log files for each of the log levels so it is easier to view.

I would like to know if it is safe and will not effect any thing else?

It now writes the files like
  • DEBUG-04-08-2016
  • ERROR-04-08-2016
  • INFO-04-08-2016
And only shows messages belong to that log

PHP Code:
<?php

class MY_Log extends CI_Log {

    public function 
write_log($level$msg)
    {
        if (
$this->_enabled === FALSE)
        {
            return 
FALSE;
        }

        
$level strtoupper($level);

        if (( ! isset(
$this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
            && ! isset(
$this->_threshold_array[$this->_levels[$level]]))
        {
            return 
FALSE;
        }

        
$filepath $this->_log_path $level .'-'date('d-m-Y').'.'.$this->_file_ext;
        
        
$message '';

        if ( ! 
file_exists($filepath))
        {
            
$newfile TRUE;
            
// Only add protection to php files
            
if ($this->_file_ext === 'php')
            {
                
$message .= "";
            }
        }

        if ( ! 
$fp = @fopen($filepath'ab'))
        {
            return 
FALSE;
        }

        
flock($fpLOCK_EX);

        
// Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
        
if (strpos($this->_date_fmt'u') !== FALSE)
        {
            
$microtime_full microtime(TRUE);
            
$microtime_short sprintf("%06d", ($microtime_full floor($microtime_full)) * 1000000);
            
$date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short$microtime_full));
            
$date $date->format($this->_date_fmt);
        }
        else
        {
            
$date date($this->_date_fmt);
        }

        
$message .= $this->_format_line($level$date$msg);

        for (
$written 0$length strlen($message); $written $length$written += $result)
        {
            if ((
$result fwrite($fpsubstr($message$written))) === FALSE)
            {
                break;
            }
        }

        
flock($fpLOCK_UN);
        
fclose($fp);

        if (isset(
$newfile) && $newfile === TRUE)
        {
            
chmod($filepath$this->_file_permissions);
        }

        return 
is_int($result);
    }


Viewing all articles
Browse latest Browse all 14346

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>