Created
February 19, 2019 16:56
-
-
Save jbutko/a6409b9fa837cea8e9cbb59a7ce6b229 to your computer and use it in GitHub Desktop.
React: Winston logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createLogger, format, transports } from 'winston'; | |
import { LOGGER_LEVEL, LOGGER_LEVELS, DEVELOPMENT_MODE } from './config'; | |
const formatTemplate = (message, ...data) => `(${message}) ${data}`; | |
export default class Logger { | |
constructor() { | |
this.logger = createLogger({ | |
format: format.simple(), | |
level: LOGGER_LEVEL, | |
levels: LOGGER_LEVELS, | |
transports: [new transports.Console()] | |
}); | |
} | |
error = (message, ...data) => { | |
if (DEVELOPMENT_MODE) { | |
this.logger.error(formatTemplate(message, ...data)); | |
} | |
}; | |
warn = (message, ...data) => { | |
if (DEVELOPMENT_MODE) { | |
this.logger.warn(formatTemplate(message, ...data)); | |
} | |
}; | |
info = (message, ...data) => { | |
if (DEVELOPMENT_MODE) { | |
this.logger.info(formatTemplate(message, ...data)); | |
} | |
}; | |
trace = (message, ...data) => { | |
if (DEVELOPMENT_MODE) { | |
this.logger.trace(formatTemplate(message, ...data)); | |
} | |
}; | |
debug = (message, ...data) => { | |
if (DEVELOPMENT_MODE) { | |
this.logger.debug(formatTemplate(message, ...data)); | |
} | |
}; | |
} | |
// via From https://www.reddit.com/r/reactjs/comments/art43v/best_way_to_log_in_a_react_app/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment