简体   繁体   中英

node log4js maxlogsize do not work

I try to get rewrited log file less than 20mb. I have configured my log4js file appender with maxlogsize parameter as 20mb, but it doesn't work. Logger write log files greater then 20mb. So may be someone know how to fix this issue?

"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;

Fixed 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];
                        });
                    }
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM