# DbTarget

> DbTarget stores log messages in a database table.

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

| class | `yii\log\DbTarget` |
| --- | --- |
| 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/DbTarget.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/log/DbTarget.php#L32) |

DbTarget stores log messages in a database table.

The database connection is specified by [`$db`](/docs/api/yii/log/dbtarget/index.md#property-db). Database schema could be initialized by applying migration:

```
yii migrate --migrationPath=@yii/log/migrations/
```

If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.

You may change the name of the table used to store the data by setting [`$logTable`](/docs/api/yii/log/dbtarget/index.md#property-logTable).

## 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) |
| [`$db`](#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection. Default: `'db'`. | `DbTarget` |
| <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) |
| <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) |
| <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) |
| [`$logTable`](#property-logTable) | `string` | Name of the DB table to store cache content. Default: `'{{%log}}'`. | `DbTarget` |
| <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) |
| <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) | Stores log messages to DB. | `DbTarget` |
| <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 DbTarget component. | `DbTarget` |
| <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) |

## Property details

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

```php
public yii\db\Connection|array|string $db = 'db'
```

The DB connection object or the application component ID of the DB connection.

After the DbTarget object is created, if you want to change this property, you should only assign it
with a DB connection object.
Starting from version 2.0.2, this can also be a configuration array for creating the object.

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

```php
public string $logTable = '{{%log}}'
```

Name of the DB table to store cache content.

Defaults to "log".

## Method details

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

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

Stores log messages to DB.

Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.

**Returns**

`mixed`

**Throws**

- [`yii\db\Exception`](/docs/api/yii/db/exception/index.md)
- [`LogRuntimeException`](/docs/api/yii/log/logruntimeexception/index.md)

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

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

Initializes the DbTarget component.

This method will initialize the [`$db`](/docs/api/yii/log/dbtarget/index.md#property-db) property to make sure it refers to a valid DB connection.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if [`$db`](/docs/api/yii/log/dbtarget/index.md#property-db) is invalid.