Skip to content

Feature Request: Tagged Template Strings #193

Description

@WorldMaker

Description / Use Case for a Feature

It would be cool to have log functions that can be used as Tagged Template Strings.

Template strings are a very useful way to format strings in modern JS with all sorts of nice type information and autocomplete in TS:

const aThing = 45
const bThing = true

const logMessage = `this is a log message for aThing at ${aThing} and bThing is ${bThing}`

Template strings are also more powerful than just string formatting, they can be tagged with a tag function. That tag function is given the strings around the "holes" as well as the values to fill the holes with (before they've been stringified).

Tag functions can be handy for structured logging. A hypothetical TemplateLogger could be written that takes a template literal directly and logs it as a structured object.

const coolLogger = new TemplateLogger()

coolLogger.info`this is a log message for aThing at ${aThing} and bThing is ${bThing}`

This would be roughly equivalent to:

logger.info('this is a log message for aThing at ', 45, ' and bThing is ', true)

It's an improvement on "splat-style" logging with better TS language service syntax support.

I may PR an attempt at this if there is interest because that "roughly equivalent to" should be an easy enough transform.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions