Skip to content

Sprintf Plugin

NPM Version

Plugin Source

The sprintf plugin provides printf-style string formatting support using sprintf-js. It allows you to format your log messages using familiar printf-style placeholders if a transport does not support this behavior.

WARNING

  • LogLayer does not allow passing items that are not strings or numbers into message methods like info, error, etc. It is recommended to only use string and number specifiers in your format strings.
  • Not all logging libraries support multiple parameters in a message method, so this plugin is only useful for those that do.

Installation

bash
npm install @loglayer/plugin-sprintf
bash
yarn add @loglayer/plugin-sprintf
bash
pnpm add @loglayer/plugin-sprintf

Usage

typescript
import { LogLayer, ConsoleTransport } from 'loglayer'
import { sprintfPlugin } from '@loglayer/plugin-sprintf'

const log = new LogLayer({
  transport: new ConsoleTransport({
    logger: console
  }),
  plugins: [
    sprintfPlugin()
  ]
})

// Example usage
log.info("Hello %s!", "world")
// Output: Hello world!

log.info("Number: %d", 42)
// Output: Number: 42