file.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var layouts = require('../layouts')
  3. , async = require('async')
  4. , path = require('path')
  5. , fs = require('fs')
  6. , streams = require('../streams')
  7. , os = require('os')
  8. , eol = os.EOL || '\n'
  9. , openFiles = [];
  10. //close open files on process exit.
  11. process.on('exit', function() {
  12. openFiles.forEach(function (file) {
  13. file.end();
  14. });
  15. });
  16. /**
  17. * File Appender writing the logs to a text file. Supports rolling of logs by size.
  18. *
  19. * @param file file log messages will be written to
  20. * @param layout a function that takes a logevent and returns a string
  21. * (defaults to basicLayout).
  22. * @param logSize - the maximum size (in bytes) for a log file,
  23. * if not provided then logs won't be rotated.
  24. * @param numBackups - the number of log files to keep after logSize
  25. * has been reached (default 5)
  26. */
  27. function fileAppender (file, layout, logSize, numBackups) {
  28. var bytesWritten = 0;
  29. file = path.normalize(file);
  30. layout = layout || layouts.basicLayout;
  31. numBackups = numBackups === undefined ? 5 : numBackups;
  32. //there has to be at least one backup if logSize has been specified
  33. numBackups = numBackups === 0 ? 1 : numBackups;
  34. function openTheStream(file, fileSize, numFiles) {
  35. var stream;
  36. if (fileSize) {
  37. stream = new streams.RollingFileStream(
  38. file,
  39. fileSize,
  40. numFiles
  41. );
  42. } else {
  43. stream = fs.createWriteStream(
  44. file,
  45. { encoding: "utf8",
  46. mode: parseInt('0644', 8),
  47. flags: 'a' }
  48. );
  49. }
  50. stream.on("error", function (err) {
  51. console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
  52. });
  53. return stream;
  54. }
  55. var logFile = openTheStream(file, logSize, numBackups);
  56. // push file to the stack of open handlers
  57. openFiles.push(logFile);
  58. return function(loggingEvent) {
  59. logFile.write(layout(loggingEvent) + eol, "utf8");
  60. };
  61. }
  62. function configure(config, options) {
  63. var layout;
  64. if (config.layout) {
  65. layout = layouts.layout(config.layout.type, config.layout);
  66. }
  67. if (options && options.cwd && !config.absolute) {
  68. config.filename = path.join(options.cwd, config.filename);
  69. }
  70. return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
  71. }
  72. function shutdown(cb) {
  73. async.each(openFiles, function(file, done) {
  74. if (!file.write(eol, "utf-8")) {
  75. file.once('drain', function() {
  76. file.end(done);
  77. });
  78. } else {
  79. file.end(done);
  80. }
  81. }, cb);
  82. }
  83. exports.appender = fileAppender;
  84. exports.configure = configure;
  85. exports.shutdown = shutdown;