-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-mysql-53.php
58 lines (51 loc) · 1.83 KB
/
example-mysql-53.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
<?php
error_reporting(-1);
ini_set('display_errors', true);
//settings - library-related
$log = getenv('LOG') ?: '/tmp/php_error.log';
$logTemporaryDir = getenv('LOG_TEMPORARY_DIR') ?: '/tmp/php_error';
$logKeepDir = getenv('LOG_KEEP_DIR') ?: '/tmp/php_error_keep';
//settings - custom-code related
$host = getenv('MYSQL_HOST') ?: 'localhost';
$port = getenv('MYSQL_PORT') ?: 3306;
$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASSWORD') ?: '';
$database = getenv('MYSQL_DATABASE') ?: 'test';
$table = getenv('MYSQL_TABLE') ?: 'php_error';
$extraColumns = array();
if (getenv('EXTRA_COLUMNS')) {
foreach(explode(',', getenv('EXTRA_COLUMNS')) as $tmp) {
list ($k, $v) = explode('=', $tmp);
$extraColumns[trim($k)] = trim($v);
}
}
//db connect
$dsn = "mysql:dbname={$database};host={$host};port={$port}";
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//prepare insert query
$sqlColumns = array('datetime = :datetime', 'message = :message');
foreach (array_keys($extraColumns) as $column) {
$sqlColumns[] = "{$column} = :{$column}";
}
$sql = "INSERT INTO {$table} SET " . join(', ', $sqlColumns);
$sth = $dbh->prepare($sql);
foreach (array_keys($extraColumns) as $column) {
$sth->bindParam(':' . $column, $extraColumns[$column]);
}
$datetime = $message = null;
$sth->bindParam(':datetime', $datetime);
$sth->bindParam(':message', $message);
//callback function
$sendErrors = function(array $errors) use ($sth, &$datetime, &$message) {
foreach ($errors as $error) {
list ($timestamp, $message) = $error;
$datetime = date('Y-m-d H:i:s', $timestamp);
$sth->execute();
}
return true;
};
//run
require 'lib-53.php';
$phpLogParser53 = new PhpLogParser53($log, $logTemporaryDir, $sendErrors, 2, $logKeepDir);
$phpLogParser53->start();