1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\Cron\Log; |
4
|
|
|
|
5
|
|
|
use T4web\Cron\Exception\RuntimeException; |
6
|
|
|
use T4web\Cron\Config; |
7
|
|
|
|
8
|
|
|
class TextFile implements LoggerInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $logDirectory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var FileSystem |
17
|
|
|
*/ |
18
|
|
|
private $fileSystem; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TextFile constructor. |
22
|
|
|
* |
23
|
|
|
* @param FileSystem $fileSystem |
24
|
|
|
* @param Config $config |
25
|
|
|
*/ |
26
|
|
|
public function __construct(FileSystem $fileSystem, Config $config) |
27
|
|
|
{ |
28
|
|
|
$this->fileSystem = $fileSystem; |
29
|
|
|
$this->logDirectory = $config->getLogDirectory(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $jobId |
34
|
|
|
* @param int $startTime |
35
|
|
|
* @param int $endTime |
36
|
|
|
* @param bool $isSuccessful |
37
|
|
|
* @param array $output |
38
|
|
|
* @param array $error |
39
|
|
|
*/ |
40
|
|
|
public function log($jobId, $startTime, $endTime, $isSuccessful, $output, $error) |
41
|
|
|
{ |
42
|
|
|
$filename = sprintf("%s/%s.log", $this->logDirectory, $jobId); |
43
|
|
|
|
44
|
|
|
$message = ''; |
45
|
|
|
if ($isSuccessful) { |
46
|
|
|
$message .= sprintf('[%s] Job processed successful', date('Y-m-d H:i:s')) . PHP_EOL; |
47
|
|
|
} else { |
48
|
|
|
$message .= sprintf('[%s] Job fail', date('Y-m-d H:i:s')) . PHP_EOL; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$message .= ' Start: ' . date('Y-m-d H:i:s', $startTime) . PHP_EOL; |
52
|
|
|
$message .= ' End: ' . date('Y-m-d H:i:s', $endTime) . PHP_EOL; |
53
|
|
|
$message .= ' Execution time: ' . $this->getExecutionTime($startTime, $endTime) . PHP_EOL; |
54
|
|
|
$message .= $this->prepareOutput($output); |
55
|
|
|
$message .= $this->prepareError($error); |
56
|
|
|
$message .= PHP_EOL; |
57
|
|
|
|
58
|
|
|
$this->fileSystem->put($filename, $message); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $startTime |
63
|
|
|
* @param int $endTime |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
private function getExecutionTime($startTime, $endTime) |
67
|
|
|
{ |
68
|
|
|
$executionTime = $endTime - $startTime; |
69
|
|
|
$days = floor($executionTime / 3600*24); |
70
|
|
|
$hours = floor(($executionTime - ($days*3600*24)) / 3600); |
71
|
|
|
$minutes = floor(($executionTime - ($hours*3600)) / 60); |
72
|
|
|
$secs = floor($executionTime % 60); |
73
|
|
|
|
74
|
|
|
$executionTimeText = ''; |
75
|
|
|
if ($days > 0) { |
76
|
|
|
$executionTimeText .= "$days days "; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($hours > 0) { |
80
|
|
|
$executionTimeText .= "$hours hours "; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($minutes > 0) { |
84
|
|
|
$executionTimeText .= "$minutes mins "; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($secs > 0) { |
88
|
|
|
$executionTimeText .= "$secs seconds"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $executionTimeText; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $output |
96
|
|
|
* @return string|void |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
private function prepareOutput($output) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if (empty($output)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return '--Output: ' . PHP_EOL . implode('', $output) . PHP_EOL . '--End output.' . PHP_EOL; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $error |
109
|
|
|
* @return string|void |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
private function prepareError($error) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if (empty($error)) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return '--Error: ' . PHP_EOL . implode('', $error) . PHP_EOL . '--End error.' . PHP_EOL; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.