feat: don’t force logger to use INFO level in prod (#1218)

This commit is contained in:
AnzerWall 2017-09-14 13:11:18 +08:00 committed by TZ | 天猪
parent 95fbd47f4c
commit 5a9531abbe
7 changed files with 41 additions and 1 deletions

View File

@ -226,6 +226,7 @@ module.exports = appInfo => {
agentLogName: 'egg-agent.log',
errorLogName: 'common-error.log',
coreLogger: {},
allowDebugAtProd: true,
};
/**

View File

@ -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';
}

View File

@ -0,0 +1,13 @@
'use strict';
exports.watcher = {
// watcher 在 prod 中默认没有设置 type防止框架开发者忘记设置这里设置一下避免报错
type: 'development',
};
exports.logger = {
level: 'DEBUG',
allowDebugAtProd: false,
};
exports.keys = 'foo';

View File

@ -0,0 +1,5 @@
'use strict';
exports.logger = {
consoleLevel: 'NONE',
};

View File

@ -0,0 +1,3 @@
{
}

View File

@ -0,0 +1,3 @@
{
"name": "mock-production-app-do-not-force"
}

View File

@ -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', '');