Welcome Guest, Not a member yet? Register   Sign In
MY_Log
#1

[eluser]Chris Newton[/eluser]
I prefer to have more flexibility in the error levels I choose to log than ERROR, DEBUG, and INFO. Additionally, sometimes I don't want to see 1 INFO message amid hundreds of DEBUG messages. To solve this for myself, I created the MY_Log library. It's very simple to set up and use.

What my library does is this: Look for a new config item containing an array of Log levels, which could contain, some, all, or none, of the default log levels, as well as allow you to create your own log levels. For instance:

Code:
$config['show_in_log']= array('ERROR','CUSTOM_LOG_MSG','MY_MESSAGE');
Would display any messages created by using log_message('ERROR', 'msg'); log_message('CUSTOM_LOG_MSG','msg'); log_message('MY_MESSAGE','msg);

Also, if you add this library and set the array to empty
Code:
$config['show_in_log']=array();
All messages of all levels will be logged. Deleting that config item completely will default to the standard log_threshold config item.

*****************************
To use this yourself, add this library to your library folder (this assumes 'MY_' is your controller override prefix) and add the config item found way down at the bottom to your primary config file.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* MY_Logging Class
*
* This library assumes that you have a config item called
* $config['show_in_log'] = array();
* you can then create any error level you would like, using the following format
* $config['show_in_log']= array('DEBUG','ERROR','INFO','SPECIAL','MY_ERROR_GROUP','ETC_GROUP');
* Setting the array to empty will log all error messages.
* Deleting this config item entirely will default to the standard
* error logging threshold config item.
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Logging
* @author        ExpressionEngine Dev Team. Mod by Chris Newton
*/
class MY_Log extends CI_Log {
    /**
     * Constructor
     *
     * @access    public
     * @param    array the array of loggable items
     * @param    string    the log file path
     * @param     string     the error threshold
     * @param    string    the date formatting codes
     */
    function MY_Log()
    {
        parent::CI_Log();
        $config =& get_config();
        if (isset ($config['show_in_log']))
        {
            $show_in_log=$config['show_in_log'];
        }
        else
        {
            $show_in_log="";
        }
        $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';
        
        if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))
        {
            $this->_enabled = FALSE;
        }
        if (is_array($show_in_log))
        {
            $this->_logging_array = $show_in_log;
        }
        if (is_numeric($config['log_threshold']))
        {
            $this->_threshold = $config['log_threshold'];
        }    
        if ($config['log_date_format'] != '')
        {
            $this->_date_fmt = $config['log_date_format'];
        }
    }
    // --------------------------------------------------------------------
    /**
     * Write Log File
     *
     * Generally this function will be called using the global log_message() function
     *
     * @access    public
     * @param    string    the error level
     * @param    string    the error message
     * @param    bool    whether the error is a native PHP error
     * @return    bool
     */        
    function write_log($level = 'error', $msg, $php_error = FALSE)
    {        
        if ($this->_enabled === FALSE)
        {
            return FALSE;
        }
        $level = strtoupper($level);
        
        if (isset($this->_logging_array))
        {
            if ((! in_array($level, $this->_logging_array)) && (! empty($this->_logging_array)))
            {
                return FALSE;
            }
        }
        else
        {
            if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
            {
                return FALSE;
            }
        }

        $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
        $message  = '';
        
        if ( ! file_exists($filepath))
        {
            $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
        }
            
        if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
        {
            return FALSE;
        }
        $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --&gt; '.$msg."\n";
        
        flock($fp, LOCK_EX);    
        fwrite($fp, $message);
        flock($fp, LOCK_UN);
        fclose($fp);
    
        @chmod($filepath, FILE_WRITE_MODE);        
        return TRUE;
    }

}

And add this to your config:
Code:
/*
|--------------------------------------------------------------------------
| CUSTOM Error Logging
|--------------------------------------------------------------------------
| This will display error log levels contained in the array below
| Leaving this array blank will log ALL levels
| Deleting it entirely while MY_Log library is active will default to the
| standard log threshold config item
| 'ERROR','DEBUG','INFO' are the system defaults.
*/
$config['show_in_log']= array('DEBUG','ERROR','INFO');
#2

[eluser]farrelley[/eluser]
I just modified the Logging Lib. you can see it here http://www.kapustabrothers.com/2008/06/0...g-library/
#3

[eluser]Chris Newton[/eluser]
It's not good form to hack the core libraries directly. That makes your code more difficult to port, and makes the CI install more difficult to update quickly. Overriding a core library with your own in the form of MY_Whatever_library will help you in the long run. Where possible you should extend the core libraries or write your own that can augment without interfering.
#4

[eluser]SneakyDave[/eluser]
This class doesn't quite work for me, unless I'm calling the method incorrectly.

I'm able to load this new library by autoloading it as 'MY_log', this is confirmed by some echo statements that the custom library has been loaded.

But if I use this method to try to log something
Code:
log_message ('INFO','msg');

It appears the native method is called, and these other guesses didn't work either...
Code:
$this->MY_Log->log_message ('INFO','msg') // Message: Undefined property: ::$MY_Log
Code:
$this->log_message ('INFO','msg') // blank page
Code:
$this->Log->log_message ('INFO','msg') // Message: Undefined property: ::$Log
Code:
$this->log->log_message ('INFO','msg') // Message: Undefined property: ::$log

So I don't know how the method is supposed to be called. I'm running CI 1.6.3
#5

[eluser]SneakyDave[/eluser]
Sonofa... never mind, I got it working, I renamed MY_log.php to MY_Log.php and loaded the correct class, and it works now.
#6

[eluser]sophistry[/eluser]
I just made a similar MY_Log.php file and I think it is less code to maintain should the CI Log.php library change substantially.

The class proposed above is fine and it works, but it was too much STUFF to change and adding a config item was not to my taste.

Here is an alternative that just uses the MY_Log.php class extension to override the $_levels var. To use it, you just create your own array items. In my case, I needed to log search items in a text file.

Config file needs log_threshold set to 1:
Code:
$config['log_threshold'] = 1;

So, my log_message() call looks like this:
Code:
log_message('SEARCH', $search_term);

and the MY_log.php file...
Code:
&lt;?php

// override the $_levels array to allow custom levels

class MY_Log extends CI_Log {

    var $_levels    = array('SEARCH' => '1', 'ERROR' => '2', 'DEBUG' => '3',  'INFO' => '4', 'ALL' => '5');
    
    function MY_Log()
    {
        parent::CI_Log();
    }
    
}

/* end of file MY_Log.php */




Theme © iAndrew 2016 - Forum software by © MyBB