Skip to content
This repository
Browse code

Working date rolling file appender.

  • Loading branch information...
commit 2b889fe7764fb2e10d7bd1f0bfc28c1561e155a4 1 parent 9ac61e3
Gareth Jones authored
49  lib/appenders/dateFile.js
... ...
@@ -0,0 +1,49 @@
  1
+var streams = require('../streams'),
  2
+    layouts = require('../layouts'),
  3
+    path = require('path'),
  4
+    os = require('os'),
  5
+    eol = os.EOL || '\n',
  6
+    openFiles = [];
  7
+
  8
+//close open files on process exit.
  9
+process.on('exit', function() {
  10
+    openFiles.forEach(function (file) {
  11
+        file.end();
  12
+    });
  13
+});
  14
+
  15
+/**
  16
+ * File appender that rolls files according to a date pattern.
  17
+ * @filename base filename.
  18
+ * @pattern the format that will be added to the end of filename when rolling,
  19
+ *          also used to check when to roll files - defaults to '.yyyy-MM-dd'
  20
+ * @layout layout function for log messages - defaults to basicLayout
  21
+ */
  22
+function appender(filename, pattern, layout) {
  23
+    layout = layout || layouts.basicLayout;
  24
+
  25
+    var logFile = new streams.BufferedWriteStream(new streams.DateRollingFileStream(filename, pattern));
  26
+    openFiles.push(logFile);
  27
+
  28
+    return function(logEvent) {
  29
+        logFile.write(layout(logEvent) + eol, "utf8");
  30
+    };
  31
+
  32
+}
  33
+
  34
+function configure(config, options) {
  35
+    var layout;
  36
+
  37
+    if (config.layout) {
  38
+	layout = layouts.layout(config.layout.type, config.layout);
  39
+    }
  40
+
  41
+    if (options && options.cwd && !config.absolute) {
  42
+        config.filename = path.join(options.cwd, config.filename);
  43
+    }
  44
+
  45
+    return appender(config.filename, config.pattern, layout);
  46
+}
  47
+
  48
+exports.appender = appender;
  49
+exports.configure = configure;
98  test/dateFileAppender-test.js
... ...
@@ -0,0 +1,98 @@
  1
+var vows = require('vows'),
  2
+    assert = require('assert'),
  3
+    path = require('path'),
  4
+    fs = require('fs'),
  5
+    log4js = require('../lib/log4js');
  6
+
  7
+function removeFile(filename) {
  8
+    return function() {
  9
+        fs.unlink(path.join(__dirname, filename), function(err) {
  10
+            if (err) {
  11
+                console.log("Could not delete ", filename, err);
  12
+            }
  13
+        });
  14
+    };
  15
+}
  16
+
  17
+vows.describe('../lib/appenders/dateFile').addBatch({
  18
+    'appender': {
  19
+        'adding multiple dateFileAppenders': {
  20
+            topic: function () {
  21
+                var listenersCount = process.listeners('exit').length,
  22
+                    dateFileAppender = require('../lib/appenders/dateFile'),
  23
+                    count = 5,
  24
+                    logfile;
  25
+
  26
+                while (count--) {
  27
+                    logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
  28
+                    log4js.addAppender(dateFileAppender.appender(logfile));
  29
+                }
  30
+
  31
+                return listenersCount;
  32
+            },
  33
+            teardown: function() {
  34
+                removeFile('datefa-default-test0.log')();
  35
+                removeFile('datefa-default-test1.log')();
  36
+                removeFile('datefa-default-test2.log')();
  37
+                removeFile('datefa-default-test3.log')();
  38
+                removeFile('datefa-default-test4.log')();
  39
+            },
  40
+
  41
+            'should only add one `exit` listener': function (initialCount) {
  42
+                assert.equal(process.listeners('exit').length, initialCount + 1);
  43
+            }
  44
+        },
  45
+
  46
+        'with default settings': {
  47
+            topic: function() {
  48
+                var that = this,
  49
+                    testFile = path.join(__dirname, 'date-appender-default.log'),
  50
+                    appender = require('../lib/appenders/dateFile').appender(testFile),
  51
+                    logger = log4js.getLogger('default-settings');
  52
+                log4js.clearAppenders();
  53
+                log4js.addAppender(appender, 'default-settings');
  54
+
  55
+                logger.info("This should be in the file.");
  56
+
  57
+                setTimeout(function() {
  58
+                    fs.readFile(testFile, "utf8", that.callback);
  59
+                }, 100);
  60
+
  61
+            },
  62
+            teardown: removeFile('date-appender-default.log'),
  63
+
  64
+            'should write to the file': function(contents) {
  65
+                assert.include(contents, 'This should be in the file');
  66
+            },
  67
+
  68
+            'should use the basic layout': function(contents) {
  69
+                assert.match(contents, /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /);
  70
+            }
  71
+        }
  72
+
  73
+    }
  74
+}).addBatch({
  75
+    'configure': {
  76
+        'with dateFileAppender': {
  77
+	    topic: function() {
  78
+	        var log4js = require('../lib/log4js')
  79
+                  , logger;
  80
+	        //this config file defines one file appender (to ./date-file-test.log)
  81
+	        //and sets the log level for "tests" to WARN
  82
+                log4js.configure('test/with-dateFile.json');
  83
+                logger = log4js.getLogger('tests');
  84
+	        logger.info('this should not be written to the file');
  85
+	        logger.warn('this should be written to the file');
  86
+
  87
+                fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
  88
+	    },
  89
+            teardown: removeFile('date-file-test.log'),
  90
+
  91
+	    'should load appender configuration from a json file': function(err, contents) {
  92
+	        assert.include(contents, 'this should be written to the file\n');
  93
+                assert.equal(contents.indexOf('this should not be written to the file'), -1);
  94
+	    }
  95
+        }
  96
+
  97
+    }
  98
+}).exportTo(module);
17  test/with-dateFile.json
... ...
@@ -0,0 +1,17 @@
  1
+{
  2
+  "appenders": [
  3
+    { 
  4
+      "category": "tests", 
  5
+      "type": "dateFile", 
  6
+      "filename": "test/date-file-test.log",
  7
+      "pattern": "-from-MM-dd", 
  8
+      "layout": { 
  9
+        "type": "messagePassThrough" 
  10
+      } 
  11
+    }
  12
+  ],
  13
+  
  14
+  "levels": {
  15
+    "tests":  "WARN"
  16
+  }
  17
+}

0 notes on commit 2b889fe

Please sign in to comment.
Something went wrong with that request. Please try again.