logger.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const winston = require('winston');
  2. const { splat, colorize, combine, timestamp, printf } = winston.format;
  3. function getLogLevel(config) {
  4. if (process.env.LOG_LEVEL) {
  5. return process.env.LOG_LEVEL;
  6. }
  7. if (config.__DEV__) {
  8. return 'debug';
  9. }
  10. if (config.__PROD__) {
  11. return 'error';
  12. }
  13. return 'verbose';
  14. }
  15. function getLogger(config) {
  16. const logLevel = getLogLevel(config);
  17. return winston.createLogger({
  18. transports: [
  19. new (winston.transports.Console)({
  20. name: 'info-console',
  21. colorize: !config.__PROD__,
  22. format: combine(
  23. timestamp(),
  24. colorize(),
  25. splat(),
  26. printf(({ timestamp: time, level, message, meta }) => {
  27. let metaString = '';
  28. if (meta) {
  29. metaString = JSON.stringify(meta);
  30. }
  31. return `[${time}] ${level}: ${message} ${metaString}`;
  32. })
  33. ),
  34. timestamp: true,
  35. level: logLevel
  36. })
  37. ]
  38. });
  39. }
  40. module.exports = { getLogger };