mrkeyoor.com_
Sat 08 Aug 22:53 UTC
npmWeb Frontendupdated 08 Aug 2026

loglevel-plugin-prefix

loglevel-plugin-prefix is a small plugin that prepends timestamps, severity labels, and logger names to messages produced by loglevel. It wraps each logger's methodFactory, supports a token template or a custom formatting function, and works with the root logger as well as named loggers. It is a presentation add-on, not a logging backend: loglevel still controls levels and output, and the plugin does not add JSON records, transports, log rotation, persistence, redaction, or remote delivery.

Verdict

Reasonable glue for an existing loglevel browser stack that wants readable prefixes. Do not treat its high transitive download count as a sign of current development, and wrap the call when TypeScript accuracy or structured production logs matter.

API stability3/5Version 0.8.4 exposes only reg and apply, and the practical API has not changed since its June 2018 publication. Repeated apply calls intentionally update stored configuration without wrapping the method factory again. Still, the README explicitly labels the plugin unstable, promises compatibility only across patch releases, and documents a future apply-before-reg error that has never shipped.
Docs3/5The README documents installation, both API calls, every default option, template tokens, custom formatters, named loggers, inheritance, browser globals, and console output examples. It is enough to get working quickly. It does not explain module-instance boundaries or the non-string argument behavior, and the bundled TypeScript declaration contradicts runtime by typing the custom format timestamp as Date instead of a formatted string.
Maintenance1/5The latest npm release, 0.8.4, was published in June 2018, and GitHub shows the last repository push in February 2022. The repository is not archived, but three open issues excluding pull requests date from 2019 and 2020, including configuration inheritance and formatting behavior reports. The small codebase may keep working, but there is no recent evidence of releases or issue response.
Ecosystem3/5The package recorded 3,372,258 downloads in the measured week and plugs directly into loglevel's root and named logger APIs in browsers and Node. That reach is mostly inherited from dependency trees around loglevel rather than a broad plugin family. It has 64 GitHub stars, no transport ecosystem of its own, and no declared peer dependency to coordinate supported loglevel versions.

Use it if

  • Your browser application already uses loglevel and you want consistent human-readable prefixes without replacing the logger
  • You need separate prefixes for loglevel named loggers such as API, auth, or rendering channels
  • You want a tiny synchronous formatter with configurable level, name, and timestamp functions
  • You support an older script-tag integration where both loglevel and the prefix plugin are loaded as browser globals
Skip it if

Setup reality

Install both loglevel and loglevel-plugin-prefix because the plugin package declares no runtime or peer dependency on loglevel. In CommonJS, require both, call prefix.reg(log) once with the root logger, then call prefix.apply(logger, options) for each root or named logger you want to format. Reversing that order currently logs a warning rather than throwing, even though the README has long said a later release would turn it into an error. Applying to the root establishes options inherited by named loggers, but per-name configuration is stored in module-level state, and an open issue reports surprising inheritance when modules load separate instances. Applying a configuration calls setLevel again so loglevel rebuilds its methods. The default output includes local time and uppercase severity but not the logger name. A custom template uses only the first occurrence of %t, %l, and %n because the implementation uses single String.replace calls. If format and template are both supplied, template wins and clears the inherited format function. Non-string first arguments receive the prefix as a separate leading argument, which matters for console inspection and any custom methodFactory. TypeScript users get bundled declarations, but the format callback's timestamp type is wrong: runtime passes a formatted string, not the declared Date. Test your wrapper instead of trusting that annotation. There are no native builds, credentials, files, or asynchronous startup steps.

Patterns

Add the default prefix to the root loggerregister-default-prefix

const log = require('loglevel');
const prefix = require('loglevel-plugin-prefix');

prefix.reg(log);
prefix.apply(log);
log.setLevel('info');
log.info('server connected');

Call reg with the root loglevel object before apply. The default template prints local HH:MM:SS and an uppercase level.

Use timestamp, level, and logger-name tokensconfigure-template

prefix.apply(log, {
  template: '[%t] %l %n:',
});

log.getLogger('api').warn('slow response');

The supported tokens are %t, %l, and %n. Only the first occurrence of each token is replaced by version 0.8.4.

Format timestamps as ISO stringsformat-iso-time

prefix.apply(log, {
  template: '[%t] %l:',
  timestampFormatter(date) {
    return date.toISOString();
  },
});

timestampFormatter receives a Date and its return value is passed into the template or custom format callback.

Build the entire prefix with a functioncustom-format-function

prefix.apply(log, {
  format(level, name, timestamp) {
    return `${timestamp} ${level} [${name}]`;
  },
});

At runtime timestamp is the formatted string, despite the bundled TypeScript declaration typing this argument as Date.

Give one named logger its own prefixprefix-named-logger

const authLog = log.getLogger('auth');

prefix.apply(authLog, {
  template: '%l (%n):',
});

authLog.error('token expired');

Register the root loglevel object once. Apply accepts a root or named logger, but reg does not accept an arbitrary named logger.

Let named loggers inherit root formattinginherit-root-options

prefix.apply(log, {
  template: '[%t] %l %n:',
  nameFormatter: (name) => name || 'app',
});

const cacheLog = log.getLogger('cache');
cacheLog.info('hit');

Named loggers without their own configuration inherit root options, provided the application is using the same plugin module instance.

Change formatting after initial setupreconfigure-prefix

prefix.apply(log, { template: '%l:' });
log.info('plain prefix');

prefix.apply(log, {
  template: '[%t] %l:',
});
log.info('timestamp restored');

A second apply updates configuration and asks loglevel to rebuild methods; it does not stack a second prefix wrapper.

Keep console substitution arguments workingpreserve-printf-placeholders

prefix.apply(log, { template: '%l:' });
log.info('processed %d records in %d ms', count, elapsedMs);

When the first argument is a string, the plugin prepends to that same string so console-style substitution placeholders remain aligned.

Log an object with a separate prefix argumentlog-object-value

prefix.apply(log, { template: '%l %n:' });
log.getLogger('state').debug({ userId: 42, active: true });

For a non-string first argument, the plugin inserts the prefix as a new first argument. Console output and custom sinks may display it separately.

Use the plugin from script tagsload-browser-globals

<script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script>
<script src="https://unpkg.com/loglevel-plugin-prefix@0.8.4/dist/loglevel-plugin-prefix.min.js"></script>
<script>
  prefix.reg(log);
  prefix.apply(log, { template: '[%t] %l:' });
  log.warn('prefixed in the browser');
</script>

Pin the plugin version in a production page. The globals are log and prefix; noConflict is available when those names collide.

Alternatives

PackageRegistryPick it when
loglevelnpmKeep only loglevel and wrap its methodFactory yourself when one simple prefix convention is enough
pinonpmChoose it for fast structured Node logging, child bindings, redaction, and machine-readable production output
winstonnpmChoose it when a Node application needs multiple transports and a larger formatting pipeline
debugnpmChoose it for namespaced diagnostic output that users enable selectively with the DEBUG setting