Skip to content

Commit

Permalink
feat: redirect support options.duplicate (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and dead-horse committed Dec 7, 2018
1 parent 1930861 commit e43f70c
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 182 deletions.
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ coverage
.logs
tmp
*.log
.nyc_output
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -22,7 +22,7 @@

Egg logger.

![](diagram.png)
![diagram](diagram.png)

Including two base class, `Logger` and `Transport`:

Expand Down Expand Up @@ -78,6 +78,12 @@ logger.redirect('error', errorLogger);
logger.error(new Error('print to errorLogger')); // equal to `errorLogger.error`
```

Write to both loggers:

```js
logger.redirect('error', errorLogger, { duplicate: true });
```

### Reload

```js
Expand Down
6 changes: 6 additions & 0 deletions README.zh-CN.md
Expand Up @@ -79,6 +79,12 @@ logger.redirect('error', errorLogger);
logger.error(new Error('print to errorLogger')); // 等价于调用 errorLogger.error
```

也支持日志双写

```js
logger.redirect('error', errorLogger, { duplicate: true });
```

### 重新加载文件

```js
Expand Down
40 changes: 20 additions & 20 deletions benchmark/file_buffer.js
Expand Up @@ -17,27 +17,27 @@ logger.info('info start');
const suite = new Benchmark.Suite();

suite
.add('without arguments', () => {
logger.info('info without arguments and message is litte bit long ........ hahah');
})
.add('with arguments', () => {
logger.info('info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm');
})
.add('with json', () => {
logger.info('info this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' });
})
.add('without arguments', () => {
logger.info('info without arguments and message is litte bit long ........ hahah');
})
.add('with arguments', () => {
logger.info('info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm');
})
.add('with json', () => {
logger.info('info this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' });
})

.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
setTimeout(() => process.exit(0), 5000);
})
.run({ async: false });
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
setTimeout(() => process.exit(0), 5000);
})
.run({ async: false });

