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: file path support relative path #41

Merged
merged 2 commits into from Dec 31, 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
1 change: 0 additions & 1 deletion .autod.conf
Expand Up @@ -15,7 +15,6 @@ module.exports = {
'egg-ci',
],
semver: [
'egg-bin@1',
Copy link
Member

Choose a reason for hiding this comment

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

不支持 node 4 了?

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/eggjs/egg-logger/blob/master/package.json#L60

看来一直不支持,应该改一下 engines

Copy link
Member Author

Choose a reason for hiding this comment

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

之前死马升大版本时漏了改好像

Copy link
Member Author

Choose a reason for hiding this comment

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

#42

'koa@1',
'debug@2',
],
Expand Down
6 changes: 5 additions & 1 deletion lib/egg/logger.js
@@ -1,5 +1,6 @@
'use strict';

const path = require('path');
const Logger = require('../logger');
const utils = require('../utils');
const FileTransport = require('../transports/file');
Expand All @@ -14,7 +15,8 @@ class EggLogger extends Logger {
/**
* @constructor
* @param {Object} options
* - {String} file - log file
* - {String} dir - log base dir
* - {String} file - log file, support relavie path
* - {String} [encoding = utf8] - log string encoding
* - {String} [level = INFO] - file log level
* - {String} [consoleLevel = NONE] - console log level
Expand All @@ -28,6 +30,8 @@ class EggLogger extends Logger {
constructor(options) {
super(options);

if (!path.isAbsolute(this.options.file)) this.options.file = path.join(this.options.dir, this.options.file);

if (this.options.outputJSON === true && this.options.file) {
this.options.jsonFile = this.options.file.replace(/\.log$/, '.json.log');
}
Expand Down
11 changes: 5 additions & 6 deletions lib/egg/loggers.js
@@ -1,6 +1,5 @@
'use strict';

const path = require('path');
const assert = require('assert');
const debug = require('debug')('egg:logger');
const utils = require('../utils');
Expand Down Expand Up @@ -66,28 +65,28 @@ class Loggers extends Map {
assert(loggerConfig.errorLogName, 'should pass config.logger.errorLogName');

const errorLogger = new ErrorLogger(utils.assign({}, loggerConfig, {
file: path.join(loggerConfig.dir, loggerConfig.errorLogName),
file: loggerConfig.errorLogName,
}));
this.set('errorLogger', errorLogger);

if (loggerConfig.type === 'agent') {
const logger = new Logger(utils.assign({}, loggerConfig, {
file: path.join(loggerConfig.dir, loggerConfig.agentLogName),
file: loggerConfig.agentLogName,
}));
this.set('logger', logger);

const coreLogger = new Logger(utils.assign({}, loggerConfig, loggerConfig.coreLogger, {
file: path.join(loggerConfig.dir, loggerConfig.agentLogName),
file: loggerConfig.agentLogName,
}));
this.set('coreLogger', coreLogger);
} else {
const logger = new Logger(utils.assign({}, loggerConfig, {
file: path.join(loggerConfig.dir, loggerConfig.appLogName),
file: loggerConfig.appLogName,
}));
this.set('logger', logger);

const coreLogger = new Logger(utils.assign({}, loggerConfig, loggerConfig.coreLogger, {
file: path.join(loggerConfig.dir, loggerConfig.coreLogName),
file: loggerConfig.coreLogName,
}));
this.set('coreLogger', coreLogger);
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -17,15 +17,16 @@
"autod": "^3.0.1",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"coffee": "^5.1.0",
"egg-bin": "^1.11.1",
"egg-ci": "^1.10.0",
"eslint": "^5.9.0",
"coffee": "^5.2.1",
"egg-bin": "^4.9.0",
"egg-ci": "^1.11.0",
"eslint": "^5.11.1",
"eslint-config-egg": "^7.1.0",
"heapdump": "^0.3.12",
"ko-sleep": "^1.0.3",
"koa": "^1.6.2",
"mm": "^2.4.1",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
"rimraf": "^2.6.2",
"should": "^13.2.3",
Expand Down
38 changes: 23 additions & 15 deletions test/lib/egg/custom_logger.test.js
@@ -1,30 +1,38 @@
'use strict';

require('should');
const fs = require('fs');
const assert = require('assert');
const { fs } = require('mz');
const path = require('path');
const coffee = require('coffee');
const rimraf = require('rimraf');
const { rimraf } = require('mz-modules');

describe('test/egg/custom_logger.test.js', () => {
const loggerFile = path.join(__dirname, '../../fixtures/egg_custom_logger.js');
const filepath = path.join(__dirname, '../../fixtures/tmp/a.log');
const tmpDir = path.join(__dirname, '../../fixtures/tmp');
const filePath = path.join(tmpDir, '/a.log');

afterEach(() => {
rimraf.sync(path.dirname(filepath));
});
beforeEach(() => rimraf(path.dirname(filePath)));
afterEach(() => rimraf(path.dirname(filePath)));

it('should format work', done => {
it('should format work', function*() {
const options = {
file: filepath,
file: filePath,
level: 'WARN',
};
coffee.fork(loggerFile, [ JSON.stringify(options) ])
.end(() => {
fs.readFileSync(filepath, 'utf8')
.should.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/);
done();
});
yield coffee.fork(loggerFile, [ JSON.stringify(options) ]).end();
const log = yield fs.readFile(filePath, 'utf-8');
assert(log.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/));
});

it('should support relative path', function* () {
const options = {
dir: tmpDir,
file: 'relative.log',
level: 'WARN',
};
yield coffee.fork(loggerFile, [ JSON.stringify(options) ]).end();

const log = yield fs.readFile(path.join(tmpDir, options.file), 'utf-8');
assert(log.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} ERROR \d+ error foo\n/));
});
});
13 changes: 13 additions & 0 deletions test/lib/egg/loggers.test.js
Expand Up @@ -16,6 +16,7 @@ describe('test/egg/loggers.test.js', () => {
const cLog = path.join(tmp, 'c.log');
const dLog = path.join(tmp, 'd.log');
const eLog = path.join(tmp, 'e.log');
const fLog = 'f.log';

describe('application', () => {
let loggers;
Expand Down Expand Up @@ -53,6 +54,9 @@ describe('test/egg/loggers.test.js', () => {
file: eLog,
concentrateError: 'ignore',
},
fLogger: {
file: fLog,
},
},
});
});
Expand Down Expand Up @@ -158,6 +162,15 @@ describe('test/egg/loggers.test.js', () => {
content.should.not.containEql('\n');
});

it('should fLogger log to f.log with relative config', function*() {
loggers.fLogger.info('fLogger info foo');

yield sleep(10);

const content = fs.readFileSync(path.join(tmp, 'f.log'), 'utf8');
content.should.containEql('fLogger info foo');
});

it('reload all logger', done => {
loggers.reload();
setTimeout(done, 500);
Expand Down