| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- const winston = require('winston');
- const { splat, colorize, combine, timestamp, printf } = winston.format;
- function getLogLevel(config) {
- if (process.env.LOG_LEVEL) {
- return process.env.LOG_LEVEL;
- }
- if (config.__DEV__) {
- return 'debug';
- }
- if (config.__PROD__) {
- return 'error';
- }
- return 'verbose';
- }
- function getLogger(config) {
- const logLevel = getLogLevel(config);
- return winston.createLogger({
- transports: [
- new (winston.transports.Console)({
- name: 'info-console',
- colorize: !config.__PROD__,
- format: combine(
- timestamp(),
- colorize(),
- splat(),
- printf(({ timestamp: time, level, message, meta }) => {
- let metaString = '';
- if (meta) {
- metaString = JSON.stringify(meta);
- }
- return `[${time}] ${level}: ${message} ${metaString}`;
- })
- ),
- timestamp: true,
- level: logLevel
- })
- ]
- });
- }
- module.exports = { getLogger };
|