-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Logger.php
75 lines (68 loc) · 2.29 KB
/
Logger.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* @file
* Contains \Drush\Log\Logger.
*
* This is the actual Logger for Drush that is responsible
* for logging messages.
*
* This logger is designed such that it can be provided to
* other libraries that log to a Psr\Log\LoggerInterface.
*
* Those who may wish to change the way logging works in Drush
* should therefore NOT attempt to replace this logger with their
* own LoggerInterface, as it will not work. It would be okay
* to extend Drush\Log\Logger, or perhaps we could provide a way
* to set an output I/O object here, in case output redirection
* was the only thing that needed to be swapped out.
*/
namespace Drush\Log;
use Drush\Drush;
use Robo\Log\RoboLogger;
use Symfony\Component\Console\Output\OutputInterface;
class Logger extends RoboLogger
{
public function __construct(OutputInterface $output)
{
parent::__construct($output);
}
public function log($level, $message, array $context = []): void
{
// Append timer and memory values.
if (Drush::debug()) {
$timer = round(microtime(true) - DRUSH_REQUEST_TIME, 2);
$suffix = sprintf(' [%s sec, %s]', $timer, self::formatSize(memory_get_usage()));
$message .= $suffix;
}
// consolidation/log handles formatting and verbosity level check.
parent::log($level, $message, $context);
}
// Copy of format_size() in Drupal.
public static function formatSize($size)
{
if ($size < DRUSH_KILOBYTE) {
// format_plural() not always available.
return dt('@count bytes', ['@count' => $size]);
} else {
$size /= DRUSH_KILOBYTE; // Convert bytes to kilobytes.
$units = [
dt('@size KB', []),
dt('@size MB', []),
dt('@size GB', []),
dt('@size TB', []),
dt('@size PB', []),
dt('@size EB', []),
dt('@size ZB', []),
dt('@size YB', []),
];
foreach ($units as $unit) {
if (round($size, 2) >= DRUSH_KILOBYTE) {
$size /= DRUSH_KILOBYTE;
} else {
break;
}
}
return str_replace('@size', round($size, 2), $unit);
}
}
}