Skip to content
PHP Ecosystem
Explore

FileTarget

API reference › yii › log › FileTarget

classyii\log\FileTarget
ExtendsTarget » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/log/FileTarget.php

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.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$categorieslist<string>List of message categories that this target is interested in. Default: [].Target
$dirModeintThe permission to be set for newly created directories. Default: 0775.FileTarget
$enabledbool|callableA boolean value or a callable to obtain the value from.Target
$enableRotationboolWhether log files should be rotated when they reach a certain maximum size. Default: true. Available since 2.0.3.FileTarget
$exceptlist<string>List of message categories that this target is NOT interested in. Default: [].Target
$exportIntervalintHow many messages should be accumulated before they are exported. Default: 1000.Target
$fileModeint|nullThe permission to be set for newly created log files. Default: null.FileTarget
$levelsarray|intMessage levels that this target is interested in.Target
$logFilestring|nullLog file path or path alias. Default: null.FileTarget
$logVarslist<string>List of the PHP predefined variables that should be logged in a message. Default: ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'].Target
$maskVarslist<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', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']. Available since 2.0.16.Target
$maxFileSizeintMaximum log file size, in kilo-bytes. Default: 10240.FileTarget
$maxLogFilesintNumber of log files used for rotation. Default: 5.FileTarget
$messageslist<yii\log\LogMessage>The messages that are retrieved from the logger so far by this log target. Default: [].Target
$microtimeboolWhether to log time with microseconds. Default: false. Available since 2.0.13.Target
$prefixcallable(yii\log\LogMessage): string|nullA PHP callable that returns a string to be prefixed to every exported message. Default: null.Target
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
collect()Processes the given log messages.Target
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
export()Writes log messages to a file.FileTarget
filterMessages() staticFilters 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\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
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\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the route.FileTarget
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
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\base\Component
formatMessageText() protectedConverts a log message value to text.Target
getContextMessage() protectedGenerates the context information to be logged.Target
getTime() protectedReturns formatted (‘Y-m-d H:i:s’) timestamp for message.Target
rotateFiles() protectedRotates log files.FileTarget
public int $dirMode = 0775

The 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.

public bool $enableRotation = true

Whether 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

public int|null $fileMode = null

The 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.

public string|null $logFile = null

Log 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.

public int $maxFileSize = 10240

Maximum log file size, in kilo-bytes.

Defaults to 10240, meaning 10MB.

public int $maxLogFiles = 5

Number of log files used for rotation.

Defaults to 5.

public function export(): mixed

Writes 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

public function init(): mixed

Initializes the route.

This method is invoked after the route is created by the route manager.

Returns

mixed

protected function rotateFiles(): mixed

Rotates log files.

Returns

mixed