Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit 0ddc620

Browse files
committed
switch to keep logs instead of removing permanently
1 parent 03d42b3 commit 0ddc620

5 files changed

Lines changed: 58 additions & 39 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
php_error.log
2+
.idea

example-53.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
error_reporting(-1);
33
ini_set('display_errors', true);
44

5-
$log = __DIR__ . '/php_error.log';
5+
$log = '/tmp/php_error.log';
66
$logTemporaryDir = '/tmp/php_error';
7+
$keepLogsDir = '/tmp/php_error_keep';
78

89
//callback function
910
$sendErrors = function(array $errors) {
@@ -13,5 +14,5 @@
1314

1415
//run
1516
require 'lib-53.php';
16-
$a = new PhpLogParser53($log, $logTemporaryDir, $sendErrors, 1);
17+
$a = new PhpLogParser53($log, $logTemporaryDir, $sendErrors, 1, $keepLogsDir);
1718
$a->start();

example-mysql-53.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//get env
66
$log = getenv('LOG') ?: '/tmp/php_error.log';
77
$logTemporaryDir = getenv('LOG_TEMPORARY_DIR') ?: '/tmp/php_error';
8+
$keepLogDir = getenv('KEEP_LOG_DIR') ?: '/tmp/php_error_keep';
9+
810
$host = getenv('MYSQL_HOST') ?: 'localhost';
911
$port = getenv('MYSQL_PORT') ?: 3306;
1012
$user = getenv('MYSQL_USER') ?: 'root';
@@ -55,5 +57,5 @@
5557

5658
//run
5759
require 'lib-53.php';
58-
$a = new PhpLogParser53($log, $logTemporaryDir, $sendErrors, 1);
60+
$a = new PhpLogParser53($log, $logTemporaryDir, $sendErrors, 1, $keepLogDir);
5961
$a->start();

generateErrors.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
error_reporting(-1);
33
ini_set('display_errors', true);
4-
ini_set('error_log', __DIR__ . '/php_error.log');
4+
ini_set('error_log', '/tmp/php_error.log');
55
if (function_exists('xdebug_disable')) {
66
xdebug_disable();
77
}
@@ -12,6 +12,5 @@
1212
}
1313

1414
//generate errors
15-
for ($i = 0; $i < 2; $i ++) {
16-
echo $a;
17-
}
15+
echo $a;
16+
throw new Exception;

lib-53.php

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,33 @@ public function __construct(
55
$logPath,
66
$temporaryDir,
77
\Closure $deliverErrorsCallback,
8-
$pollDelay = 1
8+
$pollDelay = 1,
9+
$keepLogDir = null
910
) {
1011
$this->logPath = $logPath;
1112
$this->temporaryDir = $temporaryDir;
1213
$this->pollDelay = $pollDelay * 1000000; //to milliseconds
1314
$this->deliverErrorsCallback = $deliverErrorsCallback;
15+
$this->keepLogDir = $keepLogDir;
1416
}
1517

1618
public function start() {
17-
$this->checkTemporaryDir();
1819
$this->processOldLogFiles();
1920
$this->loopOnNewFile();
2021
}
2122

22-
protected function checkTemporaryDir()
23-
{
24-
if (is_dir($this->temporaryDir)) {
25-
if (!is_readable($this->temporaryDir)) {
26-
throw new \Exception('temporary dir does not exists or not readable:' . $this->temporaryDir);
27-
}
28-
} elseif (!mkdir($this->temporaryDir)) {
29-
throw new \Exception('failed to create temporary dir:' . $this->temporaryDir);
30-
}
31-
}
32-
3323
protected function processOldLogFiles()
3424
{
3525
self::log('process old log files');
26+
$this->prepareDir($this->temporaryDir);
3627
$it = new FilesystemIterator($this->temporaryDir);
3728
foreach ($it as $fileinfo) {
38-
$result = $fileinfo->getPathname();
39-
if (!is_file($result) || !is_readable($result)) {
40-
throw new \Exception('file does not exists or is not readable:' . $result);
41-
}
29+
$filepath = $fileinfo->getPathname();
4230

43-
$content = $this->readFile($result);
31+
$content = $this->readFile($filepath);
4432
$errors = $this->parseLog($content);
4533
$this->sendErrors($errors);
46-
$this->unlink($result);
34+
$this->unlinkProcessedLog($filepath);
4735
}
4836
}
4937

