zoukankan      html  css  js  c++  java
  • node.js日志封装

    var log4js = require("log4js");
    
    function init(_log_filepath, _log_filename) {
        let logfile = _log_filepath + "/" + _log_filename + ".log";
        let logfile_error = _log_filepath + "/" + _log_filename + ".error.log";
        let layout = {
            type: "pattern",
            pattern: "[%d{yyyy-MM-dd hh:mm:ss.SSS}] [%p] %m",
        };
    
        log4js.configure({
            appenders: {
                console: {
                    type: "console",
                    layout: layout,
                },
                file: {
                    // file只是个标识字符串,可以是任意
                    type: "fileSync",
                    filename: logfile,
                    maxLogSize: 10 * 1024 * 1024, // 10MB
                    backups: 10,
                    layout: layout,
                },
                file_error: {
                    type: "fileSync",
                    filename: logfile_error,
                    maxLogSize: 1024 * 1024, // 10MB
                    backups: 5,
                    layout: layout,
                },
            },
            categories: {
                default: { appenders: ["file", "console"], level: "trace" },
                curstom_error: { appenders: ["file_error"], level: "warn" },
            },
        });
    
        var logger = log4js.getLogger("");
        var logger_curstom_error = log4js.getLogger("curstom_error");
    
        //===============================================================
        // Logger
        // 封装参考:https://stackoverflow.com/questions/56097580/override-console-logerror-with-winston-no-longer-working/56098264
    
        // v1
        //     console.trace = (...args) => logger.trace.call(logger, ...args);
        //     console.debug = (...args) => logger.debug.call(logger, ...args);
        //     console.info = (...args) => logger.info.call(logger, ...args);
        //     console.log = (...args) => logger.info.call(logger, ...args);
        //     console.warn = (...args) => logger.warn.call(logger, ...args);
        //     console.error = (...args) => logger.error.call(logger, ...args);
    
        // v2
        console.trace = function (...args) {
            logger_curstom_error.trace.call(logger_curstom_error, ...args);
            return logger.trace.call(logger, ...args);
        };
        console.debug = function (...args) {
            logger_curstom_error.debug.call(logger_curstom_error, ...args);
            return logger.debug.call(logger, ...args);
        };
        console.info = function (...args) {
            logger_curstom_error.info.call(logger_curstom_error, ...args);
            return logger.info.call(logger, ...args);
        };
        console.log = function (...args) {
            logger_curstom_error.info.call(logger_curstom_error, ...args);
            return logger.info.call(logger, ...args);
        };
        console.warn = function (...args) {
            logger_curstom_error.warn.call(logger_curstom_error, ...args);
            return logger.warn.call(logger, ...args);
        };
        console.error = function (...args) {
            logger_curstom_error.error.call(logger_curstom_error, ...args);
            return logger.error.call(logger, ...args);
        };
    }
    
    module.exports = {
        init: init,
    };

    使用:

    var logwrap = require("./util/logwrap.js");
    let filename = __filename.slice(__dirname.length + 1);
    logwrap.init("./bin", filename);
     
    后面可以正常的console.log,console.error了
     
     
     
    github项目weiqi_picture_spider(私)
  • 相关阅读:
    问题集
    第04次作业-树
    06-图
    05-查找
    04-树
    03-栈和队列
    02-线性表
    01-抽象数据类型
    C语言--总结报告
    C语言--函数嵌套
  • 原文地址:https://www.cnblogs.com/wjx0912/p/14692321.html
Copyright © 2011-2022 走看看