Skip to content

Commit

Permalink
feat: auto set custom logger with onelogger
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 21, 2024
1 parent 1e29b79 commit 7bd89ac
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
18 changes: 14 additions & 4 deletions lib/core/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const Loggers = require('egg-logger').EggLoggers;
const { EggLoggers } = require('egg-logger');
const { setCustomLogger } = require('onelogger');

module.exports = function createLoggers(app) {
const loggerConfig = app.config.logger;
Expand All @@ -11,14 +10,25 @@ module.exports = function createLoggers(app) {
loggerConfig.level = 'INFO';
}

const loggers = new Loggers(app.config);
const loggers = new EggLoggers(app.config);

// won't print to console after started, except for local and unittest
app.ready(() => {
if (loggerConfig.disableConsoleAfterReady) {
loggers.disableConsole();
}
});

// set global logger
for (const loggerName of Object.keys(loggers)) {
setCustomLogger(loggerName, loggers[loggerName]);
}
// reset global logger on beforeClose hook
app.beforeClose(() => {
for (const loggerName of Object.keys(loggers)) {
setCustomLogger(loggerName, undefined);
}
});
loggers.coreLogger.info('[egg:logger] init all loggers with options: %j', loggerConfig);

return loggers;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"koa-override": "^3.0.0",
"ms": "^2.1.3",
"on-finished": "^2.4.1",
"onelogger": "^1.0.0",
"sendmessage": "^2.0.0",
"urllib": "^2.33.0",
"urllib-next": "^3.9.0",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/custom-logger/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { getCustomLogger, getLogger } = require('onelogger');

module.exports = app => {
app.get('/', async ctx => {
const myLogger = getCustomLogger('myLogger', 'custom-logger-label');
const logger = getLogger();
myLogger.info('hello myLogger');
logger.warn('hello logger');
ctx.body = { ok: true };
});
};
4 changes: 1 addition & 3 deletions test/fixtures/apps/custom-logger/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const path = require('path');
const path = require('node:path');

module.exports = info => {
return {
Expand Down
36 changes: 31 additions & 5 deletions test/lib/core/logger.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const mm = require('egg-mock');
const Logger = require('egg-logger');
const utils = require('../../utils');

describe('test/lib/core/logger.test.js', () => {
let app;
afterEach(mm.restore);
afterEach(() => utils.sleep(5000).then(() => app.close()));
afterEach(async () => {
if (app) {
await utils.sleep(3000);
await app.close();
app = null;
}
await mm.restore();
});

it('should got right default config on prod env', async () => {
mm.env('prod');
Expand Down Expand Up @@ -253,4 +259,24 @@ describe('test/lib/core/logger.test.js', () => {
});
});
});

describe('onelogger', () => {
let app;
before(() => {
app = utils.app('apps/custom-logger');
return app.ready();
});
after(() => app.close());

it('should work with onelogger', async () => {
await app.httpRequest()
.get('/')
.expect({
ok: true,
})
.expect(200);
app.expectLog('[custom-logger-label] hello myLogger', 'myLogger');
app.expectLog('hello logger');
});
});
});

0 comments on commit 7bd89ac

Please sign in to comment.