Skip to content

Commit

Permalink
feat: add logger dynamic change info method (#897)
Browse files Browse the repository at this point in the history
Co-authored-by: waiting <1661926154@qq.com>
  • Loading branch information
czy88840616 and waitingsong authored Mar 12, 2021
1 parent 4640674 commit 5a11374
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"beta": "sh scripts/publish.sh --npm-tag beta --force-publish=*",
"next": "sh scripts/publish.sh --npm-tag next",
"release": "rm -f ./packages/.DS* && sh scripts/publish.sh",
"bootstrap": "rm -f ./packages/.DS* && lerna bootstrap",
"bootstrap": "rm -f ./packages/.DS* && lerna bootstrap --concurrency 4",
"clean": "lerna clean --yes && rm -rf ./packages/**/package-lock.json && rm -f package-lock.json",
"build": "sh scripts/build.sh",
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
Expand Down
21 changes: 21 additions & 0 deletions packages/logger/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export interface ILogger {
warn(msg: any, ...args: any[]): void;
}


export type LoggerCustomInfoHandler = (
info: MidwayTransformableInfo
) => MidwayTransformableInfo;

export interface IMidwayLogger extends ILogger {
disableConsole();
enableConsole();
Expand All @@ -22,6 +27,7 @@ export interface IMidwayLogger extends ILogger {
updateConsoleLevel(level: LoggerLevel): void;
updateDefaultLabel(defaultLabel: string): void;
updateDefaultMeta(defaultMeta: object): void;
updateTransformableInfo(customInfoHandler: LoggerCustomInfoHandler): void;
getDefaultLabel(): string;
getDefaultMeta(): Record<string, unknown>;
write(...args): boolean;
Expand Down Expand Up @@ -58,3 +64,18 @@ export interface LoggerOptions {
export interface DelegateLoggerOptions {
delegateLogger: ILogger;
}

export interface MidwayTransformableInfo {
[key: string]: any;
level: string;
timestamp: string;
LEVEL: string;
pid: number;
labelText: string;
message: string;
ctx: any;
ignoreFormat: boolean;
defaultLabel: string;
originError?: Error;
stack?: string;
}
16 changes: 14 additions & 2 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
LoggerLevel,
LoggerOptions,
IMidwayLogger,
MidwayTransformableInfo,
LoggerCustomInfoHandler,
} from './interface';
import { DelegateTransport, EmptyTransport } from './transport';
import { displayLabels, displayCommonMessage } from './format';
Expand Down Expand Up @@ -35,6 +37,9 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
loggerOptions: LoggerOptions;
defaultLabel = '';
defaultMetadata = {};
customInfoHandler: LoggerCustomInfoHandler = info => {
return info;
};

constructor(options: LoggerOptions = {}) {
super(
Expand Down Expand Up @@ -197,10 +202,14 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
this.defaultMetadata = defaultMetadata;
}

updateTransformableInfo(customInfoHandler: LoggerCustomInfoHandler): void {
this.customInfoHandler = customInfoHandler;
}

getDefaultLoggerConfigure() {
const printInfo = this.loggerOptions.printFormat
? this.loggerOptions.printFormat
: info => {
: (info: MidwayTransformableInfo): string => {
return `${info.timestamp} ${info.LEVEL} ${info.pid} ${info.labelText}${info.message}`;
};

Expand All @@ -218,7 +227,10 @@ export class MidwayBaseLogger extends EmptyLogger implements IMidwayLogger {
if (info.ignoreFormat) {
return info.message;
}
return printInfo(info);
const newInfo = this.customInfoHandler(
info as MidwayTransformableInfo
);
return printInfo(newInfo || info);
})
),
};
Expand Down
24 changes: 24 additions & 0 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,28 @@ describe('/test/index.test.ts', () => {

});

it('should dynamic change info data', async () => {
clearAllLoggers();
const logsDir = join(__dirname, 'logs');
await removeFileOrDir(logsDir);

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

(logger as IMidwayLogger).updateTransformableInfo(info => {
info.timestamp = 'bbbb';
return info
});
logger.info('file logger');
logger.info('file logger1');
logger.info('file logger2');
await sleep();

expect(matchContentTimes(join(logsDir, 'test-logger.log'), 'bbbb')).toEqual(3);

await removeFileOrDir(logsDir);
});

});

0 comments on commit 5a11374

Please sign in to comment.