Log

Use Log class to do some quick logging into file, database or anything you want. Or, you can log to all Log drivers at once. Yes, you're not limited at any point.

Why would you like to log something on several places?

The answer depends about your requirements and needs.

Example: When using CLI script manually. You might want to dump all log messages into console and you want all that log messages to have in the files. Yes, you could say that the same thing can be achived in Linux just by passing script output to the file, but what if you want to see in console only error and exception messages, but in files, you want to have all of it, including the notice and info? This framework supports that.

Currently available writers are:

File: \Koldy\Log\Writer\File
Out: \Koldy\Log\Writer\Out
Database: \Koldy\Log\Writer\Db

Configuration

Log configuration in located in configs/application.php under log section. Log section contains three keys:

enabled - boolean; should application write to log at all or not

writer_class - string; the class name of the writer

options - array; this can be array of anything. Structure depends about the writer_class


'log' => array(
    
'enabled' => true,
    
'writer_class' => '\Koldy\Log\Writer\File',
    
'options' => array( // this is constructor of \Koldy\Log\Writer\File
        
'path' => null// it goes to [your-app]/storage/log by default
        
'log' => array('debug''notice''info''warning''error''sql''exception'),
        
'email_on' => array('warning''error''exception'),
        
'email' => 'admin@encipherd.net',
        
'dump' => array('speed')
    )
)

'log' => array(
    
'enabled' => true,
    
'writer_class' => '\YourApp\CustomLogWriter',
    
'options' => array(
        
// your custom configuration set is passed to writer's constructor
        
'key1' => 'value1',
        
'key2' => 'value2',
        
'key3' => 'value3'
    
)
)

Using log

Koldy offers few types of log messages (ordered by priority, the first has the lowest priority, the last one has the greatest priority): debug, notice, sql, info, warning, error and exception.


Log::debug('Just checking this parameter: ' $yourParam);
Log::notice('File was deleted...');
Log::sql('SELECT * FROM users');
Log::info('User logged in');
Log::warning('File doesn\'t exist, but its ok');
Log::error('Something very bad happend! We can not continue');
Log::exception($e); // requires \Exception instance as parameter

Making your own log writer

Lets say you want to name your custom log writer \Site\CustomLogWriter. In that case, create directory application/library/Site and create CustomLogWriter.php in that directory. Then define namespace Site, name your class CustomLogWriter and you must extend \Koldy\Log\Writer\AbstractLogWriter. Implement all abstract methods and thats it. Don't forget to update your configs/application.php to use your \Site\CustomLogWriter.

If you want to replace or add some functionality to existing log writers, then you can just one of the writers in \Koldy\Log\Writer\*.

Using Log for logging on different locations

Using Log class can log the same message on several locations, but what if you want to, for example, separate log file and write some informations to one log file and other informations to other log file?

In that case, you'll have to initialize log writer by yourself and you'll need to keep the reference to your initialized writer. Example:


$log1 = new \Koldy\Log\Writer\File(array(
    
'path' => '/tmp/first-type-of-logs',
    
'log' => array('debug''notice''info''warning''error''sql''exception')
));

$log2 = new \Koldy\Log\Writer\File(array(
    
'path' => '/tmp/second-type-of-logs',
    
'log' => array('debug''notice''info''warning''error''sql''exception')
));

$log1->debug('This message will end up in /tmp/first-type-of-logs');
$log2->debug('This message will end up in /tmp/second-type-of-logs');
CacheMail