@@ -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