Skip to content

Commit

Permalink
feature to add metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bitrinjani committed Feb 26, 2018
1 parent c7aa8cf commit 753a3f7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
18 changes: 15 additions & 3 deletions __tests__/index.test.ts
Expand Up @@ -9,12 +9,24 @@ test('logger', () => {
log.debug('debug log');
const log2 = logger.getLogger('label');
expect(log).toBe(log2);
logger.options.enabled = false;
log.info('not shown');
});

test('logger disabled', () => {
test('logger with obj', () => {
const log = logger.getLogger('obj');
log.warn({ hidden: true }, '[hidden]warn log');
log.info({ hidden: true }, '[hidden]info log');
log.error({ hidden: true }, '[hidden]error log');
log.debug({ hidden: true }, '[hidden]debug log');
log.warn({ hidden: true });
log.info({ hidden: true });
log.error({ hidden: true });
log.debug({ hidden: true });
});

test('logger disabled', () => {
logger.options.enabled = false;
const log = logger.getLogger('label');
log.info('not shown');
logger.options.enabled = true;
});

40 changes: 32 additions & 8 deletions lib/index.ts
Expand Up @@ -5,10 +5,10 @@ import * as util from 'util';
export const options = { enabled: process.env.NODE_ENV === 'test' ? false : true };

export interface Logger {
debug: (s: string, ...args: any[]) => void;
info: (s: string, ...args: any[]) => void;
warn: (s: string, ...args: any[]) => void;
error: (s: string, ...args: any[]) => void;
debug: (s: string | object, ...args: any[]) => void;
info: (s: string | object, ...args: any[]) => void;
warn: (s: string | object, ...args: any[]) => void;
error: (s: string | object, ...args: any[]) => void;
}

const logger = pino({ level: 'debug' });
Expand All @@ -23,10 +23,34 @@ export function getLogger(label: string): Logger {
}
const childLogger = logger.child({ label });
const wrappedLogger = {
debug: (s: string, ...args) => childLogger.debug(util.format(s, ...args)),
info: (s: string, ...args) => childLogger.info(util.format(s, ...args)),
warn: (s: string, ...args) => childLogger.warn(util.format(s, ...args)),
error: (s: string, ...args) => childLogger.error(util.format(s, ...args))
debug: (s: string | object, ...args) => {
if (typeof s === 'string') {
childLogger.debug(util.format(s, ...args));
} else if (args.length > 0) {
childLogger.debug(s, util.format(args[0], ..._.tail(args)));
}
},
info: (s: string | object, ...args) => {
if (typeof s === 'string') {
childLogger.info(util.format(s, ...args));
} else if (args.length > 0) {
childLogger.info(s, util.format(args[0], ..._.tail(args)));
}
},
warn: (s: string | object, ...args) => {
if (typeof s === 'string') {
childLogger.warn(util.format(s, ...args));
} else if (args.length > 0) {
childLogger.warn(s, util.format(args[0], ..._.tail(args)));
}
},
error: (s: string | object, ...args) => {
if (typeof s === 'string') {
childLogger.error(util.format(s, ...args));
} else if (args.length > 0) {
childLogger.error(s, util.format(args[0], ..._.tail(args)));
}
}
};
cache.set(label, wrappedLogger);
return wrappedLogger;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@bitr/logger",
"version": "1.0.0",
"version": "1.0.1",
"description": "A simple wrapper of pino logger.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 753a3f7

Please sign in to comment.