SQL Adapter
This adapter is for logging to a database. The table you log to should have columns for level
, timestamp
, and message
.
<?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
.
<?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);
Retrieving Entries
By default, it will return all entries ordered in ascending order (first in, first out).
<?php
// Get all entries
$entries = $log->getAdapter()->getEntries();
// Get only 10 entries
$entries = $log->getAdapter()->getEntries(10);
// Get 10 entries starting on page 2
$entries = $log->getAdapter()->getEntries(10, 2);
// Get all entries, but order as you want
$order_by = [
'timestamp DESC',
'id DESC'
];
$entries = $log->getAdapter()->getEntries(0, 0, $order_by);
// Get a count of all entries
$count = $log->getAdapter()->getCount();
You can pair this nicely with Vespula.Paginator