Skip to content

Commit

Permalink
feat: add config.logger.disableConsoleAfterReady (#1001)
Browse files Browse the repository at this point in the history
let app can change the logger config
  • Loading branch information
fengmk2 authored and popomore committed Jun 12, 2017
1 parent 4890eda commit f1b510c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ module.exports = appInfo => {
* @property {String} encoding - log file encloding, defaults to utf8
* @property {String} level - default log level, could be: DEBUG, INFO, WARN, ERROR or NONE, defaults to INFO in production
* @property {String} consoleLevel - log level of stdout, defaults to INFO in local serverEnv, defaults to WARN in unittest, defaults to NONE elsewise
* @property {Boolean} disableConsoleAfterReady - disable logger console after app ready. defaults to `false` on local and unittest env, others is `true`.
* @property {Boolean} outputJSON - log as JSON or not, defaults to false
* @property {Boolean} buffer - if enabled, flush logs to disk at a certain frequency to improve performance, defaults to true
* @property {String} errorLogName - file name of errorLogger
Expand All @@ -213,6 +214,7 @@ module.exports = appInfo => {
env: appInfo.env,
level: 'INFO',
consoleLevel: 'INFO',
disableConsoleAfterReady: appInfo.env !== 'local' && appInfo.env !== 'unittest',
outputJSON: false,
buffer: true,
appLogName: `${appInfo.name}-web.log`,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function createLoggers(app) {

// won't print to console after started, except for local and unittest
app.ready(() => {
if (app.config.env !== 'local' && app.config.env !== 'unittest') {
if (loggerConfig.disableConsoleAfterReady) {
loggers.disableConsole();
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/lib/core/config/config.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');

const mm = require('egg-mock');
const utils = require('../../../utils');

Expand All @@ -17,5 +16,6 @@ describe('test/lib/core/config/config.test.js', () => {

it('should return config.name', () => {
assert(app.config.name === 'demo');
assert(app.config.logger.disableConsoleAfterReady === false);
});
});
3 changes: 2 additions & 1 deletion test/lib/core/loader/config_loader.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');

const path = require('path');
const mm = require('egg-mock');
const utils = require('../../../utils');
Expand Down Expand Up @@ -31,6 +30,7 @@ describe('test/lib/core/loader/config_loader.test.js', () => {
app = utils.app('apps/demo');
yield app.ready();
assert.deepEqual(app.config.logger.dir, utils.getFilepath('apps/demo/logs/demo'));
assert(app.config.logger.disableConsoleAfterReady === false);
});

it('should get logger dir when default', function* () {
Expand All @@ -39,5 +39,6 @@ describe('test/lib/core/loader/config_loader.test.js', () => {
app = utils.app('apps/demo');
yield app.ready();
assert.deepEqual(app.config.logger.dir, path.join(home, 'logs/demo'));
assert(app.config.logger.disableConsoleAfterReady === true);
});
});
9 changes: 6 additions & 3 deletions test/lib/core/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const mm = require('egg-mock');
const request = require('supertest');
const Logger = require('egg-logger');
const sleep = require('mz-modules/sleep');

const utils = require('../../utils');

describe('test/lib/core/logger.test.js', () => {
Expand All @@ -28,6 +27,7 @@ describe('test/lib/core/logger.test.js', () => {
assert(app.logger.get('console').options.level === Logger.INFO);
assert(app.coreLogger.get('file').options.level === Logger.INFO);
assert(app.coreLogger.get('console').options.level === Logger.INFO);
assert(app.config.logger.disableConsoleAfterReady === true);
});

it('should got right level on local env', function* () {
Expand All @@ -40,6 +40,7 @@ describe('test/lib/core/logger.test.js', () => {
assert(app.logger.get('console').options.level === Logger.INFO);
assert(app.coreLogger.get('file').options.level === Logger.INFO);
assert(app.coreLogger.get('console').options.level === Logger.WARN);
assert(app.config.logger.disableConsoleAfterReady === false);
});

it('should set EGG_LOG level on local env', function* () {
Expand All @@ -52,7 +53,7 @@ describe('test/lib/core/logger.test.js', () => {
assert(app.logger.get('console').options.level === Logger.ERROR);
assert(app.coreLogger.get('file').options.level === Logger.INFO);
assert(app.coreLogger.get('console').options.level === Logger.ERROR);
return app.ready();
assert(app.config.logger.disableConsoleAfterReady === false);
});

it('should got right config on unittest env', function* () {
Expand All @@ -65,7 +66,7 @@ describe('test/lib/core/logger.test.js', () => {
assert(app.logger.get('console').options.level === Logger.WARN);
assert(app.coreLogger.get('file').options.level === Logger.INFO);
assert(app.coreLogger.get('console').options.level === Logger.WARN);
return app.ready();
assert(app.config.logger.disableConsoleAfterReady === false);
});

it('should set log.consoleLevel to env.EGG_LOG', function* () {
Expand All @@ -82,6 +83,7 @@ describe('test/lib/core/logger.test.js', () => {
mm(process.env, 'EGG_LOG', 'NONE');
app = utils.app('apps/nobuffer-logger');
yield app.ready();
assert(app.config.logger.disableConsoleAfterReady === false);

const ctx = app.mockContext();
const logfile = path.join(app.config.logger.dir, 'common-error.log');
Expand All @@ -102,6 +104,7 @@ describe('test/lib/core/logger.test.js', () => {
app = utils.app('apps/mock-production-app');
yield app.ready();

assert(app.config.logger.disableConsoleAfterReady === true);
const ctx = app.mockContext();
const logfile = path.join(app.config.logger.dir, 'common-error.log');
// app.config.logger.buffer.should.equal(true);
Expand Down

0 comments on commit f1b510c

Please sign in to comment.