FileTarget
API reference › yii › log › FileTarget
FileTarget records log messages in a file.
The log file is specified via $logFile. If the size of the log file exceeds
$maxFileSize (in kilo-bytes), a rotation will be performed, which renames
the current log file by suffixing the file name with ‘.1’. All existing log
files are moved backwards by one place, i.e., ‘.2’ to ‘.3’, ‘.1’ to ‘.2’, and so on.
The property $maxLogFiles specifies how many history files to keep.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$categories | list<string> | List of message categories that this target is interested in. Default: []. | Target |
$dirMode | int | The permission to be set for newly created directories. Default: 0775. | FileTarget |
$enabled | bool| | A boolean value or a callable to obtain the value from. | Target |
$enableRotation | bool | Whether log files should be rotated when they reach a certain maximum size. Default: true. Available since 2.0.3. | FileTarget |
$except | list<string> | List of message categories that this target is NOT interested in. Default: []. | Target |
$exportInterval | int | How many messages should be accumulated before they are exported. Default: 1000. | Target |
$fileMode | int| | The permission to be set for newly created log files. Default: null. | FileTarget |
$levels | array| | Message levels that this target is interested in. | Target |
$logFile | string| | Log file path or path alias. Default: null. | FileTarget |
$logVars | list<string> | List of the PHP predefined variables that should be logged in a message. Default: ['_GET',. | Target |
$maskVars | list<string> | List of the PHP predefined variables that should NOT be logged “as is” and should always be replaced with a mask *** before logging, when exist. Default: ['_SERVER.HTTP_AUTHORIZATION',. Available since 2.0.16. | Target |
$maxFileSize | int | Maximum log file size, in kilo-bytes. Default: 10240. | FileTarget |
$maxLogFiles | int | Number of log files used for rotation. Default: 5. | FileTarget |
$messages | list<yii\ | The messages that are retrieved from the logger so far by this log target. Default: []. | Target |
$microtime | bool | Whether to log time with microseconds. Default: false. Available since 2.0.13. | Target |
$prefix | callable(yii\ | A PHP callable that returns a string to be prefixed to every exported message. Default: null. | Target |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
collect() | Processes the given log messages. | Target |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
export() | Writes log messages to a file. | FileTarget |
filterMessages() static | Filters the given messages according to their categories and levels. | Target |
formatMessage() | Formats a log message for display as a string. | Target |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getEnabled() | Check whether the log target is enabled. | Target |
getLevels() | Target | |
getMessagePrefix() | Returns a string to be prefixed to the given message. | Target |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the route. | FileTarget |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
setEnabled() | Sets a value indicating whether this log target is enabled. | Target |
setLevels() | Sets the message levels that this target is interested in. | Target |
trigger() | Triggers an event. | yii\ |
formatMessageText() protected | Converts a log message value to text. | Target |
getContextMessage() protected | Generates the context information to be logged. | Target |
getTime() protected | Returns formatted (‘Y-m-d H:i:s’) timestamp for message. | Target |
rotateFiles() protected | Rotates log files. | FileTarget |
Property details
Section titled “Property details”$dirMode
Section titled “$dirMode ”public int $dirMode = 0775The permission to be set for newly created directories.
This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
$enableRotation
Section titled “$enableRotation ”public bool $enableRotation = trueWhether log files should be rotated when they reach a certain maximum size.
Log rotation is enabled by default. This property allows you to disable it, when you have configured an external tools for log rotation on your server.
Since
2.0.3
$fileMode
Section titled “$fileMode ”public int|null $fileMode = nullThe permission to be set for newly created log files.
This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.
$logFile
Section titled “$logFile ”public string|null $logFile = nullLog file path or path alias.
If not set, it will use the “@runtime/logs/app.log” file. The directory containing the log files will be automatically created if not existing.
$maxFileSize
Section titled “$maxFileSize ”public int $maxFileSize = 10240Maximum log file size, in kilo-bytes.
Defaults to 10240, meaning 10MB.
$maxLogFiles
Section titled “$maxLogFiles ”public int $maxLogFiles = 5Number of log files used for rotation.
Defaults to 5.
Method details
Section titled “Method details”export()
Section titled “export() ”public function export(): mixedWrites log messages to a file.
Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
Returns
mixed
Throws
yii\base\InvalidConfigException: if unable to open the log file for writingLogRuntimeException: if unable to write complete log to file
init()
Section titled “init() ”public function init(): mixedInitializes the route.
This method is invoked after the route is created by the route manager.
Returns
mixed
rotateFiles()
Section titled “rotateFiles() ”protected function rotateFiles(): mixedRotates log files.
Returns
mixed