Sql Adapter

This adapter is for logging to a database. The table you log to should have columns for level, timestamp, and message.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\Sql as SqlLog;

$dsn = 'mysql:dbname=mydb;host=localhost';
$pdo = new \PDO($dsn, 'juser', '********');
$table = 'log';
$sql_adapter = new SqlLog($pdo, $table);

$log = new Log($sql_adapter);

You can use the setCols() method to set the required 3 columns. You must make sure that the array keys are level, timestamp, and message.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\Sql as SqlLog;

$dsn = 'mysql:dbname=mydb;host=localhost';
$pdo = new \PDO($dsn, 'juser', '********');
$table = 'log';
$sql_adapter = new SqlLog($pdo, $table);

// In your database table, you might have columns `loglevel`, `logdate`, and `entry`.
$cols = [
    'level'=>'loglevel',
    'timestamp'=>'logdate',
    'message'=>'entry'
];

$sql_adapter->setCols($cols);

$log = new Log($sql_adapter);