-
Notifications
You must be signed in to change notification settings - Fork 94
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
Issue with date formatting? #320
Comments
Hi @KirianCaumes If I understand it correctly, using |
Yep that's it, but you might be right te keep it closer to how the nest console works, don't know 🤷♀️ Btw, I don't think my "fix" break anything, as by default the date is displayed with the |
@KirianCaumes I agree with you. If However, we should handle the case when:
This: |
Like this, you can handle the three different cases: export const nestLikeConsoleFormat = (appName = 'NestWinston'): Format => winston.format.printf(({ context, level, timestamp, message, ...meta }) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const color = nestLikeColorScheme[level] || ((text: string): string => text);
const metaStr = safeStringify(meta)
const timeToDisplay = (() => {
// Without winston.format.timestamp()
if ('undefined' === typeof timestamp) {
return ' '
} else {
const dt = new Date(timestamp)
// With winston.format.timestamp(), is this case "format" is equivalent to "new Date().toISOString()", example '2021-03-31T11:36:52.110Z'
if (Object.prototype.toString.call(dt) === "[object Date]" && !isNaN(dt.getTime())) {
return `${dt.toLocaleString()} `
} else {
// With winston.format.timestamp({ format: "DD/MM/YYYY HH:mm:ss" }), with valid "fecha" format, example '31/03/2021 13:43:00'
return `${timestamp} `
}
}
})()
return `${color(`[${appName}]`)} ` +
`${clc.yellow(level.charAt(0).toUpperCase() + level.slice(1))}\t` +
(timeToDisplay) +
('undefined' !== typeof context ? `${clc.yellow('[' + context + ']')} ` : '') +
`${color(message)}` +
`${metaStr !== "{}" ? ` - ${metaStr}` : ''}`;
}); Feel free to make it cleaner 😁 ps: I've also handled a bit differently the |
Thanks, looking into this right now. I'll keep you updated :) |
Have you checked the new tag which includes your fix? It's OK? 👍 |
I just check it, and it works fine, thank you! Btw, what do you think about hidding the "meta" data if it is empty, like I did in my previous message? |
Sorry forgot about it, it depends on how Nest logger do in this case (since that formatter should mimic the Nest one). Can you check and report? Maybe we should open another issue for this. |
Hey, (new Date("my invalid date"))?.toISOString() It doesn't return "Invalid Date" as: (new Date("my invalid date"))?.toLocaleString() But it throw an error: "Uncaught RangeError: invalid date". [4:03:34 PM] File change detected. Starting incremental compilation...
[4:03:34 PM] Found 0 errors. Watching for file changes.
Debugger attached.
(node:5213) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
1: 0xa04200 node::Abort() [node]
2: 0xa78549 [node]
3: 0xbe56eb [node]
4: 0xbe6c96 [node]
5: 0xbe7316 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [node]
6: 0x14012f9 [node]
Aborted An easy solution would be to do something like this: if ('undefined' !== typeof timestamp) {
try {
if (timestamp === (new Date(timestamp)).toISOString()) {
timestamp = (new Date(timestamp)).toLocaleString();
}
} catch (error) { }
} What do you think about this? |
Hi there, why the |
Here is an example: new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp({ format: "DD/MM/YYYY HH:mm:ss" }),
nestWinstonModuleUtilities.format.nestLike("Emptio")
),
})
// => '14/04/2021 13:31:54'
// (new Date('14/04/2021 13:31:54')).toISOString() -> Uncaught RangeError: invalid date
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
nestWinstonModuleUtilities.format.nestLike("Emptio")
),
})
// => '2021-04-14T13:33:29.814Z'
// (new Date('2021-04-14T13:33:29.814Z')).toISOString() -> "2021-04-14T13:33:29.814Z" |
Oh right, thanks, guess I need to implement some testing for that. I was fooled using always an accepted format. I’ll work on a fix! |
Hello,
I'm not sure if it's a bug or not, but if I want to format my date thanks to "winston.format.timestamp", an error "Invalid Date" is displayed in the console.
Code example:
So my question is : why the function "nestLikeConsoleFormat" at line 21, does not look like this, to allow custom format to displayed?
Thank you very much for your answer!
The text was updated successfully, but these errors were encountered: