Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reload loggers after rotating #5

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = agent => {
// reload logger to new fd after rotating
agent.messenger.on('log-reload', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

挂在 agent 上的 logger 也需要 reload

agent.loggers.reload('got log-reload message');
agent.coreLogger.info('[egg-logrotator] agent logger reload: got log-reload message');
});
};
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = app => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个就是 codecov/patch

// reload logger to new fd after rotating
app.messenger.on('log-reload', () => {
app.loggers.reload('got log-reload message');
app.coreLogger.info('[egg-logrotator] app logger reload: got log-reload message');
});
};
6 changes: 6 additions & 0 deletions test/fixtures/logger-reload/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = agent => {
agent.coreLogger.warn('agent warn');
agent.coreLogger.error('agent error');
};
22 changes: 22 additions & 0 deletions test/fixtures/logger-reload/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const path = require('path');


module.exports = app => {
app.get('/log', function* () {
this.logger.warn('%s %s', this.method, this.path);
this.logger.error(new Error('error'));
this.body = {
method: this.method,
path: this.path,
};
});

app.get('/rotate', function* () {
const schedule = path.join(__dirname, '../../../../app/schedule/rotate_by_file');
console.log(schedule);
yield app.runSchedule(schedule);
this.body = 'done';
});
};
3 changes: 3 additions & 0 deletions test/fixtures/logger-reload/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "logger-reload"
}
55 changes: 55 additions & 0 deletions test/logrotator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const mm = require('egg-mock');
const fs = require('fs');
const glob = require('glob');
const moment = require('moment');
const request = require('supertest');


require('should');

Expand Down Expand Up @@ -239,6 +241,59 @@ describe('test/logrotator.test.js', () => {
});
});

describe('reload logger', () => {
let app;
const baseDir = path.join(__dirname, 'fixtures/logger-reload');
before(() => {
app = mm.cluster({
baseDir: 'logger-reload',
});
return app.ready();
});
// logging to files
before(() => {
return request(app.callback())
.get('/log')
.expect({
method: 'GET',
path: '/log',
})
.expect(200);
});
// start rotating
before(() => {
return request(app.callback())
.get('/rotate')
.expect(200);
});

after(() => app.close());

it('should reload worker loggers', function* () {
yield sleep(2000);

const logname = moment().subtract(1, 'days').format('.YYYY-MM-DD');
const logfile1 = path.join(baseDir, 'logs/logger-reload/logger-reload-web.log');
const content1 = fs.readFileSync(logfile1, 'utf8');
content1.should.equal('');

const logfile2 = path.join(baseDir, `logs/logger-reload/logger-reload-web.log${logname}`);
const content2 = fs.readFileSync(logfile2, 'utf8');
content2.should.containEql('GET /');

const logfile3 = path.join(baseDir, `logs/logger-reload/egg-agent.log${logname}`);
const content3 = fs.readFileSync(logfile3, 'utf8');
content3.should.containEql('agent warn');

yield request(app.callback())
.get('/log')
.expect(200);

// will logging to new file
const content4 = fs.readFileSync(logfile1, 'utf8');
content4.should.containEql('GET /');
});
});
});

function sleep(ms) {
Expand Down