Skip to content

Commit d5461f6

Browse files
committed
tweak log component and setting
1 parent 324b6c5 commit d5461f6

File tree

7 files changed

+33
-58
lines changed

7 files changed

+33
-58
lines changed

Agent.md renamed to Claude.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Agent.md
1+
# Claude.md
22

33
## Project Objective
44

@@ -15,4 +15,9 @@ Yii2 Swoole extension, provides coroutine http server and more coroutine feature
1515
## Directory Structure
1616

1717
- examples/: yii2 applications for demonstrating how to use yii2-swoole
18-
- src/: yii2-swoole extension for integrating swoole coroutine into yii2
18+
- src/: yii2-swoole extension for integrating swoole coroutine into yii2
19+
20+
## Coding Standards
21+
22+
- Always write clean code.
23+
- Only add necessary comments and avoid redundant comments.

examples/app-api/config/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'targets' => [
1818
[
1919
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
20-
'levels' => ['error', 'warning', 'info'],
20+
'levels' => YII_DEBUG ? ['error', 'warning', 'info'] : ['error', 'warning'],
2121
'exportInterval' => 1,
2222
'logFile' => '@runtime/logs/console.log',
2323
'maxFileSize' => 10240, // 10MB

examples/app-api/config/web.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
'targets' => [
3535
[
3636
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
37-
'levels' => ['error', 'warning'],
37+
'levels' => YII_DEBUG ? ['error', 'warning', 'info'] : ['error', 'warning'],
3838
'exportInterval' => 1,
3939
'logFile' => '@runtime/logs/app.log',
40-
'maxFileSize' => 10240,
40+
'maxFileSize' => 10240, // 10MB
4141
'maxLogFiles' => 5,
4242
'enableRotation' => true,
4343
'categories' => [],
4444
'except' => [],
45-
'logVars' => false,
45+
'logVars' => [],
4646
'microtime' => true,
4747
],
4848
],

examples/app-basic/config/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
'targets' => [
3333
[
3434
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
35-
'levels' => ['error', 'warning', 'info'],
35+
'levels' => YII_DEBUG ? ['error', 'warning', 'info'] : ['error', 'warning'],
3636
'exportInterval' => 1,
3737
'logFile' => '@runtime/logs/console.log',
3838
'maxFileSize' => 10240, // 10MB

examples/app-basic/config/web.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
'targets' => [
5151
[
5252
'class' => \Dacheng\Yii2\Swoole\Log\CoroutineFileTarget::class,
53-
'levels' => ['error', 'warning'],
53+
'levels' => YII_DEBUG ? ['error', 'warning', 'info'] : ['error', 'warning'],
5454
'exportInterval' => 1,
5555
'logFile' => '@runtime/logs/app.log',
56-
'maxFileSize' => 10240,
56+
'maxFileSize' => 10240, // 10MB
5757
'maxLogFiles' => 5,
5858
'enableRotation' => true,
5959
'categories' => [],

src/Log/CoroutineFileTarget.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public function init()
5757

5858
$this->maxLogFiles = max(1, $this->maxLogFiles);
5959
$this->maxFileSize = max(1, $this->maxFileSize);
60-
61-
$this->ensureInitialized();
6260
}
6361

6462
private function ensureInitialized(): void
@@ -67,19 +65,17 @@ private function ensureInitialized(): void
6765
return;
6866
}
6967

70-
if (\Swoole\Coroutine::getCid() > 0) {
71-
$this->worker = new LogWorker(
72-
$this->logFile,
73-
$this->enableRotation,
74-
$this->maxFileSize,
75-
$this->maxLogFiles,
76-
$this->fileMode,
77-
$this->dirMode
78-
);
68+
$this->worker = new LogWorker(
69+
$this->logFile,
70+
$this->enableRotation,
71+
$this->maxFileSize,
72+
$this->maxLogFiles,
73+
$this->fileMode,
74+
$this->dirMode
75+
);
7976

80-
$this->worker->start();
81-
$this->initialized = true;
82-
}
77+
$this->worker->start();
78+
$this->initialized = true;
8379
}
8480

8581
public function export()
@@ -98,12 +94,6 @@ public function export()
9894

9995
$this->ensureInitialized();
10096

101-
if (!$this->initialized || $this->worker === null) {
102-
$this->exportSync();
103-
$this->messages = [];
104-
return;
105-
}
106-
10797
$formattedMessages = array_map([$this, 'formatMessage'], $this->messages);
10898
$this->messages = [];
10999

@@ -225,7 +215,6 @@ public function shutdown(): void
225215
$this->initialized = false;
226216
}
227217

228-
229218
public function __destruct()
230219
{
231220
try {

src/Log/LogWorker.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ public function stop(): void
8989
}
9090
}
9191

92-
/**
93-
* Pushes messages directly to the buffer
94-
*/
9592
public function pushMessages(array $messages): bool
9693
{
9794
if (!$this->running) {
@@ -134,40 +131,24 @@ private function writeMessages(array $messages): void
134131
return;
135132
}
136133

137-
$fp = @fopen($this->logFile, 'a');
138-
if ($fp === false) {
139-
error_log("LogWorker: Unable to open log file: {$this->logFile}");
134+
$success = @file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
135+
136+
if ($success === false) {
137+
error_log("LogWorker: Unable to write to log file: {$this->logFile}");
140138
return;
141139
}
142-
143-
@flock($fp, LOCK_EX);
144-
140+
141+
if ($this->fileMode !== null) {
142+
@chmod($this->logFile, $this->fileMode);
143+
}
144+
145145
if ($this->enableRotation) {
146146
clearstatcache();
147147
$fileSize = @filesize($this->logFile);
148148
if ($fileSize !== false && $fileSize > $this->maxFileSize * 1024) {
149-
@flock($fp, LOCK_UN);
150-
@fclose($fp);
151-
152149
$this->rotateFiles();
153-
154-
$fp = @fopen($this->logFile, 'a');
155-
if ($fp === false) {
156-
error_log("LogWorker: Unable to reopen log file after rotation: {$this->logFile}");
157-
return;
158-
}
159-
@flock($fp, LOCK_EX);
160150
}
161151
}
162-
163-
@fwrite($fp, $text);
164-
@fflush($fp);
165-
@flock($fp, LOCK_UN);
166-
@fclose($fp);
167-
168-
if ($this->fileMode !== null) {
169-
@chmod($this->logFile, $this->fileMode);
170-
}
171152
}
172153

173154
private function rotateFiles(): void

0 commit comments

Comments
 (0)