Socket
Socket
Sign inDemoInstall

loglevel-plugin-prefix

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

loglevel-plugin-prefix


Version published
Maintainers
1
Created

Package description

What is loglevel-plugin-prefix?

The loglevel-plugin-prefix npm package is a plugin for the loglevel logging library that allows you to add customizable prefixes to your log messages. This can be useful for adding context to your logs, such as timestamps, log levels, or custom tags.

What are loglevel-plugin-prefix's main functionalities?

Basic Prefixing

This feature allows you to add a basic prefix to your log messages. By default, it adds the log level as a prefix.

const log = require('loglevel');
const prefix = require('loglevel-plugin-prefix');
prefix.apply(log);
log.info('This is an info message');

Custom Prefix Format

This feature allows you to customize the format of the prefix. You can include a timestamp, log level, and logger name in the prefix.

const log = require('loglevel');
const prefix = require('loglevel-plugin-prefix');
prefix.apply(log, {
  template: '[%t] %l (%n):',
  timestampFormatter: date => date.toISOString(),
  levelFormatter: level => level.toUpperCase(),
  nameFormatter: name => name || 'global'
});
log.info('This is an info message');

Custom Prefix Function

This feature allows you to define a custom function to format the prefix. This gives you complete control over how the prefix is generated.

const log = require('loglevel');
const prefix = require('loglevel-plugin-prefix');
prefix.apply(log, {
  format(level, name, timestamp) {
    return `${timestamp} [${level}] ${name}:`;
  }
});
log.info('This is an info message');

Other packages similar to loglevel-plugin-prefix

Readme

Source

loglevel-plugin-prefix

Plugin for loglevel message prefixing

Installation

npm install loglevel-plugin-prefix --save

API

apply(log[, options]);

log - root logger, imported from loglevel package

options - configuration object

var defaults = {
  template: '[%t] %l:',
  timestampFormatter: date => date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1'),
  levelFormatter: level => level.toUpperCase(),
  nameFormatter: name => name || 'root'
}

Plugin formats the prefix using template option as a printf-like format.

The template is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the value from loglevel messages parameters. Supported placeholders are:

  • %t - timestamp of message
  • %l - level of message
  • %n - name of logger

The timestampFormatter, levelFormatter and nameFormatter is a functions for formatting corresponding values

Base usage

Browser directly

Download production version and copy to your project folder

<script src="loglevel.min.js"></script>
<script src="loglevel-plugin-prefix.min.js"></script>

<script>
  var logger = log.noConflict();
  prefix.noConflict().apply(logger);
  logger.warn('prefixed message');
</script>

Output

[12:53:46] WARN: prefixed message

ES6


import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';

prefix.apply(log);
log.warn('prefixed message');

CommonJS


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

prefix.apply(log);
log.warn('prefixed message');

AMD

define(['loglevel', 'loglevel-plugin-prefix'], function(log, prefix) {
  prefix.apply(log);
  log.warn('prefixed message');
});

Custom options

import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';

prefix.apply(log, {
  template: '[%t] %l (%n) static text:',
  timestampFormatter: date => date.toISOString(),
  levelFormatter: level => level.charAt(0).toUpperCase() + level.substr(1),
  nameFormatter: name => name || 'global'
});

log.warn('prefixed message');

Output

[2017-05-29T16:53:46.000Z] Warn (global) static text: prefixed message

Example

// moduleA.js
import log from 'loglevel';

export default function () {
  log.warn('message from moduleA');
}
// moduleB.js
import log from 'loglevel';

const logger = log.getLogger('moduleB');

export default function () {
  logger.warn('message from moduleB');
}
// moduleC.js
import log from 'loglevel';

export default function () {
  const logger = log.getLogger('moduleC');
  logger.warn('message from moduleC');
}
// main.js
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';

import a from './moduleA';
import b from './moduleB';
import c from './moduleC';

log.warn('message from root %s prefixing', 'before');

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

log.warn('message from root %s prefixing', 'after');

a();
b();
c();

Output

message from root before prefixing
[16:53:46] WARN (root): message from root after prefixing
[16:53:46] WARN (root): message from moduleA
message from moduleB
[16:53:46] WARN (moduleC): message from moduleC

Errors

import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';

log.setLevel('info');
prefix.apply(log);

log.info('message from root after prefixing');

try {
  prefix.apply(log, { timestampFormatter: date => date.toISOString() });
} catch(e) {
  log.error(e);
};

log.info('message from root after pre-prefixing');

const logger = log.getLogger('child');

try {
  prefix.apply(logger, { template: '[%t] %l (%n):' });
} catch(e) {
  logger.error(e);
};

logger.info('message from child logger');

Output

[16:53:46] INFO: message from root after prefixing
[16:53:46] ERROR: TypeError: You can assign a prefix only one time
[16:53:46] INFO: message from root after pre-prefixing
[16:53:46] ERROR: TypeError: Argument is not a root loglevel object
[16:53:46] INFO: message from child logger

Keywords

FAQs

Last updated on 06 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc