# FileTarget

> FileTarget records log messages in a file.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [log](/docs/api/yii/log/index.md) › FileTarget

| class | `yii\log\FileTarget` |
| --- | --- |
| Extends | [`Target`](/docs/api/yii/log/target/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/log/FileTarget.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/log/FileTarget.php#L27) |

FileTarget records log messages in a file.

The log file is specified via [`$logFile`](/docs/api/yii/log/filetarget/index.md#property-logFile). If the size of the log file exceeds
[`$maxFileSize`](/docs/api/yii/log/filetarget/index.md#property-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`](/docs/api/yii/log/filetarget/index.md#property-maxLogFiles) specifies how many history files to keep.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-categories"></span>[`$categories`](/docs/api/yii/log/target/index.md#property-categories) | `list<string>` | List of message categories that this target is interested in. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$dirMode`](#property-dirMode) | `int` | The permission to be set for newly created directories. Default: `0775`. | `FileTarget` |
| <span id="property-enabled"></span>[`$enabled`](/docs/api/yii/log/target/index.md#property-enabled) | `bool\|callable` | A boolean value or a callable to obtain the value from. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$enableRotation`](#property-enableRotation) | `bool` | Whether log files should be rotated when they reach a certain [maximum size](/docs/api/yii/log/filetarget/index.md#property-maxFileSize). Default: `true`. Available since 2.0.3. | `FileTarget` |
| <span id="property-except"></span>[`$except`](/docs/api/yii/log/target/index.md#property-except) | `list<string>` | List of message categories that this target is NOT interested in. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-exportInterval"></span>[`$exportInterval`](/docs/api/yii/log/target/index.md#property-exportInterval) | `int` | How many messages should be accumulated before they are exported. Default: `1000`. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$fileMode`](#property-fileMode) | `int\|null` | The permission to be set for newly created log files. Default: `null`. | `FileTarget` |
| <span id="property-levels"></span>[`$levels`](/docs/api/yii/log/target/index.md#property-levels) | `array\|int` | Message levels that this target is interested in. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$logFile`](#property-logFile) | `string\|null` | Log file path or [path alias](/docs/guide/concept-aliases/index.md). Default: `null`. | `FileTarget` |
| <span id="property-logVars"></span>[`$logVars`](/docs/api/yii/log/target/index.md#property-logVars) | `list<string>` | List of the PHP predefined variables that should be logged in a message. Default: `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-maskVars"></span>[`$maskVars`](/docs/api/yii/log/target/index.md#property-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', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']`. Available since 2.0.16. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`$maxFileSize`](#property-maxFileSize) | `int` | Maximum log file size, in kilo-bytes. Default: `10240`. | `FileTarget` |
| [`$maxLogFiles`](#property-maxLogFiles) | `int` | Number of log files used for rotation. Default: `5`. | `FileTarget` |
| <span id="property-messages"></span>[`$messages`](/docs/api/yii/log/target/index.md#property-messages) | `list<yii\log\LogMessage>` | The messages that are retrieved from the logger so far by this log target. Default: `[]`. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-microtime"></span>[`$microtime`](/docs/api/yii/log/target/index.md#property-microtime) | `bool` | Whether to log time with microseconds. Default: `false`. Available since 2.0.13. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="property-prefix"></span>[`$prefix`](/docs/api/yii/log/target/index.md#property-prefix) | `callable(yii\log\LogMessage): string\|null` | A PHP callable that returns a string to be prefixed to every exported message. Default: `null`. | [`Target`](/docs/api/yii/log/target/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-collect"></span>[`collect()`](/docs/api/yii/log/target/index.md#method-collect) | Processes the given log messages. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`export()`](#method-export) | Writes log messages to a file. | `FileTarget` |
| <span id="method-filterMessages"></span>[`filterMessages()`](/docs/api/yii/log/target/index.md#method-filterMessages) static | Filters the given messages according to their categories and levels. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-formatMessage"></span>[`formatMessage()`](/docs/api/yii/log/target/index.md#method-formatMessage) | Formats a log message for display as a string. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getEnabled"></span>[`getEnabled()`](/docs/api/yii/log/target/index.md#method-getEnabled) | Check whether the log target is enabled. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getLevels"></span>[`getLevels()`](/docs/api/yii/log/target/index.md#method-getLevels) |  | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getMessagePrefix"></span>[`getMessagePrefix()`](/docs/api/yii/log/target/index.md#method-getMessagePrefix) | Returns a string to be prefixed to the given message. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the route. | `FileTarget` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-setEnabled"></span>[`setEnabled()`](/docs/api/yii/log/target/index.md#method-setEnabled) | Sets a value indicating whether this log target is enabled. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-setLevels"></span>[`setLevels()`](/docs/api/yii/log/target/index.md#method-setLevels) | Sets the message levels that this target is interested in. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-formatMessageText"></span>[`formatMessageText()`](/docs/api/yii/log/target/index.md#method-formatMessageText) protected | Converts a log message value to text. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getContextMessage"></span>[`getContextMessage()`](/docs/api/yii/log/target/index.md#method-getContextMessage) protected | Generates the context information to be logged. | [`Target`](/docs/api/yii/log/target/index.md) |
| <span id="method-getTime"></span>[`getTime()`](/docs/api/yii/log/target/index.md#method-getTime) protected | Returns formatted ('Y-m-d H:i:s') timestamp for message. | [`Target`](/docs/api/yii/log/target/index.md) |
| [`rotateFiles()`](#method-rotateFiles) protected | Rotates log files. | `FileTarget` |

## Property details

### $dirMode <span id="property-dirMode"></span>

```php
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.

### $enableRotation <span id="property-enableRotation"></span>

```php
public bool $enableRotation = true
```

Whether log files should be rotated when they reach a certain [maximum size](/docs/api/yii/log/filetarget/index.md#property-maxFileSize).

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 <span id="property-fileMode"></span>

```php
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.

### $logFile <span id="property-logFile"></span>

```php
public string|null $logFile = null
```

Log file path or [path alias](/docs/guide/concept-aliases/index.md).

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 <span id="property-maxFileSize"></span>

```php
public int $maxFileSize = 10240
```

Maximum log file size, in kilo-bytes.

Defaults to 10240, meaning 10MB.

### $maxLogFiles <span id="property-maxLogFiles"></span>

```php
public int $maxLogFiles = 5
```

Number of log files used for rotation.

Defaults to 5.

## Method details

### export() <span id="method-export"></span>

```php
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**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if unable to open the log file for writing
- [`LogRuntimeException`](/docs/api/yii/log/logruntimeexception/index.md): if unable to write complete log to file

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the route.

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

**Returns**

`mixed`

### rotateFiles() <span id="method-rotateFiles"></span>

```php
protected function rotateFiles(): mixed
```

Rotates log files.

**Returns**

`mixed`