/
PDOLogger.php
67 lines (58 loc) · 1.9 KB
/
PDOLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Chevron\Loggers;
use \Psr\Log\AbstractLogger;
use \Chevron\DB\Interfaces\PDOWrapperInterface;
/**
* implement the Psr3 Logger mixed with mysql via Chevron\DB
* @package Chevron\Logger
*/
class PDOLogger extends AbstractLogger {
/**
* The db object
*/
protected $dbConn;
/**
* The table in which to store logs
*/
protected $table;
/**
* method to create a logger object tied to a DB connection and table
* @param PDOWrapperInterface $dbConn The db connection
* @param string $table The table name
* @return
*/
public function __construct( PDOWrapperInterface $dbConn, $table = "logging" ){
$this->dbConn = $dbConn;
$this->table = $table;
}
/**
* method to write data to the log -- as of now the default table contains only
* room for an MD5 hash for the message. The table can be modded to hold anything
* for the message but that might affect the column type for `message`
* @param string $level The level of the log message -- use Psr3 constants
* @param string $message A brief message
* @param array $context An array of additional information to store with the log
* @return
*/
public function log( $level, $message, array $context = array() ) {
$this->dbConn->insert($this->table, array(
"created" => array(true, "NOW()"),
"level" => $level,
"message" => $message,
"context" => serialize($context),
));
}
}
/*
CREATE TABLE `logging` (
`logging_id` int(11) NOT NULL AUTO_INCREMENT,
`created` datetime DEFAULT NULL,
`level` enum('emergency','alert','critical','error','warning','notice','info','debug') DEFAULT 'info',
`message` char(24) DEFAULT NULL,
`context` mediumblob DEFAULT NULL,
PRIMARY KEY (`logging_id`),
INDEX `level` USING BTREE (`level`) comment '',
INDEX `message` USING BTREE (`message`) comment '',
INDEX `created` USING BTREE (`created`) comment ''
) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
*/