Skip to content

LogObject.date should accept a string formatted by a custom date formatter instead of Date.toLocaleTimeString() #351

@GoodWave2020

Description

@GoodWave2020

Environment

Node: 22.12.0
PNPM: 9.15.1

Reproduction

When running the following code:

import consola from 'consola';

consola.log({ date: 'string' });

the following error occurs:

Error: date.toLocaleTimeString is not a function

This error is triggered because consola expects the date field to be an instance of Date rather than a string.

Describe the bug

When invoking consola.log with an object where the date field is set to a string, the process fails at runtime. Internally, consola calls toLocaleTimeString on the date field, which only exists on Date instances. Passing a string results in the aforementioned error.

return opts.date ? date.toLocaleTimeString() : "";

Additional context

I am attempting to customize the log output by manually specifying a custom date format.

To achieve this, I passed an object with a date field set to a string (i.e., { date: 'string' }). However, this leads to a runtime error because consola expects a Date object.

It would be helpful if either the documentation clarified that only Date objects are accepted, or if consola provided support or a graceful fallback for string values.

This change would allow developers greater flexibility when customizing log output.

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions