all files / direwolf/lib/core/ logger.js

91.67% Statements 11/12
50% Branches 1/2
100% Functions 1/1
90.91% Lines 10/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                   
/**
 * Created by zdy on 2016/9/28.
 * 应用日志类
 */
 
'use strict';
 
const log4js = require('log4js');
const Path = require('path');
const fs = require('fs');
const direwolfConfig = require('../config/config');
const BASE_DIR = process.cwd();
 
class Logger {
    constructor(name) {
 
        // 日志目录
        this.dir = Path.join(BASE_DIR, direwolfConfig.logDir);
 
        // 确保日志目录存在
        Iif (!fs.existsSync(this.dir)) {
            fs.mkdirSync(this.dir);
        }
 
        log4js.configure({
            appenders: [
                // 输出到文件
                {
                    type: 'dateFile',
                    filename: Path.join(this.dir, 'log.log'),
                    pattern: '-yyyy-MM-dd'
                },
                // 输出到控制台
                {
                    type: 'console'
                }
            ]
        });
 
        this.logger = log4js.getLogger(name);
    }
}
 
module.exports = name => (new Logger(name)).logger;