dateFile.js 1.3 KB

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