Skip to content

Commit

Permalink
Make message omittable
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroaki SHIBUKI authored and Hiroaki SHIBUKI committed Mar 14, 2018
1 parent 05604b5 commit e7e3246
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -17,12 +17,12 @@ const Logger = require('node-json-logger');
const logger = new Logger();

logger.info('info.');
logger.info('info.', { data1: 'data#1', data2: 'data#2' });
logger.info({ data1: 'data#1', data2: 'data#2' });
```
Output:
```json
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","message":"info."}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","message":"info.","data1":"data#1","data2":"data#2"}}
{"timestamp":"2001-03-14T01:00:00.000Z","level":"info","data1":"data#1","data2":"data#2"}}
```

# <a href="#Levels"></a>Levels
Expand All @@ -41,8 +41,8 @@ Note:
* xxx is one of the [Levels](#Levels)

Arguments:
* message: Specify a message as string.
* details: Specify details as object. (optional)
* message: Specify a message as string (Optional).
* details: Specify details as object (optional).

# Configuration
## level
Expand Down
27 changes: 14 additions & 13 deletions index.js
Expand Up @@ -12,30 +12,34 @@ class Logger {
this.options.source = this.options.source || undefined;

this.getTimestamp = () => moment.utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]');
this.formatLog = JSON.stringify;
this.stringifyLog = JSON.stringify;
this.writeLog = console.log;

this.trace = this.debug = this.info = this.warn = this.error = this.fatal = () => { };
const level = levels.includes(this.options.level) ? this.options.level : 'debug';
switch (level) {
case 'trace':
this.trace = (message, details) => this.output('trace', message, details);
this.trace = message => this.output('trace', message);
case 'debug':
this.debug = (message, details) => this.output('debug', message, details);
this.debug = message => this.output('debug', message);
case 'info':
this.info = (message, details) => this.output('info', message, details);
this.info = message => this.output('info', message);
case 'warn':
this.warn = (message, details) => this.output('warn', message, details);
this.warn = message => this.output('warn', message);
case 'error':
this.error = (message, details) => this.output('error', message, details);
this.error = message => this.output('error', message);
case 'fatal':
this.fatal = (message, details) => this.output('fatal', message, details);
this.fatal = message => this.output('fatal', message);
}
}

output(level, message, details) {
output(level, message) {
if (typeof message === 'string') {
message = { message: message };
}

const head = {};
const tail = Object.assign({}, details || {});
const tail = Object.assign({}, message || {});

if (this.options.timestamp) {
head.timestamp = this.getTimestamp();
Expand All @@ -50,11 +54,8 @@ class Logger {
delete tail.source;
}

head.message = message + '';
delete tail.message;

const log = Object.assign(head, tail);
this.writeLog(this.formatLog(log));
this.writeLog(this.stringifyLog(log));
}
}

Expand Down
12 changes: 6 additions & 6 deletions index.test.js
Expand Up @@ -31,8 +31,8 @@ test('Logger output a log in the format designed.', t => {
logger[l](`${l}.`);
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","message":"${l}."}`);

logger[l](`${l}.`, { data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","message":"${l}.","data1":"${l}#1","data2":"${l}#2"}`);
logger[l]({ data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","data1":"${l}#1","data2":"${l}#2"}`);
});
});

Expand All @@ -58,8 +58,8 @@ test('Logger can output a log contains source.', t => {
logger[l](`${l}.`);
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","source":"${logger.options.source}","message":"${l}."}`);

logger[l](`${l}.`, { data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","source":"${logger.options.source}","message":"${l}.","data1":"${l}#1","data2":"${l}#2"}`);
logger[l]({ data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"timestamp":"${timestamp}","level":"${l}","source":"${logger.options.source}","data1":"${l}#1","data2":"${l}#2"}`);
});
});

Expand Down Expand Up @@ -122,7 +122,7 @@ test('Timestamp can be disiable with config.', t => {
logger[l](`${l}.`);
t.is(console.last(), `{"level":"${l}","message":"${l}."}`);

logger[l](`${l}.`, { data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"level":"${l}","message":"${l}.","data1":"${l}#1","data2":"${l}#2"}`);
logger[l]({ data1: `${l}#1`, data2: `${l}#2` });
t.is(console.last(), `{"level":"${l}","data1":"${l}#1","data2":"${l}#2"}`);
});
});

0 comments on commit e7e3246

Please sign in to comment.