/** * Created by rain on 2015/8/15. */ /*jslint node:true */ /*jslint nomen:true */ 'use strict'; const winston = require('winston'); const fs = require('fs-extra'); const path = require('path'); module.exports = function (app, config) { config.level = config.level || 'error'; config.filename = config.filename || path.join(process.cwd(), "log", "runtime.log"); let dir = path.dirname(config.filename); let logger = {}; try { fs.mkdirsSync(dir); logger = new (winston.Logger)({ level: config.level, transports: [ new (winston.transports.Console)({ colorize: 'all', timestamp: true }), new (winston.transports.File)(config) ], exitOnError: false }); } catch (err) { console.log(err); logger = new (winston.Logger)({ level: config.level, transports: [ new (winston.transports.Console)({ colorize: 'all', timestamp: true }) ], exitOnError: false }); } app.fs = app.fs || {}; app.fs.logger = logger; logger.log('debug', "[FS-LOGGER]", "Init."); return function *(ctx, next) { ctx.fs = ctx.fs || {}; ctx.fs.logger = logger; yield next(); } };