Skip to content

Commit

Permalink
feat: add zlib logger file (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored May 24, 2021
1 parent 10d2e7f commit 2ae9131
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 59 deletions.
3 changes: 1 addition & 2 deletions packages/decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
],
"files": [
"dist/**/*.js",
"dist/**/*.d.ts",
"version.json"
"dist/**/*.d.ts"
],
"engines": {
"node": ">= 10.0.0"
Expand Down
31 changes: 0 additions & 31 deletions packages/decorator/version.json

This file was deleted.

3 changes: 1 addition & 2 deletions packages/grpc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compileOnSave": true,
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"jsx": "react"
"outDir": "dist"
},
"include": [
"./src/**/*.ts"
Expand Down
4 changes: 3 additions & 1 deletion packages/logger/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface IMidwayLogger extends ILogger {
close(): any;
}

export type LoggerLevel = 'silly' | 'debug' | 'info' | 'warn' | 'error';
export type LoggerLevel = 'all' | 'silly' | 'debug' | 'info' | 'warn' | 'error' | 'none';

export interface LoggerOptions {
format?: logform.Format;
Expand All @@ -62,6 +62,8 @@ export interface LoggerOptions {
errMaxSize?: string;
errMaxFiles?: string;
eol?: string;
fileZippedArchive?: boolean;
errZippedArchive?: boolean;
}

export interface DelegateLoggerOptions {
Expand Down
11 changes: 8 additions & 3 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const isWindows = os.platform() === 'win32';
export const EmptyLogger: Logger = createLogger().constructor as Logger;

const midwayLogLevels = {
all: 0,
none: 0,
error: 1,
warn: 2,
info: 3,
verbose: 4,
debug: 5,
silly: 6,
all: 7,
};

/**
Expand Down Expand Up @@ -81,13 +82,14 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
format.colorize({
all: true,
colors: {
all: 'reset',
none: 'reset',
error: 'red',
warn: 'yellow',
info: 'reset',
verbose: 'reset',
debug: 'blue',
silly: 'reset',
all: 'reset',
},
})
),
Expand Down Expand Up @@ -144,6 +146,7 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
maxSize: this.loggerOptions.fileMaxSize || '200m',
maxFiles: this.loggerOptions.fileMaxFiles || '31d',
eol: this.loggerOptions.eol || os.EOL,
zippedArchive: this.loggerOptions.fileZippedArchive,
});
}
this.add(this.fileTransport);
Expand All @@ -165,6 +168,7 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
maxSize: this.loggerOptions.errMaxSize || '200m',
maxFiles: this.loggerOptions.errMaxFiles || '31d',
eol: this.loggerOptions.eol || os.EOL,
zippedArchive: this.loggerOptions.errZippedArchive,
});
}
this.add(this.errTransport);
Expand Down Expand Up @@ -259,7 +263,8 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
(args.length === 1 && typeof args[0] !== 'object') ||
!args[0]['level']
) {
return super.log.apply(this, ['all', ...args, { ignoreFormat: true }]);
// 这里必须要用 none
return super.log.apply(this, ['none', ...args, { ignoreFormat: true }]);
} else {
return super.write.apply(this, args);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/logger/src/rotate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// fork from https://github.com/winstonjs/winston-daily-rotate-file v4.5.5

import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as zlib from 'zlib';
import * as hash from 'object-hash';
import { MESSAGE } from 'triple-beam';
import { PassThrough } from 'stream';
Expand Down Expand Up @@ -131,6 +134,29 @@ export class DailyRotateFileTransport extends Transport {
}
this.emit('logRemoved', params.name);
});

if (options.zippedArchive) {
this.logStream.on('rotate', oldFile => {
const oldFileExist = fs.existsSync(oldFile);
const gzExist = fs.existsSync(oldFile + '.gz');
if (!oldFileExist || gzExist) {
return;
}

const gzip = zlib.createGzip();
const inp = fs.createReadStream(oldFile);
const out = fs.createWriteStream(oldFile + '.gz');
inp
.pipe(gzip)
.pipe(out)
.on('finish', () => {
if (fs.existsSync(oldFile)) {
fs.unlinkSync(oldFile);
}
this.emit('archive', oldFile + '.gz');
});
});
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ describe('/test/index.test.ts', () => {
const logger = createLogger<IMidwayLogger>('logger', {
dir: logsDir,
disableError: true,
level: 'error',
});
logger.write('hello world');
const buffer = Buffer.from('hello world', 'utf-8');
Expand Down Expand Up @@ -742,4 +743,45 @@ describe('/test/index.test.ts', () => {
await removeFileOrDir(logsDir);
});

it('should no output when level = none', async () => {
clearAllLoggers();
const logsDir = join(__dirname, 'logs');
await removeFileOrDir(logsDir);

const logger = createFileLogger('file', {
dir: logsDir,
fileLogName: 'test-logger.log',
level: 'none',
});

logger.info('file logger');
logger.info('file logger1');
logger.info('file logger2');
await sleep();

expect(matchContentTimes(join(logsDir, 'test-logger.log'), 'file logger')).toEqual(0);
await removeFileOrDir(logsDir);
});

it('should output all level when level = all', async () => {
clearAllLoggers();
const logsDir = join(__dirname, 'logs');
await removeFileOrDir(logsDir);

const logger = createFileLogger('file', {
dir: logsDir,
fileLogName: 'test-logger.log',
level: 'all',
});

logger.info('file logger');
logger.info('file logger1');
logger.info('file logger2');
await sleep();

expect(matchContentTimes(join(logsDir, 'test-logger.log'), 'file logger')).toEqual(3);
await removeFileOrDir(logsDir);
});


});
22 changes: 22 additions & 0 deletions packages/logger/test/rotate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,27 @@ describe('winston/transports/daily-rotate-file', function () {
sendLogItem(transport, 'info', randomString(1056));
transport.close();
});

describe('when setting zippedArchive', function () {
it('should archive the log after rotating', function (done) {
const opts: any = Object.assign({}, options);
opts.zippedArchive = true;
opts.maxSize = '1k';

transport = new DailyRotateFile(opts);

transport.on('finish', function () {
fs.readdir(logDir, function (err, files) {
expect(files.filter(function (file) {
return path.extname(file) === '.gz';
}).length).toEqual(1);
done();
});
});
sendLogItem(transport, 'info', randomString(1056));
sendLogItem(transport, 'info', randomString(1056));
transport.close();
});
});
});
});
20 changes: 4 additions & 16 deletions packages/prometheus/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
{
"extends": "../../tsconfig.json",
"compileOnSave": true,
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":false,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"noImplicitReturns": false,
"pretty": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules",
"test"
"include": [
"./src/**/*.ts"
]
}
3 changes: 1 addition & 2 deletions packages/web-express/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compileOnSave": true,
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"jsx": "react"
"outDir": "dist"
},
"include": [
"./src/**/*.ts"
Expand Down
3 changes: 1 addition & 2 deletions packages/web-koa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compileOnSave": true,
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"jsx": "react"
"outDir": "dist"
},
"include": [
"./src/**/*.ts"
Expand Down

0 comments on commit 2ae9131

Please sign in to comment.