diff --git a/.autod.conf b/.autod.conf index e178168..6377a01 100644 --- a/.autod.conf +++ b/.autod.conf @@ -15,7 +15,6 @@ module.exports = { 'egg-ci', ], semver: [ - 'egg-bin@1', 'koa@1', 'debug@2', ], diff --git a/lib/egg/logger.js b/lib/egg/logger.js index 528bba3..8b2a5bb 100644 --- a/lib/egg/logger.js +++ b/lib/egg/logger.js @@ -1,5 +1,6 @@ 'use strict'; +const path = require('path'); const Logger = require('../logger'); const utils = require('../utils'); const FileTransport = require('../transports/file'); @@ -14,7 +15,8 @@ class EggLogger extends Logger { /** * @constructor * @param {Object} options - * - {String} file - log file + * - {String} dir - log base dir + * - {String} file - log file, support relavie path * - {String} [encoding = utf8] - log string encoding * - {String} [level = INFO] - file log level * - {String} [consoleLevel = NONE] - console log level @@ -28,6 +30,8 @@ class EggLogger extends Logger { constructor(options) { super(options); + if (!path.isAbsolute(this.options.file)) this.options.file = path.join(this.options.dir, this.options.file); + if (this.options.outputJSON === true && this.options.file) { this.options.jsonFile = this.options.file.replace(/\.log$/, '.json.log'); } diff --git a/lib/egg/loggers.js b/lib/egg/loggers.js index ab69a02..b349644 100644 --- a/lib/egg/loggers.js +++ b/lib/egg/loggers.js @@ -1,6 +1,5 @@ 'use strict'; -const path = require('path'); const assert = require('assert'); const debug = require('debug')('egg:logger'); const utils = require('../utils'); @@ -66,28 +65,28 @@ class Loggers extends Map { assert(loggerConfig.errorLogName, 'should pass config.logger.errorLogName'); const errorLogger = new ErrorLogger(utils.assign({}, loggerConfig, { - file: path.join(loggerConfig.dir, loggerConfig.errorLogName), + file: loggerConfig.errorLogName, })); this.set('errorLogger', errorLogger); if (loggerConfig.type === 'agent') { const logger = new Logger(utils.assign({}, loggerConfig, { - file: path.join(loggerConfig.dir, loggerConfig.agentLogName), + file: loggerConfig.agentLogName, })); this.set('logger', logger); const coreLogger = new Logger(utils.assign({}, loggerConfig, loggerConfig.coreLogger, { - file: path.join(loggerConfig.dir, loggerConfig.agentLogName), + file: loggerConfig.agentLogName, })); this.set('coreLogger', coreLogger); } else { const logger = new Logger(utils.assign({}, loggerConfig, { - file: path.join(loggerConfig.dir, loggerConfig.appLogName), + file: loggerConfig.appLogName, })); this.set('logger', logger); const coreLogger = new Logger(utils.assign({}, loggerConfig, loggerConfig.coreLogger, { - file: path.join(loggerConfig.dir, loggerConfig.coreLogName), + file: loggerConfig.coreLogName, })); this.set('coreLogger', coreLogger); } diff --git a/package.json b/package.json index 5bb7449..38d3400 100644 --- a/package.json +++ b/package.json @@ -17,15 +17,16 @@ "autod": "^3.0.1", "beautify-benchmark": "^0.2.4", "benchmark": "^2.1.4", - "coffee": "^5.1.0", - "egg-bin": "^1.11.1", - "egg-ci": "^1.10.0", - "eslint": "^5.9.0", + "coffee": "^5.2.1", + "egg-bin": "^4.9.0", + "egg-ci": "^1.11.0", + "eslint": "^5.11.1", "eslint-config-egg": "^7.1.0", "heapdump": "^0.3.12", "ko-sleep": "^1.0.3", "koa": "^1.6.2", "mm": "^2.4.1", + "mz": "^2.7.0", "mz-modules": "^2.1.0", "rimraf": "^2.6.2", "should": "^13.2.3", diff --git a/test/lib/egg/custom_logger.test.js b/test/lib/egg/custom_logger.test.js index aa39fd4..37b1682 100644 --- a/test/lib/egg/custom_logger.test.js +++ b/test/lib/egg/custom_logger.test.js @@ -1,30 +1,38 @@ 'use strict'; -require('should'); -const fs = require('fs'); +const assert = require('assert'); +const { fs } = require('mz'); const path = require('path'); const coffee = require('coffee'); -const rimraf = require('rimraf'); +const { rimraf } = require('mz-modules'); describe('test/egg/custom_logger.test.js', () => { const loggerFile = path.join(__dirname, '../../fixtures/egg_custom_logger.js'); - const filepath = path.join(__dirname, '../../fixtures/tmp/a.log'); + const tmpDir = path.join(__dirname, '../../fixtures/tmp'); + const filePath = path.join(tmpDir, '/a.log'); - afterEach(() => { - rimraf.sync(path.dirname(filepath)); - }); + beforeEach(() => rimraf(path.dirname(filePath))); + afterEach(() => rimraf(path.dirname(filePath))); - it('should format work', done => { + it('should format work', function*() { const options = { - file: filepath, + file: filePath, level: 'WARN', }; - coffee.fork(loggerFile, [ JSON.stringify(options) ]) - .end(() => { - fs.readFileSync(filepath, 'utf8') - .should.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/); - done(); - }); + yield coffee.fork(loggerFile, [ JSON.stringify(options) ]).end(); + const log = yield fs.readFile(filePath, 'utf-8'); + assert(log.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/)); }); + it('should support relative path', function* () { + const options = { + dir: tmpDir, + file: 'relative.log', + level: 'WARN', + }; + yield coffee.fork(loggerFile, [ JSON.stringify(options) ]).end(); + + const log = yield fs.readFile(path.join(tmpDir, options.file), 'utf-8'); + assert(log.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/)); + }); }); diff --git a/test/lib/egg/loggers.test.js b/test/lib/egg/loggers.test.js index fea8999..f5bd122 100644 --- a/test/lib/egg/loggers.test.js +++ b/test/lib/egg/loggers.test.js @@ -16,6 +16,7 @@ describe('test/egg/loggers.test.js', () => { const cLog = path.join(tmp, 'c.log'); const dLog = path.join(tmp, 'd.log'); const eLog = path.join(tmp, 'e.log'); + const fLog = 'f.log'; describe('application', () => { let loggers; @@ -53,6 +54,9 @@ describe('test/egg/loggers.test.js', () => { file: eLog, concentrateError: 'ignore', }, + fLogger: { + file: fLog, + }, }, }); }); @@ -158,6 +162,15 @@ describe('test/egg/loggers.test.js', () => { content.should.not.containEql('\n'); }); + it('should fLogger log to f.log with relative config', function*() { + loggers.fLogger.info('fLogger info foo'); + + yield sleep(10); + + const content = fs.readFileSync(path.join(tmp, 'f.log'), 'utf8'); + content.should.containEql('fLogger info foo'); + }); + it('reload all logger', done => { loggers.reload(); setTimeout(done, 500);