W3cubDocs

/Yii 1.1

CLogger

Package system.logging
Inheritance class CLogger » CComponent
Since 1.0
Source Code framework/logging/CLogger.php
CLogger records log messages in memory.

CLogger implements the methods to retrieve the messages with various filter conditions, including log levels and log categories.

Public Properties

Property Type Description Defined By
autoDump boolean this property will be passed as the parameter to flush() when it is called in log() due to the limit of autoFlush being reached. CLogger
autoFlush integer how many messages should be logged before they are flushed to destinations. CLogger
executionTime float Returns the total time for serving the current request. CLogger
logs array Retrieves log messages. CLogger
memoryUsage integer Returns the memory usage of the current application. CLogger
profilingResults array Returns the profiling results. CLogger

Public Methods

Method Description Defined By
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
flush() Removes all recorded messages from the memory. CLogger
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getExecutionTime() Returns the total time for serving the current request. CLogger
getLogs() Retrieves log messages. CLogger
getMemoryUsage() Returns the memory usage of the current application. CLogger
getProfilingResults() Returns the profiling results. CLogger
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
log() Logs a message. CLogger
onFlush() Raises an onFlush event. CLogger
raiseEvent() Raises an event. CComponent

Events

Event Description Defined By
onFlush Raises an onFlush event. CLogger

Property Details

autoDump property (available since v1.1.8)

public boolean $autoDump;

this property will be passed as the parameter to flush() when it is called in log() due to the limit of autoFlush being reached. By default, this property is false, meaning the filtered messages are still kept in the memory by each log route after calling flush(). If this is true, the filtered messages will be written to the actual medium each time flush() is called within log().

autoFlush property (available since v1.1.0)

public integer $autoFlush;

how many messages should be logged before they are flushed to destinations. Defaults to 10,000, meaning for every 10,000 messages, the flush method will be automatically invoked once. If this is 0, it means messages will never be flushed automatically.

executionTime property read-only

public float getExecutionTime()

Returns the total time for serving the current request. This method calculates the difference between now and the timestamp defined by constant YII_BEGIN_TIME. To estimate the execution time more accurately, the constant should be defined as early as possible (best at the beginning of the entry script.)

logs property read-only

public array getLogs(string $levels='', array|string $categories=array ( ), array|string $except=array ( ))

Retrieves log messages.

Messages may be filtered by log levels and/or categories. A level filter is specified by a list of levels separated by comma or space (e.g. 'trace, error'). A category filter is similar to level filter (e.g. 'system, system.web'). A difference is that in category filter you can use pattern like 'system.*' to indicate all categories starting with 'system'.

If you do not specify level filter, it will bring back logs at all levels. The same applies to category filter.

Level filter and category filter are combinational, i.e., only messages satisfying both filter conditions will be returned.

memoryUsage property read-only

public integer getMemoryUsage()

Returns the memory usage of the current application. This method relies on the PHP function memory_get_usage(). If it is not available, the method will attempt to use OS programs to determine the memory usage. A value 0 will be returned if the memory usage can still not be determined.

profilingResults property read-only

public array getProfilingResults(string $token=NULL, string $categories=NULL, boolean $refresh=false)

Returns the profiling results. The results may be filtered by token and/or category. If no filter is specified, the returned results would be an array with each element being array($token,$category,$time). If a filter is specified, the results would be an array of timings.

Since 1.1.11, filtering results by category supports the same format used for filtering logs in getLogs, and similarly supports filtering by multiple categories and wildcard.

Method Details

flush() method (available since v1.1.0)

public void flush(boolean $dumpLogs=false)
$dumpLogs boolean whether to process the logs immediately as they are passed to log route
Source Code: framework/logging/CLogger.php#338 (show)
public function flush($dumpLogs=false)
{
    
$this->onFlush(new CEvent($this, array('dumpLogs'=>$dumpLogs)));
    
$this->_logs=array();
    
$this->_logCount=0;
}

Removes all recorded messages from the memory. This method will raise an onFlush event. The attached event handlers can process the log messages before they are removed.

getExecutionTime() method

public float getExecutionTime()
{return} float the total time for serving the current request.
Source Code: framework/logging/CLogger.php#222 (show)
public function getExecutionTime()
{
    return 
microtime(true)-YII_BEGIN_TIME;
}

Returns the total time for serving the current request. This method calculates the difference between now and the timestamp defined by constant YII_BEGIN_TIME. To estimate the execution time more accurately, the constant should be defined as early as possible (best at the beginning of the entry script.)

getLogs() method

