Skip to content

Commit

Permalink
Allow for no timestamp being appended (#64)
Browse files Browse the repository at this point in the history
The documentation states that `false` is a valid value for the `dateFormat` global config param. That usually suggests that passing `false` means "do not add this field" but in this case, the code said otherwise, it meant: "use ISO".

Since most loggers nowadays (including `console.log`!) already add a timestamp to the log line, there has to be a way to opt-out of this field, to avoid repetition.

Example:

**Before:**
```
2020-08-11T21:25:35.736Z	90142447-1486-1b56-34e1-68e1b1439670	INFO	[Response] 2020-08-11T21:25:35+0000 https://example.com/api/meters/123 GET
```

**After:**
```
2020-08-11T21:25:35.736Z	90142447-1486-1b56-34e1-68e1b1439670	INFO	[Response] https://example.com/api/meters/123 GET
```
  • Loading branch information
Jonathan Chiocchio authored Aug 19, 2020
1 parent 2ff143b commit 0449e8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/common/__test__/string-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ test('makeDateFormat should add date', () => {
expect(result).toContain(date.getFullYear());
});

test('makeDateFormat with dateFormat = false, does not add the timestamp', () => {
const sb = new StringBuilder({
dateFormat: false,
});
const date = new Date();
const result = sb.makeDateFormat(date).build();

expect(result).toBe('');
});

test('makeHeaders should add headers', () => {
const sb = new StringBuilder({
headers: true,
Expand Down
9 changes: 6 additions & 3 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ class StringBuilder {
}

makeDateFormat(date: Date) {
// @ts-ignore
const dateFormat = dateformat(date, this.config.dateFormat || 'isoDateTime');
this.printQueue.push(dateFormat);
// allow for opting-out of adding the timestamp (as most loggers already add this)
if (this.config.dateFormat !== false) {
// @ts-ignore
const dateFormat = dateformat(date, this.config.dateFormat || 'isoDateTime');
this.printQueue.push(dateFormat);
}
return this;
}

Expand Down

0 comments on commit 0449e8b

Please # to comment.