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};
  • 相关阅读:
    MySQL5.7二进制安装及多实例
    MySQL5.7版本的yum安装方式
    PHP安装
    MySQL5.6安装部署及多实例主从
    Prometheus监控MySQL和Linux主机结合Grafana出图
    MySQL5.7源码安装(编译)
    MySQL基本管理
    WC框架
    .NET 调用虚方法2 转
    .NET 调用虚方法1 转
  • 原文地址:https://www.cnblogs.com/wkzhao/p/10551743.html
Copyright © 2011-2022 走看看