// node version: v7.2.1, date: Thu Dec 08 2016 14:00:19 GMT+0800 (CST)
// Starting...
Expand Down
40 changes: 20 additions & 20 deletions benchmark/format.js
Expand Up @@ -17,26 +17,26 @@ function writeFormat(msg) {
const suite = new Benchmark.Suite();

suite
.add('write(msg)', () => {
write('hello');
})
.add('writeFormat(msg)', () => {
writeFormat('hello');
})
.add('writeFormat(msg, args)', () => {
writeFormat('hello %s', 'world');
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });
.add('write(msg)', () => {
write('hello');
})
.add('writeFormat(msg)', () => {
writeFormat('hello');
})
.add('writeFormat(msg, args)', () => {
writeFormat('hello %s', 'world');
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Wed Nov 02 2016 12:19:33 GMT+0800 (CST)
// Starting...
Expand Down
58 changes: 29 additions & 29 deletions benchmark/format_buffer_vs_string.js
Expand Up @@ -51,36 +51,36 @@ const suite = new Benchmark.Suite();

suite

.add('bufferFormat without arguments', () => {
bufferFormat('info', [ 'info without arguments and message is litte bit long ........ hahah' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat without arguments', () => {
stringFormat('info', [ 'info without arguments and message is litte bit long ........ hahah' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('bufferFormat with arguments', () => {
bufferFormat('info', [ 'info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat with arguments', () => {
stringFormat('info', [ 'info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('bufferFormat with json', () => {
bufferFormat('info', [ 'this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' }], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat with json', () => {
stringFormat('info', [ 'this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' }], null, { encoding: 'utf8', eol: '\n' });
})

.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
.add('bufferFormat without arguments', () => {
bufferFormat('info', [ 'info without arguments and message is litte bit long ........ hahah' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat without arguments', () => {
stringFormat('info', [ 'info without arguments and message is litte bit long ........ hahah' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('bufferFormat with arguments', () => {
bufferFormat('info', [ 'info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat with arguments', () => {
stringFormat('info', [ 'info %s %s %s %s %d %s', 'a', 'bbbb', 'ccccc', 'ddddd', 123123, 'this is long string mmmmmmm' ], null, { encoding: 'utf8', eol: '\n' });
})
.add('bufferFormat with json', () => {
bufferFormat('info', [ 'this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' }], null, { encoding: 'utf8', eol: '\n' });
})
.add('stringFormat with json', () => {
stringFormat('info', [ 'this is json %j', { a: 'a', b: 'bbbb', c: 'ccccc', d: 'ddddd', e: 123123, f: 'this is long string mmmmmmm' }], null, { encoding: 'utf8', eol: '\n' });
})

.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
// process.exit(0);
})
.run({ async: false });
})
.run({ async: false });

// <Buffer 74 68 69 73 20 69 73 20 62 75 66 66 65 72 46 6f 72 6d 61 74 2c 20 68 69 20 7b 22 66 6f 6f 22 3a 22 62 61 72 22 7d 0a>
// this is stringFormat, hi {"foo":"bar"}
Expand Down
74 changes: 37 additions & 37 deletions benchmark/index.js
Expand Up @@ -40,43 +40,43 @@ err.code = 'MOCK';
const suite = new Benchmark.Suite();

suite
.add('logger.error(err)', () => {
loggerWithoutJSON.error(err);
})
.add('logger.info(message)', () => {
loggerWithoutJSON.info('hello, %s', 'world');
})
.add('logger.write(message)', () => {
loggerWithoutJSON.write('hello, world');
})
.add('contextLogger.info(message)', () => {
contextLogger.info('hello, %s', 'world');
})
.add('logger.error(err) with JSON', () => {
loggerWithJSON.error(err);
})
.add('logger.info(message) with JSON', () => {
loggerWithJSON.info('hello, %s', 'world');
})
.add('logger.write(message) with JSON', () => {
loggerWithJSON.write('hello, world');
})
.add('contextLogger.info(message) with JSON', () => {
contextLoggerWithJSON.info('hello, %s', 'world');
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
loggerWithJSON.disable('console');
loggerWithoutJSON.disable('console');
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });
.add('logger.error(err)', () => {
loggerWithoutJSON.error(err);
})
.add('logger.info(message)', () => {
loggerWithoutJSON.info('hello, %s', 'world');
})
.add('logger.write(message)', () => {
loggerWithoutJSON.write('hello, world');
})
.add('contextLogger.info(message)', () => {
contextLogger.info('hello, %s', 'world');
})
.add('logger.error(err) with JSON', () => {
loggerWithJSON.error(err);
})
.add('logger.info(message) with JSON', () => {
loggerWithJSON.info('hello, %s', 'world');
})
.add('logger.write(message) with JSON', () => {
loggerWithJSON.write('hello, world');
})
.add('contextLogger.info(message) with JSON', () => {
contextLoggerWithJSON.info('hello, %s', 'world');
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
loggerWithJSON.disable('console');
loggerWithoutJSON.disable('console');
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Tue Jul 26 2016 00:27:29 GMT+0800 (CST)
// Starting...
Expand Down
54 changes: 27 additions & 27 deletions benchmark/map-has.js
Expand Up @@ -13,33 +13,33 @@ map.set('e', 5);
const suite = new Benchmark.Suite();

suite
.add('map.has().get()', () => {
if (map.has('a')) {
return map.get('a');
}
})
.add('map.has()', () => {
if (map.has('a')) {
return;
}
})
.add('map.get()', () => {
const val = map.get('a');
if (val) {
return val;
}
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });
.add('map.has().get()', () => {
if (map.has('a')) {
return map.get('a');
}
})
.add('map.has()', () => {
if (map.has('a')) {
return;
}
})
.add('map.get()', () => {
const val = map.get('a');
if (val) {
return val;
}
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Fri Jul 08 2016 23:53:40 GMT+0800 (CST)
// Starting...
Expand Down

0 comments on commit e43f70c

Please sign in to comment.