dateFile.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const streams = require('streamroller');
  2. const os = require('os');
  3. const eol = os.EOL;
  4. /**
  5. * File appender that rolls files according to a date pattern.
  6. * @filename base filename.
  7. * @pattern the format that will be added to the end of filename when rolling,
  8. * also used to check when to roll files - defaults to '.yyyy-MM-dd'
  9. * @layout layout function for log messages - defaults to basicLayout
  10. * @timezoneOffset optional timezone offset in minutes - defaults to system local
  11. */
  12. function appender(
  13. filename,
  14. pattern,
  15. layout,
  16. options,
  17. timezoneOffset
  18. ) {
  19. // the options for file appender use maxLogSize, but the docs say any file appender
  20. // options should work for dateFile as well.
  21. options.maxSize = options.maxLogSize;
  22. const logFile = new streams.DateRollingFileStream(
  23. filename,
  24. pattern,
  25. options
  26. );
  27. logFile.on("drain", () => {
  28. process.emit("log4js:pause", false);
  29. });
  30. const app = function (logEvent) {
  31. if (!logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8")) {
  32. process.emit("log4js:pause", true);
  33. }
  34. };
  35. app.shutdown = function (complete) {
  36. logFile.write('', 'utf-8', () => {
  37. logFile.end(complete);
  38. });
  39. };
  40. return app;
  41. }
  42. function configure(config, layouts) {
  43. let layout = layouts.basicLayout;
  44. if (config.layout) {
  45. layout = layouts.layout(config.layout.type, config.layout);
  46. }
  47. if (!config.alwaysIncludePattern) {
  48. config.alwaysIncludePattern = false;
  49. }
  50. return appender(
  51. config.filename,
  52. config.pattern,
  53. layout,
  54. config,
  55. config.timezoneOffset
  56. );
  57. }
  58. module.exports.configure = configure;