@@ -57,19 +45,17 @@ protected function loopOnNewFile()
5745
}
5846

5947
if (filesize($this->logPath)) {
60-
$newPath = $this->temporaryDir . '/' . time() . '_' . uniqid();
61-
if (rename($this->logPath, $newPath)) {
62-
self::log("rename success: {$this->logPath} -> {$newPath}");
63-
} else {
64-
throw new \Exception("rename failed: {$this->logPath} -> {$newPath}");
65-
}
48+
$newName = date('Y-m-d_H-i-s') . '_' . microtime(true);
49+
$newPath = $this->temporaryDir . '/' . $newName;
50+
51+
$this->rename($this->logPath, $this->temporaryDir, $newName);
6652

6753
$content = $this->readFile($newPath);
6854
$errors = $this->parseLog($content);
6955
unset($content);
7056
$this->sendErrors($errors);
7157
unset($errors);
72-
$this->unlink($newPath);
58+
$this->unlinkProcessedLog($newName);
7359

7460
self::log('memory usage ' . (memory_get_usage(true)/1024) . 'K');
7561
}
@@ -78,22 +64,53 @@ protected function loopOnNewFile()
7864
}
7965
}
8066

67+
protected function prepareDir($dir)
68+
{
69+
if (is_dir($dir)) {
70+
if (!is_readable($dir)) {
71+
throw new \Exception('Directory does not exists or not readable:' . $dir);
72+
}
73+
} elseif (!mkdir($dir)) {
74+
throw new \Exception('failed to create directory:' . $dir);
75+
}
76+
}
77+
8178
protected function readFile($filename)
8279
{
8380
if (!is_file($filename) || !is_readable($filename)) {
8481
throw new \Exception('file does not exists or is not readable:' . $filename);
8582
}
8683
self::log('read file ' . $filename);
8784
$result = file_get_contents($filename);
85+
if (false === $result) {
86+
throw new \Exception('file_get_contents failed:' . $filename);
87+
}
8888
return $result;
8989
}
9090

91-
protected function unlink($filename)
91+
protected function unlinkProcessedLog($filename)
92+
{
93+
if ($this->keepLogDir) {
94+
$this->prepareDir($this->keepLogDir);
95+
$this->rename($this->temporaryDir . '/' . $filename, $this->keepLogDir, $filename);
96+
} else {
97+
if (unlink($this->temporaryDir . '/' . $filename)) {
98+
self::log('file deleted successfully: ' . $filename);
99+
} else {
100+
throw new \Exception('remove error log file failed: ' . $filename);
101+
}
102+
}
103+
}
104+
105+
protected function rename($pathFrom, $dirTo, $nameTo)
92106
{
93-
if (unlink($filename)) {
94-
self::log('file deleted successfully: ' . $filename);
107+
$this->prepareDir($dirTo);
108+
$to = $dirTo . '/' . $nameTo;
109+
110+
if (rename($pathFrom, $to)) {
111+
self::log("rename success: {$pathFrom} -> {$to}");
95112
} else {
96-
throw new \Exception('remove error log file failed: ' . $filename);
113+
throw new \Exception("rename failed: {$pathFrom} -> {$to}");
97114
}
98115
}
99116

@@ -124,7 +141,6 @@ protected function sendErrors(array $errors)
124141

125142
public static function log($message)
126143
{
127-
echo date('[Y-m-d H:i:s] ') . $message . PHP_EOL;
144+
echo date('[Y-m-d H:i:s]') . ' ' . $message . PHP_EOL;
128145
}
129-
130146
}

0 commit comments

Comments
 (0)