Welcome Guest, Not a member yet? Register   Sign In
Log/Logging to different files
#1

[eluser]Unknown[/eluser]
Hi all,

I just tweaked the logging code so that I could log to an arbitrary files.
I know its not wise to change the core code but this was too minor to write a separate
Log library or extend the core Log library.

I am posting the code hoping it might be useful for someone.

----------- Code Changes --------------

system/codeigniter/Common.php line 241

Add $filepath to the function definition

Code:
function log_message($level = 'error', $message, $php_error = FALSE, [b]$filepath[/b])
{
    static $LOG;
    
    $config =& get_config();
    if ($config['log_threshold'] == 0)
    {
        return;
    }

    $LOG =& load_class('Log');
    $LOG->write_log($level, $message, $php_error, [b]$filepath[/b]);
}

system/libraries/Log.php line 75

Add $filepath to the function definition

Code:
function write_log($level = 'error', $msg, $php_error = FALSE, [b]$filepath[/b])

inside this function on line 98

replace this code with

Code:
$filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
        $message  = '';
with

Code:
if(empty($filepath))
            $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
        else
            $filepath = $this->log_path. $filepath .'-'.date('Y-m-d').EXT;
        
        $message  = '';


------- Usage ---------

Normally we use
log_message('error', " msg to log ") ;

You can optionally use now
log_message('error', " msg to log", '', 'app' ) ;

The later will be logged to some file like app-2009-12-31.php the log directory.

---------------

Derek, It would be great if you can accomodate this small modification in the coming releases.

regds,
Shikhar
#2

[eluser]Unknown[/eluser]
Hi,

I was trying to tweak this to pass additional parameters to log.
Code:
function log_message($level = 'error', $message, $php_error = FALSE, $arguments = array())
{
  static $_log;

  if (config_item('log_threshold') == 0)
  {
   return;
  }

  $_log =& load_class('Log');
  $_log->write_log($level, $message, $php_error, $arguments);
}

This doesn't work. I tried printing $arguments and I always get empty array (Even if I'm passing the argument). What is the role of
Code:
[b] and [\b]
in your example?




Theme © iAndrew 2016 - Forum software by © MyBB