Skip to content

Commit

Permalink
refactor(utils/logger): changes format when level >= 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 7, 2019
1 parent bbf86d2 commit d9ace49
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk, { Chalk } from 'chalk';
import { DEFAULT_LOG_LEVEL } from '~/constants';
import { TLogger } from '~/types';

// TODO: register exits logger once logger has been updated there
const APP_NAME = 'kpo';
const logger = loglevel.getLogger(`_${APP_NAME}_logger_`);
logger.setDefaultLevel(DEFAULT_LOG_LEVEL);
Expand All @@ -14,18 +15,20 @@ function setLevel(level: TLogger): void {
const colors: { [key in TLogger]?: Chalk } = {
trace: chalk.magenta,
debug: chalk.cyan,
info: chalk.bold.green,
warn: chalk.bold.yellow,
error: chalk.bold.red
};

function prefix(level: TLogger): string {
const color = colors[level];
const name = level.toUpperCase();
// Don't log app name or info prefix when log level is 'info', 'warn', 'error'
// Don't prefix app name when log level is 'info', 'warn', 'error';
// don't prefix level 'info'
if (logger.getLevel() >= 2) {
return level === 'info' ? '' : color ? color(`${name}: `) : `${name}: `;
}
return APP_NAME + ' ' + (color ? color(`${name}: `) : `${name}: `);
return (color ? color(`[${name}]`) : `[${name}]`) + ` ${APP_NAME}: `;
}

const factory = logger.methodFactory;
Expand Down

0 comments on commit d9ace49

Please sign in to comment.