These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Custom Logging question
  • I'm trying to set up a custom log to aid in support of an ongoing project, but am running into a problem using the existing Log:: structure.

    What I'm attempting to do (simplified to very broad pseudo-code):

    if ( $user->id === 1 )
    {
        // Log all activity by user 1
        $mylog = 'application/log/users/1.php'; // Log for activity of this user
        Log::add( $mylog, 'action x occurred' );
    }
    
    // Log this activity when it occurs
    $actionlog = 'application/log/actions/action_x.php';  // Log for this activity
    Log::add( $actionlog, $user->id.' performed action x' );
    

    Any thoughts on how I can get this done?

  • class Log extends Kohana_Log 
    {
        public static function insert($location, $level, $message, array $values = NULL)
        {
            if ( ! is_dir($location))
            {
                // Create the yearly directory
                mkdir($location, 02777);
    
                // Set permissions (must be manually set to fix umask issues)
                chmod($location, 02777);
            }
    
            try 
            {
                // we add it using kohana; to have a complete log file
                Log::instance()->add($level, $message, $values);
    
                $writer = new Log_File($location);
            }
            catch (Exception $e)
            {
                die($e->getMessage());
            }
    
    
            $message = array
                (
                    'time'  => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
                    'level' => $level,
                    'body'  => $message,
                );
    
            $writer->write(array($message));
        }
    
    } # class
    

    Based on your example...

    Log::insert
        (
            APPPATH.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'users',
            Log::WARNING,
            'action x occurred'
        );
    
  • Thanks, Akki -- I'll give that a go.

    My latest bonehead moment: I should have noted in the OP that I'm working in 3.2

  • That should work in 3.2

    [edit]

    Oh wait, let me guess it's telling you the method doesn't exist when you try to overwrite? If you get that I'm afraid the easy solution is to stick that code into the APPPATH. There is a more proper solution, but the thing is there's nothing particularly wrong with the code in question but rather how kohana and kohana's bootstrap is setup which causes it to ignore any class declared in a module if the class is called in bootstrap.

    I have it fixed so it works properly for me, but it's quite a few changes.

  • Thanks again, Akki!

    Here's what I ended up with for the user-logs portion of this:

        public static function user($message='', $user=FALSE, array $values = NULL, $level=Log::INFO)
        // Rearranged args so most-used first. Different signature than Log::insert()
        {
            if (FALSE === $user || !is_object($user) || !($user instanceof Model_User) )
            {
                // Note: I'm using mixu's useradmin module @ https://github.com/mixu/useradmin, 
                // but anything that uses Auth module as a basis should work
                $user = Auth::instance()->get_user();
            }
            $location = $user->id;
            
            // Prepend $user->id with path to user logs
            $location = APPPATH.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'users'.DIRECTORY_SEPARATOR.$location;
            
            if ( ! is_dir($location))
            {
                // Create the yearly directory
                mkdir($location, 02777);
                
                // Set permissions (must be manually set to fix umask issues)
                chmod($location, 02777);
            }
            
            try
            {
                // we add it using kohana; to have a complete log file
                Log::instance()->add($level, $message, $values);
                
                // Writes to main Kohana Logs
                $writer = new Log_File($location);
            }
            catch (Exception $e)
            {
                die($e->getMessage());
            }
                
            $message = array
            (
                'time'  => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
                'level' => $level,
                'body'  => $message,
            );
            
            // Writes to custom user Logs
            // application/logs/users/$user->id/yyyy/mm/dd.php
            $writer->write(array($message));
        }
    
    

    Not the most efficient thing in the world, but it gets done what I wanted done. With this in place, I can record in user-centric logs whenever a user has performed whichever actions I want to trace:

    Log::user('Hello, World');
    

    Gives me (in application/logs/users/1/2011/11/14.php):

    2011-11-14 10:03:29 --- INFO: action x occurred
    2011-11-14 10:04:15 --- INFO: action y occurred
    2011-11-14 10:06:59 --- INFO: Hello, World
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion