Skip to content

Commit 367b785

Browse files
committed
日志LogRecord记录时间信息
1 parent 63faead commit 367b785

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/think/event/LogRecord.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@
1010
// +----------------------------------------------------------------------
1111
namespace think\event;
1212

13+
use DateTimeImmutable;
14+
1315
/**
1416
* LogRecord事件类
1517
*/
1618
class LogRecord
1719
{
1820
/** @var string */
19-
public $type;
21+
public string $type;
2022

2123
/** @var string */
2224
public $message;
2325

26+
/** @var DateTimeImmutable */
27+
public DateTimeImmutable $time;
28+
2429
public function __construct($type, $message)
2530
{
2631
$this->type = $type;
2732
$this->message = $message;
33+
$this->time = new DateTimeImmutable();
2834
}
2935
}

src/think/log/Channel.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ class Channel implements LoggerInterface
2626

2727
/**
2828
* 日志信息
29-
* @var array
29+
* @var array<LogRecord>
3030
*/
31-
protected $log = [];
31+
protected array $log = [];
3232

3333
/**
3434
* 关闭日志
3535
* @var bool
3636
*/
37-
protected $close = false;
37+
protected bool $close = false;
3838

3939
public function __construct(protected string $name, protected LogHandlerInterface $logger, protected array $allow, protected bool $lazy, protected Event $event)
4040
{
@@ -85,10 +85,11 @@ public function record($msg, string $type = 'info', array $context = [], bool $l
8585
$msg = strtr($msg, $replace);
8686
}
8787

88-
if (!empty($msg) || 0 === $msg) {
89-
$this->log[] = [$type, $msg];
88+
if (!empty($msg)) {
89+
$record = new LogRecord($type, $msg);
90+
$this->log[] = $record;
9091
if ($this->event) {
91-
$this->event->trigger(new LogRecord($type, $msg));
92+
$this->event->trigger($record);
9293
}
9394
}
9495

@@ -128,16 +129,14 @@ public function getLog(): array
128129
public function save(): bool
129130
{
130131
$log = $this->log;
131-
132-
$this->log = [];
133-
134132
if ($this->event) {
135-
$event = new LogWrite($this->name, $log);
133+
$event = new LogWrite($this->name, $this->log);
136134
$this->event->trigger($event);
137135
$log = $event->log;
138136
}
139137

140138
$this->logger->save($log);
139+
$this->log = [];
141140

142141
return true;
143142
}

src/think/log/driver/File.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use think\App;
1616
use think\contract\LogHandlerInterface;
17+
use think\event\LogRecord;
1718

1819
/**
1920
* 本地化调试输出到文件
@@ -59,7 +60,7 @@ public function __construct(App $app, $config = [])
5960
/**
6061
* 日志写入接口
6162
* @access public
62-
* @param array $log 日志信息
63+
* @param array<LogRecord> $log 日志信息
6364
* @return bool
6465
*/
6566
public function save(array $log): bool
@@ -72,10 +73,10 @@ public function save(array $log): bool
7273
$messages = [];
7374

7475
// 日志信息封装
75-
$time = \DateTime::createFromFormat('0.u00 U', microtime())
76-
->setTimezone(new \DateTimeZone(date_default_timezone_get()))->format($this->config['time_format']);
77-
78-
foreach ($log as [$type, $msg]) {
76+
foreach ($log as $record) {
77+
$type = $record->type;
78+
$msg = $record->message;
79+
$time = $record->time->format($this->config['time_format']);
7980
if (!is_string($msg)) {
8081
$msg = var_export($msg, true);
8182
}

tests/LogTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testFileLog()
6969

7070
$this->log->info('foo');
7171

72-
$this->assertEquals([['info', 'foo']], $this->log->getLog());
72+
$this->assertEquals([['info', 'foo']], array_map(fn($log) => [$log['type'], $log['message']], $this->log->getLog()));
7373

7474
$this->log->clear();
7575

@@ -95,7 +95,7 @@ public function testFileLog()
9595
['debug', 'foo'],
9696
['sql', 'foo'],
9797
['custom', 'foo'],
98-
], $this->log->getLog());
98+
], array_map(fn($log) => [$log['type'], $log['message']], $this->log->getLog()));
9999

100100
$this->log->write('foo');
101101
$this->assertTrue($root->hasChildren());

0 commit comments

Comments
 (0)