Skip to content

Commit

Permalink
fix: use @eggjs/logrotator
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 3, 2025
1 parent cb36fca commit b61d81d
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 44 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/pr-contributor-welcome.yml

This file was deleted.

1 change: 0 additions & 1 deletion index-old.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
// } from 'egg-core';
// import EggCookies = require('egg-cookies');
// import 'egg-multipart';
// import 'egg-logrotator';
// import 'egg-view';

// declare module 'egg' {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@eggjs/development": "^4.0.0",
"@eggjs/i18n": "^3.0.1",
"@eggjs/jsonp": "^3.0.0",
"@eggjs/logrotator": "^4.0.0",
"@eggjs/onerror": "^3.0.0",
"@eggjs/schedule": "^5.0.2",
"@eggjs/security": "^4.0.0",
Expand All @@ -36,7 +37,6 @@
"cluster-client": "^3.7.0",
"egg-errors": "^2.3.2",
"egg-logger": "^3.6.1",
"egg-logrotator": "^3.2.0",
"egg-multipart": "^3.5.0",
"egg-view": "^2.1.4",
"extend2": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion site/docs/basics/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Specific consolidation rules can be found in [Configuration](./config.md).
  - [multipart](https://github.com/eggjs/egg-multipart) File Streaming Upload
  - [security](https://github.com/eggjs/security) Security
  - [development](https://github.com/eggjs/development) Development Environment Configuration
  - [logrotator](https://github.com/eggjs/egg-logrotator) Log segmentation
  - [logrotator](https://github.com/eggjs/logrotator) Log segmentation
  - [schedule](https://github.com/eggjs/schedule) Timing tasks
  - [static](https://github.com/eggjs/static) Static server
  - [jsonp](https://github.com/eggjs/jsonp) jsonp support
Expand Down
2 changes: 1 addition & 1 deletion site/docs/basics/plugin.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ exports.mysql = {
- [multipart](https://github.com/eggjs/egg-multipart) 文件流式上传
- [security](https://github.com/eggjs/security) 安全
- [development](https://github.com/eggjs/development) 开发环境配置
- [logrotator](https://github.com/eggjs/egg-logrotator) 日志切分
- [logrotator](https://github.com/eggjs/logrotator) 日志切分
- [schedule](https://github.com/eggjs/schedule) 定时任务
- [static](https://github.com/eggjs/static) 静态服务器
- [jsonp](https://github.com/eggjs/jsonp) jsonp 支持
Expand Down
4 changes: 2 additions & 2 deletions site/docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Performance is what we always consider as important part in our services so that

## Log Sharding

One common requirement you can find in enterprise logs is automatic log sharding, which offers a convenient way for management. Luckily, Egg takes [egg-logrotator](https://github.com/eggjs/egg-logrotator) as built-in solution to meet the need.
One common requirement you can find in enterprise logs is automatic log sharding, which offers a convenient way for management. Luckily, Egg takes [@eggjs/logrotator](https://github.com/eggjs/logrotator) as built-in solution to meet the need.

### Daily Sharding

Expand Down Expand Up @@ -369,4 +369,4 @@ Generally, requests are frequent events to Web services, so writing logs into di

> Logs will be firstly transferred into memory, and then Egg will asynchronously write them into files by second.
More about [egg-logger](https://github.com/eggjs/egg-logger) and [egg-logrotator](https://github.com/eggjs/egg-logrotator)
More about [egg-logger](https://github.com/eggjs/egg-logger) and [@eggjs/logrotator](https://github.com/eggjs/logrotator)
5 changes: 3 additions & 2 deletions site/docs/core/logger.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ app.getLogger('errorLogger').set('remote', new RemoteErrorTransport({ level: 'ER
```

上述代码示例中,虽然比较简单,但是在实际使用时需要考虑性能问题。通常采取先暂存至内存,再定时上传的策略,以此优化性能。

## 日志切割

企业级日志一个最常见的需求之一是对日志进行自动切割,以方便管理。框架对日志切割的支持由 [egg-logrotator](https://github.com/eggjs/egg-logrotator) 插件提供。
企业级日志一个最常见的需求之一是对日志进行自动切割,以方便管理。框架对日志切割的支持由 [@eggjs/logrotator](https://github.com/eggjs/logrotator) 插件提供。

### 按天切割

Expand Down Expand Up @@ -369,4 +370,4 @@ module.exports = (appInfo) => {

> 日志同步写入内存,异步每隔一段时间(默认 1 秒)进行刷盘。
更多细节,请参考 [egg-logger](https://github.com/eggjs/egg-logger)[egg-logrotator](https://github.com/eggjs/egg-logrotator)
更多细节,请参考 [egg-logger](https://github.com/eggjs/egg-logger)[@eggjs/logrotator](https://github.com/eggjs/logrotator)
2 changes: 1 addition & 1 deletion src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default {
*/
logrotator: {
enable: true,
package: 'egg-logrotator',
package: '@eggjs/logrotator',
},

/**
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import '@eggjs/security';
import '@eggjs/schedule';
import '@eggjs/session';
import '@eggjs/onerror';
import '@eggjs/logrotator';

export type {
EggAppInfo,
} from '@eggjs/core';
Expand Down
8 changes: 8 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectType } from 'tsd';
import { EggCore, Context } from '@eggjs/core';
import { LogRotator } from '@eggjs/logrotator';
import {
Application, IBoot, ILifecycleBoot,
LoggerLevel,
Expand All @@ -17,6 +18,13 @@ expectType<IMessenger>(app.messenger);
expectType<IMessenger>(app.messenger.broadcast('test'));
expectType<void>(app.loggers.reload());

class MyLogRotator extends LogRotator {
async getRotateFiles() {
return new Map();
}
}
expectType<LogRotator>(new MyLogRotator({ app }));

const ctx = app.createAnonymousContext();

expectType<Promise<void>>(app.runInAnonymousContextScope(async ctx => {
Expand Down
3 changes: 2 additions & 1 deletion test/lib/plugins/logrotator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ describe('test/lib/plugins/logrotator.test.ts', () => {
after(() => app.close());

it('should rotate log file default', async () => {
const file = importResolve('egg-logrotator/app/schedule/rotate_by_file.js');
const file = importResolve('@eggjs/logrotator/dist/esm/app/schedule/rotate_by_file.js');
// console.log('job', file);
await app.runSchedule(file);
await scheduler.wait(1000);
const files = (await fs.readdir(app.config.logger.dir)).filter(f => f.includes('.log.'));
console.log(files);
assert(files.length > 0);
files.forEach(file => {
assert(/\.log\.\d{4}-\d{2}-\d{2}$/.test(file));
Expand Down

0 comments on commit b61d81d

Please sign in to comment.