From 5a9531abbec83fbff08ddb6feb475f87498d2a3d Mon Sep 17 00:00:00 2001 From: AnzerWall Date: Thu, 14 Sep 2017 13:11:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20don=E2=80=99t=20force=20logger=20to=20u?= =?UTF-8?q?se=20INFO=20level=20in=20prod=20(#1218)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.default.js | 1 + lib/core/logger.js | 2 +- .../config/config.js | 13 +++++++++++++ .../config/config.unittest.js | 5 +++++ .../config/map.json | 3 +++ .../mock-production-app-do-not-force/package.json | 3 +++ test/lib/core/logger.test.js | 15 +++++++++++++++ 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/apps/mock-production-app-do-not-force/config/config.js create mode 100644 test/fixtures/apps/mock-production-app-do-not-force/config/config.unittest.js create mode 100644 test/fixtures/apps/mock-production-app-do-not-force/config/map.json create mode 100644 test/fixtures/apps/mock-production-app-do-not-force/package.json diff --git a/config/config.default.js b/config/config.default.js index e6f4973b4..0192d106e 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -226,6 +226,7 @@ module.exports = appInfo => { agentLogName: 'egg-agent.log', errorLogName: 'common-error.log', coreLogger: {}, + allowDebugAtProd: true, }; /** diff --git a/lib/core/logger.js b/lib/core/logger.js index 49162ff0e..bef76a92e 100644 --- a/lib/core/logger.js +++ b/lib/core/logger.js @@ -6,7 +6,7 @@ module.exports = function createLoggers(app) { const loggerConfig = app.config.logger; loggerConfig.type = app.type; - if (app.config.env === 'prod' && loggerConfig.level === 'DEBUG') { + if (app.config.env === 'prod' && loggerConfig.level === 'DEBUG' && loggerConfig.allowDebugAtProd === true) { loggerConfig.level = 'INFO'; } diff --git a/test/fixtures/apps/mock-production-app-do-not-force/config/config.js b/test/fixtures/apps/mock-production-app-do-not-force/config/config.js new file mode 100644 index 000000000..75906416d --- /dev/null +++ b/test/fixtures/apps/mock-production-app-do-not-force/config/config.js @@ -0,0 +1,13 @@ +'use strict'; + +exports.watcher = { + // watcher 在 prod 中默认没有设置 type,防止框架开发者忘记设置,这里设置一下,避免报错 + type: 'development', +}; + +exports.logger = { + level: 'DEBUG', + allowDebugAtProd: false, +}; + +exports.keys = 'foo'; diff --git a/test/fixtures/apps/mock-production-app-do-not-force/config/config.unittest.js b/test/fixtures/apps/mock-production-app-do-not-force/config/config.unittest.js new file mode 100644 index 000000000..d108222fe --- /dev/null +++ b/test/fixtures/apps/mock-production-app-do-not-force/config/config.unittest.js @@ -0,0 +1,5 @@ +'use strict'; + +exports.logger = { + consoleLevel: 'NONE', +}; diff --git a/test/fixtures/apps/mock-production-app-do-not-force/config/map.json b/test/fixtures/apps/mock-production-app-do-not-force/config/map.json new file mode 100644 index 000000000..179713338 --- /dev/null +++ b/test/fixtures/apps/mock-production-app-do-not-force/config/map.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/test/fixtures/apps/mock-production-app-do-not-force/package.json b/test/fixtures/apps/mock-production-app-do-not-force/package.json new file mode 100644 index 000000000..6e83126c2 --- /dev/null +++ b/test/fixtures/apps/mock-production-app-do-not-force/package.json @@ -0,0 +1,3 @@ +{ + "name": "mock-production-app-do-not-force" +} diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.js index 24d1a89e7..4a2a0131a 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.js @@ -29,6 +29,21 @@ describe('test/lib/core/logger.test.js', () => { assert(app.config.logger.disableConsoleAfterReady === true); }); + it('should got right level on prod env when set allowDebugAtProd to false', function* () { + mm.env('prod'); + mm(process.env, 'EGG_LOG', ''); + mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app-do-not-force/config')); + app = utils.app('apps/mock-production-app-do-not-force'); + yield app.ready(); + + assert(app.config.logger.allowDebugAtProd === false); + + assert(app.logger.get('file').options.level === Logger.DEBUG); + assert(app.logger.get('console').options.level === Logger.INFO); + assert(app.coreLogger.get('file').options.level === Logger.DEBUG); + assert(app.coreLogger.get('console').options.level === Logger.INFO); + }); + it('should got right level on local env', function* () { mm.env('local'); mm(process.env, 'EGG_LOG', '');