logger.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const path = require('path');
  3. const Logger = require('../logger');
  4. const utils = require('../utils');
  5. const FileTransport = require('../transports/file');
  6. const FileBufferTransport = require('../transports/file_buffer');
  7. const ConsoleTransport = require('../transports/console');
  8. /**
  9. * Support three transports: Console, File and JSON File
  10. */
  11. class EggLogger extends Logger {
  12. /**
  13. * @param {Object} options
  14. * - {String} dir - log base dir
  15. * - {String} file - log file, support relavie path
  16. * - {String} [encoding = utf8] - log string encoding
  17. * - {String} [level = INFO] - file log level
  18. * - {String} [consoleLevel = NONE] - console log level
  19. * - {Function} [formatter] - log format function
  20. * - {String} [jsonFile] - JSON log file
  21. * - {Boolean} [outputJSON = false] - send JSON log or not
  22. * - {Boolean} [outputJSONOnly = false] - only send JSON log
  23. * - {Boolean} [buffer] - use {@link FileBufferTransport} or not
  24. * - {String} [eol] - end of line char
  25. * - {String} [concentrateError] - whether write error logger to common-error.log, `duplicate` / `redirect` / `ignore`
  26. */
  27. constructor(options) {
  28. super(options);
  29. if (!path.isAbsolute(this.options.file)) this.options.file = path.join(this.options.dir, this.options.file);
  30. if (this.options.outputJSON === true && this.options.file) {
  31. this.options.jsonFile = this.options.file.replace(/\.log$/, '.json.log');
  32. }
  33. const EggFileTransport = this.options.buffer === true ? FileBufferTransport : FileTransport;
  34. if (!this.options.outputJSONOnly) {
  35. const fileTransport = new EggFileTransport({
  36. file: this.options.file,
  37. level: this.options.level || 'INFO',
  38. encoding: this.options.encoding,
  39. formatter: this.options.formatter,
  40. contextFormatter: this.options.contextFormatter,
  41. flushInterval: this.options.flushInterval,
  42. eol: this.options.eol,
  43. });
  44. this.set('file', fileTransport);
  45. }
  46. if (this.options.jsonFile) {
  47. const jsonFileTransport = new EggFileTransport({
  48. file: this.options.jsonFile,
  49. level: this.options.level,
  50. encoding: this.options.encoding,
  51. flushInterval: this.options.flushInterval,
  52. json: true,
  53. eol: this.options.eol,
  54. });
  55. this.set('jsonFile', jsonFileTransport);
  56. }
  57. const consoleTransport = new ConsoleTransport({
  58. level: this.options.consoleLevel,
  59. formatter: utils.consoleFormatter,
  60. contextFormatter: this.options.contextFormatter,
  61. eol: this.options.eol,
  62. });
  63. this.set('console', consoleTransport);
  64. }
  65. get level() {
  66. return this.options.level;
  67. }
  68. set level(level) {
  69. this.options.level = level;
  70. for (const transport of this.values()) {
  71. if (transport instanceof ConsoleTransport) continue;
  72. transport.level = level;
  73. }
  74. }
  75. get consoleLevel() {
  76. return this.options.consoleLevel;
  77. }
  78. set consoleLevel(level) {
  79. this.options.consoleLevel = level;
  80. for (const transport of this.values()) {
  81. if (transport instanceof ConsoleTransport) {
  82. transport.level = level;
  83. }
  84. }
  85. }
  86. get defaults() {
  87. return {
  88. file: null,
  89. encoding: 'utf8',
  90. level: 'INFO',
  91. consoleLevel: 'NONE',
  92. formatter: utils.defaultFormatter,
  93. buffer: true,
  94. outputJSON: false,
  95. outputJSONOnly: false,
  96. jsonFile: '',
  97. };
  98. }
  99. }
  100. module.exports = EggLogger;