Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use @eggjs/logrotator #5390

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions src/lib/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,5 +698,8 @@ declare module '@eggjs/core' {
createAnonymousContext(req?: any): EggContext;
runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>, req?: unknown): Promise<void>;
readonly messenger: IMessenger;
Subscription: typeof BaseContextClass;
BaseHookClass: typeof BaseHookClass;
Boot: typeof BaseHookClass;
}
}
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
17 changes: 17 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 Down Expand Up @@ -68,6 +69,22 @@ expectType<boolean>(app.config.session.httpOnly);
expectType<(err: any, ctx: any) => void>(app.config.onerror.html!);
expectType<string>(app.config.onerror.errorPageUrl as string);

// logrotator plugin types
class MyLogRotator extends LogRotator {
async getRotateFiles() {
return new Map();
}
}
expectType<LogRotator>(new MyLogRotator({ app }));
expectType<boolean>(app.config.logrotator.disableRotateByDay);
expectType<number>(app.config.logrotator.maxDays);
expectType<number>(app.config.logrotator.maxFileSize);
expectType<number>(app.config.logrotator.maxFiles);
expectType<number>(app.config.logrotator.rotateDuration);
expectType<boolean>(app.config.logrotator.gzip);
expectType<string>(app.config.logrotator.hourDelimiter);
expectType<string[] | null>(app.config.logrotator.filesRotateBySize);

class AppBoot implements ILifecycleBoot {
private readonly app: Application;

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
Loading