-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStdoutLogger.php
43 lines (35 loc) · 1.13 KB
/
StdoutLogger.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
<?php
declare(strict_types=1);
/**
*
* @author xiaoguo0426
* @contact [email protected]
* @license MIT
*/
namespace App\Util;
use Carbon\Carbon;
use Psr\Log\LogLevel;
/**
* Default logger for logging server start and requests.
* PSR-3 logger implementation that logs to STDOUT, using a newline after each
* message. Priority is ignored.
*/
final class StdoutLogger extends \Hyperf\Framework\Logger\StdoutLogger
{
protected function getMessage(string $message, string $level = LogLevel::INFO, array $tags = []): string
{
$tag = match ($level) {
LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL => 'error',
LogLevel::ERROR => 'fg=red',
LogLevel::WARNING, LogLevel::NOTICE => 'comment',
default => 'info',
};
$datetime = Carbon::now()->format('Y-m-d H:i:s-v');
$template = \sprintf('[%s] <%s>[%s]</>', $datetime, $tag, strtoupper($level));
$implodedTags = '';
foreach ($tags as $value) {
$implodedTags .= (' [' . $value . ']');
}
return \sprintf($template . ' %s' . $implodedTags, $message);
}
}