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

fix: support json files #17

Merged
merged 1 commit into from
Feb 23, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/lib/day_rotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ class DayRotator extends Rotator {
const files = new Map();
const loggers = this.app.loggers;
for (const key in loggers) {
this._setFile(loggers[key].options.file, files);
const logger = loggers[key];
this._setFile(logger.options.file, files);
if (logger.options.jsonFile) {
this._setFile(logger.options.jsonFile, files);
}
}

// Should rotate agent log, because schedule is running under app worker,
// agent log is the only differece between app worker and agent worker.
// - app worker -> egg-web.log
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/logrotator-json-format/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const path = require('path');

module.exports = () => {
const exports = {
logger: {
outputJSON: true,
},
logrotator: {
filesRotateByHour: [
path.join(__dirname, '../logs', 'logrotator', 'hour.log'),
path.join(__dirname, '../logs', 'logrotator', 'hour.json.log'),
],
filesRotateBySize: [
path.join(__dirname, '../logs', 'logrotator', 'size.log'),
path.join(__dirname, '../logs', 'logrotator', 'size.json.log'),
],
maxFileSize: 1,
maxFiles: 2,
},
customLogger: {
dayLogger: {
file: path.join(__dirname, '../logs', 'logrotator', 'day.log'),
},
sizeLogger: {
file: path.join(__dirname, '../logs', 'logrotator', 'size.log'),
},
hourLogger: {
file: path.join(__dirname, '../logs', 'logrotator', 'hour.log'),
},
},
};
return exports;
};
6 changes: 6 additions & 0 deletions test/fixtures/logrotator-json-format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "logrotator",
"dependencies": {
},
"private":true
}
48 changes: 47 additions & 1 deletion test/logrotator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('test/logrotator.test.js', () => {
yield app.runSchedule(schedule);

const files = glob.sync(path.join(app.config.logger.dir, '*.log.*'));
assert(files.length === 4);
assert(files.length > 4);
assert(files.filter(name => name.indexOf('foo.log.') > 0));
files.forEach(file => assert(/log.\d{4}-\d{2}-\d{2}$/.test(file)));

Expand Down Expand Up @@ -376,6 +376,52 @@ describe('test/logrotator.test.js', () => {

});

describe('json logger', () => {
let app;
before(() => {
app = mm.app({
baseDir: 'logrotator-json-format',
cache: false,
});
return app.ready();
});
after(() => app.close());

it('should be rotated by day', function* () {
const logDir = app.config.logger.dir;
const now = moment().startOf('date');
const date = now.clone().subtract(1, 'days').format('YYYY-MM-DD');
const schedule = path.join(__dirname, '../app/schedule/rotate_by_file');
yield app.runSchedule(schedule);

assert(fs.existsSync(path.join(logDir, `day.log.${date}`)));
assert(fs.existsSync(path.join(logDir, `day.json.log.${date}`)));
});

it('should be rotated by hour', function* () {
const logDir = app.config.logger.dir;
const date = moment().subtract(1, 'hours').format('YYYY-MM-DD-HH');
const schedule = path.join(__dirname, '../app/schedule/rotate_by_hour');
yield app.runSchedule(schedule);

assert(fs.existsSync(path.join(logDir, `hour.log.${date}`)));
assert(fs.existsSync(path.join(logDir, `hour.json.log.${date}`)));
});

it('should be rotated by size', function* () {
app.getLogger('sizeLogger').info('size');
// wait flush
yield sleep(1000);

const logDir = app.config.logger.dir;
const schedule = path.join(__dirname, '../app/schedule/rotate_by_size');
yield app.runSchedule(schedule);

assert(fs.existsSync(path.join(logDir, 'size.log.1')));
assert(fs.existsSync(path.join(logDir, 'size.json.log.1')));
});
});

});

function sleep(ms) {
Expand Down