1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the LdapToolsBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Chad Sikorra <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LdapTools\Bundle\LdapToolsBundle\Log; |
12
|
|
|
|
13
|
|
|
use LdapTools\Log\LdapLoggerInterface; |
14
|
|
|
use LdapTools\Log\LogOperation; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* LDAP logger. |
20
|
|
|
* |
21
|
|
|
* @author Chad Sikorra <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class LdapLogger implements LdapLoggerInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var null|LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|Stopwatch |
32
|
|
|
*/ |
33
|
|
|
protected $stopwatch; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param LoggerInterface|null $logger |
37
|
|
|
* @param Stopwatch|null $stopwatch |
38
|
|
|
*/ |
39
|
|
|
public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null) |
40
|
|
|
{ |
41
|
|
|
$this->logger = $logger; |
42
|
|
|
$this->stopwatch = $stopwatch; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function start(LogOperation $log) |
49
|
|
|
{ |
50
|
|
|
if (!is_null($this->stopwatch)) { |
51
|
|
|
$this->stopwatch->start('ldaptools', strtolower($log->getOperation()->getName())); |
52
|
|
|
} |
53
|
|
|
if (!is_null($this->logger)) { |
54
|
|
|
$this->log($log); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function end(LogOperation $log) |
62
|
|
|
{ |
63
|
|
|
if (!is_null($this->stopwatch)) { |
64
|
|
|
$this->stopwatch->stop('ldaptools'); |
65
|
|
|
} |
66
|
|
|
if (!is_null($this->logger)) { |
67
|
|
|
$this->log($log); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Logs a message. |
73
|
|
|
* |
74
|
|
|
* @param LogOperation $log |
75
|
|
|
*/ |
76
|
|
|
protected function log(LogOperation $log) |
77
|
|
|
{ |
78
|
|
|
$message = $this->getLogMessage($log); |
79
|
|
|
|
80
|
|
|
if (!is_null($log->getError())) { |
81
|
|
|
$this->logger->error($message); |
82
|
|
|
} else { |
83
|
|
|
$this->logger->debug($message); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param LogOperation $log |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function getLogMessage(LogOperation $log) |
92
|
|
|
{ |
93
|
|
|
$startOrStop = is_null($log->getStopTime()) ? 'Start' : 'End'; |
94
|
|
|
$message = "(".$log->getDomain()." on ".$log->getOperation()->getServer().") $startOrStop ".$log->getOperation()->getName()." Operation - "; |
95
|
|
|
|
96
|
|
|
$params = []; |
97
|
|
|
if (is_null($log->getStopTime())) { |
98
|
|
|
foreach ($log->getOperation()->getLogArray() as $key => $value) { |
99
|
|
|
if ($key != "Server") { |
100
|
|
|
$params[] = "$key: $value"; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
if (!is_null($log->getError())) { |
105
|
|
|
$params[] = "Error: ".$log->getError(); |
106
|
|
|
} |
107
|
|
|
$params[] = "Completed in ".(round(($log->getStopTime() - $log->getStartTime()) * 1000))." ms."; |
108
|
|
|
} |
109
|
|
|
$message .= implode(', ', $params); |
110
|
|
|
|
111
|
|
|
return $message; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|