winston.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import appRoot from "app-root-path";
  2. import winston from "winston";
  3. const { combine, colorize, printf, timestamp } = winston.format;
  4. const logFormat = printf(info => {
  5. return `[${info.timestamp}] ${info.level}: ${info.message}`;
  6. });
  7. const rawFormat = printf(info => {
  8. return `[${info.timestamp}] ${info.level}: ${info.message}`;
  9. });
  10. // define the custom settings for each transport (file, console)
  11. const options = {
  12. file: {
  13. level: "info",
  14. filename: `${appRoot}/logs/app.log`,
  15. handleExceptions: true,
  16. json: true,
  17. maxsize: 5242880, // 5MB
  18. maxFiles: 5,
  19. colorize: false
  20. },
  21. errorFile: {
  22. level: "error",
  23. name: "file.error",
  24. filename: `${appRoot}/logs/error.log`,
  25. handleExceptions: true,
  26. json: true,
  27. maxsize: 5242880, // 5MB
  28. maxFiles: 100,
  29. colorize: true
  30. },
  31. console: {
  32. level: "debug",
  33. handleExceptions: true,
  34. json: false,
  35. format: combine(colorize(), rawFormat)
  36. }
  37. };
  38. // instantiate a new Winston Logger with the settings defined above
  39. export const logger = winston.createLogger({
  40. format: combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), logFormat),
  41. transports: [
  42. new winston.transports.File(options.file),
  43. new winston.transports.Console(options.console)
  44. ],
  45. exitOnError: false // do not exit on handled exceptions
  46. });
  47. // create a stream object with a 'write' function that will be used by `morgan`
  48. export const stream = {
  49. write: message => {
  50. logger.info(message);
  51. }
  52. };
  53. winston.addColors({
  54. debug: "white",
  55. error: "red",
  56. info: "green",
  57. warn: "yellow"
  58. });