Skip to content

Commit

Permalink
馃摝 NEW: Upgrade egg-logger v3 to enable localStorage (#5085)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 11, 2022
1 parent c76e16c commit e94c7df
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const proto = module.exports = {
* @return {Logger} logger
*/
getLogger(name) {
if (this.app.config.logger.enableFastContextLogger) {
return this.app.getLogger(name);
}
let cache = this[CONTEXT_LOGGERS];
if (!cache) {
cache = this[CONTEXT_LOGGERS] = {};
Expand Down
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ module.exports = appInfo => {
* @property {Object} coreLogger - custom config of coreLogger
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to false
* @property {Boolean} enablePerformanceTimer - using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to false. e.g.: logger will set 1.456ms instead of 1ms.
* @property {Boolean} enableFastContextLogger - using the app logger instead of EggContextLogger, defaults to false
*/
config.logger = {
dir: path.join(appInfo.root, 'logs', appInfo.name),
Expand All @@ -275,6 +276,7 @@ module.exports = appInfo => {
coreLogger: {},
allowDebugAtProd: false,
enablePerformanceTimer: false,
enableFastContextLogger: false,
};

/**
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ declare module 'egg' {
disableConsoleAfterReady?: boolean;
/** using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to `false`. e.g.: logger will set 1.456ms instead of 1ms. */
enablePerformanceTimer?: boolean;
/** using the app logger instead of EggContextLogger, defaults to `false` */
enableFastContextLogger?: boolean;
}

/** Custom Loader Configuration */
Expand Down Expand Up @@ -343,6 +345,7 @@ declare module 'egg' {
* @property {Object} coreLogger - custom config of coreLogger
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to false
* @property {Boolean} enablePerformanceTimer - using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to false. e.g.: logger will set 1.456ms instead of 1ms.
* @property {Boolean} enableFastContextLogger - using the app logger instead of EggContextLogger, defaults to false
*/
logger: EggLoggerConfig;

Expand Down
1 change: 1 addition & 0 deletions lib/core/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Loggers = require('egg-logger').EggLoggers;
module.exports = function createLoggers(app) {
const loggerConfig = app.config.logger;
loggerConfig.type = app.type;
loggerConfig.localStorage = app.ctxStorage;

if (app.config.env === 'prod' && loggerConfig.level === 'DEBUG' && !loggerConfig.allowDebugAtProd) {
loggerConfig.level = 'INFO';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"egg-errors": "^2.3.1",
"egg-i18n": "^2.1.1",
"egg-jsonp": "^2.0.0",
"egg-logger": "^2.9.0",
"egg-logger": "^3.0.1",
"egg-logrotator": "^3.1.0",
"egg-multipart": "^3.1.0",
"egg-onerror": "^2.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exports.index = async ctx => {
ctx.logger.info('enableFastContextLogger: %s', ctx.app.config.logger.enableFastContextLogger);
ctx.body = {
enableFastContextLogger: ctx.app.config.logger.enableFastContextLogger,
};
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-enableFastContextLogger/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = app => {
app.get('/', 'home.index');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.keys = 'tracer-demo keys';

exports.logger = {
enableFastContextLogger: true,
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-enableFastContextLogger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "demo_app"
}
14 changes: 14 additions & 0 deletions test/fixtures/apps/tracer-demo/app/controller/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = app => {
return {
async index(ctx) {
const r = await app.curl(ctx.query.url, {
dataType: 'json',
});
app.logger.info('app logger support traceId');
ctx.body = {
url: ctx.query.url,
data: r.data,
};
},
};
};
1 change: 1 addition & 0 deletions test/fixtures/apps/tracer-demo/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module.exports = app => {
app.get('/', 'home.index');
app.get('/foo', 'foo.index');
};
20 changes: 20 additions & 0 deletions test/lib/core/httpclient_tracer_demo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ describe('test/lib/core/httpclient_tracer_demo.test.js', () => {
})
.expect(200);
});

it('should app logger support localStorage by default', async () => {
const traceId = 'mock-traceId-123123';
app.mockContext({
tracer: {
traceId,
},
});
await app.httpRequest()
.get('/foo?url=' + encodeURIComponent(url + '/get_headers'))
.expect(res => {
assert(res.body.url === url + '/get_headers');
assert(res.body.data['x-request-id']);
assert(res.body.data['x-request-id'].startsWith('anonymous-'));
})
.expect(200);
// wait for windows log store
if (process.platform === 'win32') await utils.sleep(2000);
app.expectLog(/ INFO \d+ \[-\/127.0.0.1\/mock-traceId-123123\/\d+ms GET \/foo\?url=http%3A%2F%2F127.0.0.1%3A\d+%2Fget_headers] app logger support traceId/);
});
});
17 changes: 17 additions & 0 deletions test/lib/core/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ describe('test/lib/core/logger.test.js', () => {
assert(app.agent.logger.options.file === app.agent.coreLogger.options.file);
});

it('should config.logger.enableFastContextLogger = true work', async () => {
app = utils.app('apps/app-enableFastContextLogger');
await app.ready();
app.mockContext({
tracer: {
traceId: 'mock-trace-id-123',
},
});
await app.httpRequest()
.get('/')
.expect(200)
.expect({
enableFastContextLogger: true,
});
app.expectLog(/ INFO \d+ \[-\/127\.0\.0\.1\/mock-trace-id-123\/\d+ms GET \/] enableFastContextLogger: true/);
});

describe('logger.level = DEBUG', () => {
let app;
before(() => {
Expand Down

0 comments on commit e94c7df

Please sign in to comment.