tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Bug: Wrong Timestamp in Output

Open MBurchard opened this issue 2 years ago • 0 comments

Describe the bug Demo Script:

import {Logger} from 'tslog';
import dayjs from 'dayjs';

const logTemplate = '{{rawIsoStr}}\t{{logLevelName}}\t[{{filePathWithLine}}{{name}}]\t';
const log = new Logger({prettyLogTemplate: logTemplate, prettyLogTimeZone: 'local'});

const test = dayjs().format('YYYY-MM-DDTHH:mm:ssZ[Z]');

log.debug(test);

Output:

2023-01-19T12:05:37.263Z        DEBUG   [/scripts/demo.ts:13]   2023-01-19T12:05:37+01:00Z

Expected behavior If I want to see the raw ISO time format but for the local time, it should look like dayjs formats it. Otherwise, someone seeing the log could misunderstand the timestamps.

Node.js Version v18.13.0

OS incl. Version macOS Ventura Version 13.1 (22C65)

Additional Information I would like to propose something. It would be great if you could configure the format of the timestamp globally and not have to give it with every logger. Additionally, it would be great if you could specify it without having to specify the whole prettyLogTemplate. Would the use of dayjs be possible? Then you could completely customise the format of the timestamp according to the needs of the client.

Logger.configure({
  prettyTimeStampFormat: 'YYYY-MM-DDTHH:mm:ssZ[Z]',
});

Logger.configure({
  prettyTimeStampFormat: (ts: Date) => {dayjs(ts).format('YYYY-MM-DDTHH:mm:ssZ[Z]');},
});

The second example shows a way to integrate dayjs, if the user wants to, without adding it to tslogif you don't want to.

MBurchard avatar Jan 19 '23 11:01 MBurchard