dateFile.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. var streams = require('../streams')
  3. , layouts = require('../layouts')
  4. , path = require('path')
  5. , os = require('os')
  6. , eol = os.EOL || '\n'
  7. , openFiles = [];
  8. //close open files on process exit.
  9. process.on('exit', function() {
  10. openFiles.forEach(function (file) {
  11. file.end();
  12. });
  13. });
  14. /**
  15. * File appender that rolls files according to a date pattern.
  16. * @filename base filename.
  17. * @pattern the format that will be added to the end of filename when rolling,
  18. * also used to check when to roll files - defaults to '.yyyy-MM-dd'
  19. * @layout layout function for log messages - defaults to basicLayout
  20. */
  21. function appender(filename, pattern, alwaysIncludePattern, layout) {
  22. layout = layout || layouts.basicLayout;
  23. var logFile = new streams.DateRollingFileStream(
  24. filename,
  25. pattern,
  26. { alwaysIncludePattern: alwaysIncludePattern }
  27. );
  28. openFiles.push(logFile);
  29. return function(logEvent) {
  30. logFile.write(layout(logEvent) + eol, "utf8");
  31. };
  32. }
  33. function configure(config, options) {
  34. var layout;
  35. if (config.layout) {
  36. layout = layouts.layout(config.layout.type, config.layout);
  37. }
  38. if (!config.alwaysIncludePattern) {
  39. config.alwaysIncludePattern = false;
  40. }
  41. if (options && options.cwd && !config.absolute) {
  42. config.filename = path.join(options.cwd, config.filename);
  43. }
  44. return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout);
  45. }
  46. exports.appender = appender;
  47. exports.configure = configure;