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: serverless logger rotator error #992

Merged
merged 3 commits into from
Apr 16, 2021
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
39 changes: 29 additions & 10 deletions packages-serverless/runtime-engine/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const debuglog = util.debuglog('RuntimeEngine:Logger');

export class ServerlessLogger extends Logger implements IServerlessLogger {
options;
private waiting = false;

constructor(options) {
super(options);
Expand Down Expand Up @@ -78,26 +79,44 @@ export class ServerlessLogger extends Logger implements IServerlessLogger {
*/
protected async rotateLogBySize(): Promise<void> {
try {
const maxFileSize =
this.options.maxFileSize ||
Number(process.env.LOG_ROTATE_FILE_SIZE) ||
DEFAULT_MAX_FILE_SIZE;
const transport: any = this.get('file');
if (transport?._stream?.writable) {
const stat = await fs.fstat(transport._stream.fd);
if (stat.size >= maxFileSize) {
this.info(
`File ${transport._stream.path} (fd ${transport._stream.fd}) reach the maximum file size, current size: ${stat.size}, max size: ${maxFileSize}`
);
await this.rotateBySize();
if (transport._stream.fd) {
await this.checkAndRotate(transport);
} else {
if (this.waiting) {
debuglog('rotateLogBySize waiting for open fd');
return;
}
this.waiting = true;
// 如果没有 fd,这里需要监听 open 事件,这时候才会有 fd,否则直接抛异常了
transport._stream.on('open', async (fd: number) => {
this.waiting = false;
await this.checkAndRotate(transport);
});
}
}
} catch (e) {
debuglog('rotateLogBySize error =>' + e.stack);
e.message = `${e.message}`;
this.error(e);
}
}

protected async checkAndRotate(transport) {
const maxFileSize =
this.options.maxFileSize ||
Number(process.env.LOG_ROTATE_FILE_SIZE) ||
DEFAULT_MAX_FILE_SIZE;
const stat = await fs.fstat(transport._stream.fd);
if (stat.size >= maxFileSize) {
this.info(
`File ${transport._stream.path} (fd ${transport._stream.fd}) reach the maximum file size, current size: ${stat.size}, max size: ${maxFileSize}`
);
await this.rotateBySize();
}
}

/**
* Cut log file by size
* @param {RotationStrategy} strategy
Expand Down
20 changes: 20 additions & 0 deletions packages-serverless/runtime-engine/test/lib/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,24 @@ describe('logger.test.ts', () => {
});
});
});

describe('logger benchmark', () => {
it('logger benchmark should be ok', async () => {
const loggerFactory = new BaseLoggerFactory(__dirname);
const log = (loggerFactory as any).createLogger(path.join(__dirname, '_benchmarks/test.log'), {
fileClearInterval: 100,
maxFileSize: 1,
maxFiles: 2,
});

for (let i = 0; i < 100000; i++) {
log.info('asjdfaoj230u4u9rpasdjfasjdfpoaiweurpoqwurapsjf;lasdjfopasiefpoqwuerpoajsdpfjasdjfa;lsdjfaosdfjpawierpqoiwe ==> i = %s', i);
log.error('asjdfaoj230u4u9rpasdjfasjdfpoaiweurpoqwurapsjf;lasdjfopasiefpoqwuerpoajsdpfjasdjfa;lsdjfaosdfjpawierpqoiwe ==> i = %s', i);

// await new Promise<void>(resolve => {
// setTimeout(() => resolve(), 50);
// });
}
});
});
});