public array getLogs(string $levels='', array|string $categories=array ( ), array|string $except=array ( ))
$levels string level filter
$categories array|string category filter
$except array|string list of log categories to ignore
{return} array list of messages. Each array element represents one message with the following structure: array( [0] => message (string) [1] => level (string) [2] => category (string) [3] => timestamp (float, obtained by microtime(true));
Source Code: framework/logging/CLogger.php#133 (show)
public function getLogs($levels='',$categories=array(), $except=array())
{
    
$this->_levels=preg_split('/[\s,]+/',strtolower($levels),-1,PREG_SPLIT_NO_EMPTY);

    if (
is_string($categories))
        
$this->_categories=preg_split('/[\s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
    else
        
$this->_categories=array_filter(array_map('strtolower',$categories));

    if (
is_string($except))
        
$this->_except=preg_split('/[\s,]+/',strtolower($except),-1,PREG_SPLIT_NO_EMPTY);
    else
        
$this->_except=array_filter(array_map('strtolower',$except));

    
$ret=$this->_logs;

    if(!empty(
$levels))
        
$ret=array_values(array_filter($ret,array($this,'filterByLevel')));

    if(!empty(
$this->_categories) || !empty($this->_except))
        
$ret=array_values(array_filter($ret,array($this,'filterByCategory')));

    return 
$ret;
}

Retrieves log messages.

Messages may be filtered by log levels and/or categories. A level filter is specified by a list of levels separated by comma or space (e.g. 'trace, error'). A category filter is similar to level filter (e.g. 'system, system.web'). A difference is that in category filter you can use pattern like 'system.*' to indicate all categories starting with 'system'.

If you do not specify level filter, it will bring back logs at all levels. The same applies to category filter.

Level filter and category filter are combinational, i.e., only messages satisfying both filter conditions will be returned.

getMemoryUsage() method

public integer getMemoryUsage()
{return} integer memory usage of the application (in bytes).
Source Code: framework/logging/CLogger.php#235 (show)
public function getMemoryUsage()
{
    if(
function_exists('memory_get_usage'))
        return 
memory_get_usage();
    else
    {
        
$output=array();
        if(
strncmp(PHP_OS,'WIN',3)===0)
        {
            
exec('tasklist /FI "PID eq ' getmypid() . '" /FO LIST',$output);
            return isset(
$output[5])?preg_replace('/[\D]/','',$output[5])*1024 0;
        }
        else
        {
            
$pid=getmypid();
            
exec("ps -eo%mem,rss,pid | grep $pid"$output);
            
$output=explode("  ",$output[0]);
            return isset(
$output[1]) ? $output[1]*1024 0;
        }
    }
}

Returns the memory usage of the current application. This method relies on the PHP function memory_get_usage(). If it is not available, the method will attempt to use OS programs to determine the memory usage. A value 0 will be returned if the memory usage can still not be determined.

getProfilingResults() method

public array getProfilingResults(string $token=NULL, string $categories=NULL, boolean $refresh=false)
$token string token filter. Defaults to null, meaning not filtered by token.
$categories string category filter. Defaults to null, meaning not filtered by category.
$refresh boolean whether to refresh the internal timing calculations. If false, only the first time calling this method will the timings be calculated internally.
{return} array the profiling results.
Source Code: framework/logging/CLogger.php#272 (show)
public function getProfilingResults($token=null,$categories=null,$refresh=false)
{
    if(
$this->_timings===null || $refresh)
        
$this->calculateTimings();
    if(
$token===null && $categories===null)
        return 
$this->_timings;

    
$timings $this->_timings;
    if(
$categories!==null) {
        
$this->_categories=preg_split('/[\s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
        
$timings=array_filter($timings,array($this,'filterTimingByCategory'));
    }

    
$results=array();
    foreach(
$timings as $timing)
    {
        if(
$token===null || $timing[0]===$token)
            
$results[]=$timing[2];
    }
    return 
$results;
}

Returns the profiling results. The results may be filtered by token and/or category. If no filter is specified, the returned results would be an array with each element being array($token,$category,$time). If a filter is specified, the results would be an array of timings.

Since 1.1.11, filtering results by category supports the same format used for filtering logs in getLogs, and similarly supports filtering by multiple categories and wildcard.

log() method

public void log(string $message, string $level='info', string $category='application')
$message string message to be logged
$level string level of the message (e.g. 'Trace', 'Warning', 'Error'). It is case-insensitive.
$category string category of the message (e.g. 'system.web'). It is case-insensitive.
Source Code: framework/logging/CLogger.php#94 (show)
public function log($message,$level='info',$category='application')
{
    
$this->_logs[]=array($message,$level,$category,microtime(true));
    
$this->_logCount++;
    if(
$this->autoFlush>&& $this->_logCount>=$this->autoFlush && !$this->_processing)
    {
        
$this->_processing=true;
        
$this->flush($this->autoDump);
        
$this->_processing=false;
    }
}

Logs a message. Messages logged by this method may be retrieved back via getLogs.

See Also

onFlush() method (available since v1.1.0)

public void onFlush(CEvent $event)
$event CEvent the event parameter
Source Code: framework/logging/CLogger.php#350 (show)
public function onFlush($event)
{
    
$this->raiseEvent('onFlush'$event);
}

Raises an onFlush event.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CLogger