node log4js maxlogsize不起作用

时间:2017-01-19 08:21:08

标签: node.js logging log4js-node

我尝试将重写的日志文件小于20mb。 我已经将我的log4js文件appender配置为maxmbsize参数为20mb,但它不起作用。记录器写入大于20mb的日志文件。那么可能有人知道如何解决这个问题?

"use strict";

let path = require("path");
let log4js = require('log4js');

let $depth = 10;
log4js.configure({
    appenders: [
        {
            type: "console",
            layout: {
                type    : "pattern",
                pattern : "[%d{ISO8601}] %[%p {%x{ln}} -%]\t%m",
                tokens: {
                    ln : function() {
                        var errorStack =  (new Error).stack.split("\n");
                        var tempLocation = errorStack[$depth];
                        if ( typeof tempLocation !== 'undefined' && tempLocation ) {
                            return tempLocation .replace(/^\s+at\s+(\S+)\s\((.+?)([^\/]+):(\d+):\d+\)$/, function (){
                                return arguments[1] +' '+ arguments[3] +' line '+ arguments[4];
                            });
                        }
                    }
                }
            }
        },
        {
            type: "file",
            filename: __dirname + '/../data/' + "/smartHomeServer.log",
            layout: {
                type    : "pattern",
                pattern : "[%d{ISO8601}] %[%p {%x{ln}} -%]\t%m",
                maxLogSize: 20971520,
                backups: 1,
                tokens: {
                    ln : function() {
                        var errorStack =  (new Error).stack.split("\n");
                        var tempLocation = errorStack[$depth];
                        if ( typeof tempLocation !== 'undefined' && tempLocation ) {
                            return tempLocation.replace(/^\s+at\s+(\S+)\s\((.+?)([^\/]+):(\d+):\d+\)$/, function () {
                                return arguments[1] + ' ' + arguments[3] + ' line ' + arguments[4];
                            });
                        }
                    }
                }
            }
        }
    ]
});
let log = log4js.getLogger();

module.exports = log;

1 个答案:

答案 0 :(得分:0)

修正了appender:

{
        type: "file",
        filename: __dirname + '/../data/' + "/smartHomeServer.log",
        maxLogSize: 20971520,
        backups: 1,
        layout: {
            type    : "pattern",
            pattern : "[%d{ISO8601}] %[%p {%x{ln}} -%]\t%m",
            tokens: {
                ln : function() {
                    var errorStack =  (new Error).stack.split("\n");
                    var tempLocation = errorStack[$depth];
                    if ( typeof tempLocation !== 'undefined' && tempLocation ) {
                        return tempLocation.replace(/^\s+at\s+(\S+)\s\((.+?)([^\/]+):(\d+):\d+\)$/, function () {
                            return arguments[1] + ' ' + arguments[3] + ' line ' + arguments[4];
                        });
                    }
                }
            }
        }
    }
相关问题