zoukankan      html  css  js  c++  java
  • ts中使用winston打印日志

    import * as winston from 'winston';
    // 引入config模块,log相关配置放在配置文件下
    import config from 'config';
    
    class Logger {
    
        private readonly consoleLevel: string;
        private readonly fileLevel: string;
    
        constructor (){
            const logConfig = JSON.parse(JSON.stringify(config.get('log-config')));
            this.consoleLevel = logConfig.consoleLevel;
            this.fileLevel = logConfig.fileLevel;
    
            this.addLogger('controller');
            this.addLogger('service');
        }
    
        public getLogger (category: string): winston.Logger {return winston.loggers.get(category);
        }
    
        // 添加多个logger
        private addLogger (category: string){
            winston.loggers.add(category, {
               format: winston.format.combine(
                   winston.format.label({ label: category}),
                   winston.format.timestamp(),
                   winston.format.printf((info) => {
                       return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`;
                   })
               ),
    
               transports: [
                   // level设置打印的最低级别
                   new winston.transports.Console({ level: this.consoleLevel }),
                   new winston.transports.File({
                       level: this.fileLevel,
                       filename: __dirname + `/logs/${category}.log`,
                       maxsize: 5242880,
                       maxFiles: 20
                   })
               ]
           });
        }
    
    }
    
    const LogFactory = new Logger();
    
    export {LogFactory};
  • 相关阅读:
    [Unity菜鸟] 摄像头
    [Unity菜鸟] Final IK
    谷歌 值得关注的网站
    [Unity菜鸟] 射线
    [Unity菜鸟] Character控制移动
    [Unity菜鸟] FBX模型动画提取
    [Unity菜鸟] Time
    [Unity菜鸟] 产生各不相同的随机数
    [Unity菜鸟] 材质
    反调试:检测进程名
  • 原文地址:https://www.cnblogs.com/wkzhao/p/10551743.html
Copyright © 2011-2022 走看看