DbTarget
API reference › yii › log › DbTarget
DbTarget stores log messages in a database table.
The database connection is specified by $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.
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 |
$db | yii\| | The DB connection object or the application component ID of the DB connection. Default: 'db'. | DbTarget |
$enabled | bool| | A boolean value or a callable to obtain the value from. | Target |
$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 |
$levels | array| | Message levels that this target is interested in. | Target |
$logTable | string | Name of the DB table to store cache content. Default: '{{%log}}'. | DbTarget |
$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 |
$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() | Stores log messages to DB. | DbTarget |
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 DbTarget component. | DbTarget |
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 |
Property details
Section titled “Property details”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
Section titled “$logTable ”public string $logTable = '{{%log}}'Name of the DB table to store cache content.
Defaults to “log”.
Method details
Section titled “Method details”export()
Section titled “export() ”public function export(): mixedStores log messages to DB.
Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
init()
Section titled “init() ”public function init(): mixedInitializes the DbTarget component.
This method will initialize the $db property to make sure it refers to a valid DB connection.