-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.class.php
More file actions
85 lines (70 loc) · 2.17 KB
/
Logger.class.php
File metadata and controls
85 lines (70 loc) · 2.17 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Log runtime messages to the database.
* Written by Bradley Gill
* altered effect (http://alteredeffect.com)
* bradgill@gmail.com
*
* Usage:
* Logger::$database = $database; // Tell it where to save
* Logger::log( 'hi there' );
*
*/
class Logger {
public static $database = null;
/**
* Log a string to the database.
* @param $string to log.
*/
public static function log( $string ) {
if ( Logger::$_instance == null )
Logger::$_instance = new Logger();
// log the string
Logger::$_instance->_log( $string );
}
// Nothing to see down here!
private static $_instance = null;
private $id;
// Constructor
private function __construct() {
global $config;
// log the start
$query = 'INSERT INTO `'.$config['database']['tables']['logger'].'` (`started`, `modified_at`) VALUES (NOW(), NOW())';
try {
$this::$database->query( $query );
$this->id = $this::$database->lastInsertId();
} catch (PDOException $e) {
if ( $this::$database->errorCode() == '42S02' ) {
$this::$database->query( 'CREATE TABLE `'.$config['database']['tables']['logger'].'` (
`started` DATETIME NOT NULL ,
`modified_at` DATETIME NOT NULL ,
`complete` TINYINT NOT NULL ,
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`log` TEXT NOT NULL
) ENGINE = MYISAM ;');
try {
$this::$database->query( $query );
$this->id = $this::$database->lastInsertId();
} catch (PDOException $e) {
Controller::error( 503, 'Could not start log' );
}
} else
Controller::error( 503, 'Could not start log' );
}
}
// Log the string
private function _log( $string ) {
global $config;
// Log the string
$stmt = $this::$database->prepare('UPDATE `'.$config['database']['tables']['logger'].'` SET `log`=CONCAT(`log`, :msg), `modified_at`=NOW() WHERE id=:id' );
$stmt->execute( array( 'msg'=>$string, 'id'=>$this->id ) );
}
// Close the log
public function __destruct() {
global $config;
// Log the end
$stmt = $this::$database->prepare('UPDATE `'.$config['database']['tables']['logger'].'` SET `complete`=1, `modified_at`=NOW() WHERE id=:id' );
$stmt->execute( array( 'id'=>$this->id ) );
}
}
?>