Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
loglevel-plugin-prefix
Advanced tools
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.
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');
Winston is a versatile logging library for Node.js that supports multiple transports, custom formats, and log levels. It offers more advanced features compared to loglevel-plugin-prefix, such as log rotation and integration with various logging services.
Bunyan is another logging library for Node.js that provides a JSON-based logging format. It includes features like log levels, serializers, and streams. Bunyan is more focused on structured logging compared to the prefixing capabilities of loglevel-plugin-prefix.
Pino is a fast and low-overhead logging library for Node.js. It supports JSON logging, log levels, and custom serializers. Pino is designed for performance and is more lightweight compared to loglevel-plugin-prefix.
Plugin for loglevel message prefixing
npm install loglevel-plugin-prefix --save
apply(log[, options]);
This method applies the plugin to the logger.
log - root logger, imported from loglevel package
options - configuration object
var defaults = {
template: '[%t] %l:',
timestampFormatter: function (date) {
return date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
},
levelFormatter: function (level) {
return level.toUpperCase();
},
nameFormatter: function (name) {
return 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:
The timestampFormatter, levelFormatter and nameFormatter is a functions for formatting corresponding values
disable();
This method cancels the effect of the plugin.
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();
var prefixer = prefix.noConflict();
prefixer.apply(logger);
logger.warn('prefixed message');
</script>
Output
[16:53:46] WARN: prefixed message
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';
prefix.apply(log);
log.warn('prefixed message');
var log = require('loglevel');
var prefix = require('loglevel-plugin-prefix');
prefix.apply(log);
log.warn('prefixed message');
define(['loglevel', 'loglevel-plugin-prefix'], function(log, prefix) {
prefix.apply(log);
log.warn('prefixed message');
});
var log = require('loglevel');
var prefix = require('loglevel-plugin-prefix');
prefix.apply(log, {
template: '[%t] %l (%n) static text:',
timestampFormatter: function (date) { return date.toISOString() },
levelFormatter: function (level) { return level.charAt(0).toUpperCase() + level.substr(1) },
nameFormatter: function (name) { return name || 'global' }
});
log.warn('prefixed message');
Output
[2017-05-29T12:53:46.000Z] Warn (global) static text: prefixed message
// moduleA.js
var log = require('loglevel');
module.exports = function () {
log.warn('message from moduleA');
}
// moduleB.js
var log = require('loglevel');
var logger = log.getLogger('moduleB');
module.exports = function () {
logger.warn('message from moduleB');
}
// moduleC.js
var log = require('loglevel');
module.exports = function () {
var logger = log.getLogger('moduleC');
logger.warn('message from moduleC');
}
// main.js
var log = require('loglevel');
var prefix = require('loglevel-plugin-prefix');
var a = require('./moduleA');
var b = require('./moduleB');
var c = require('./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();
prefix.apply(log, {
template: '[%t] %l:',
timestampFormatter: function (date) { return date.toISOString() }
});
log.warn('message from root after reapplying');
prefix.disable();
log.warn('message from root after disabling');
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
[2017-05-29T12:53:46.000Z] WARN: message from root after reapplying
message from root after disabling
var log = require('loglevel');
var prefix = require('loglevel-plugin-prefix');
var mock = require('loglevel-plugin-mock');
log.enableAll();
prefix.apply(log);
log.info('message from root after prefixing');
prefix.apply(log, { timestampFormatter: function (date) { return date.toISOString() } });
log.info('message from root after pre-prefixing');
mock.apply(log);
try {
prefix.apply(log, { template: '[%t] %l (%n):' });
} catch(e) {
log.error(e);
};
try {
prefix.disable();
} catch(e) {
log.error(e);
};
mock.disable();
var 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
[2017-05-29T12:53:46.000Z] INFO: message from root after pre-prefixing
[2017-05-29T12:53:46.000Z] ERROR: Error: You can't reassign a plugin after appling another plugin
[2017-05-29T12:53:46.000Z] ERROR: Error: You can't disable a plugin after appling another plugin
[2017-05-29T12:53:46.000Z] ERROR: TypeError: Argument is not a root loglevel object
[2017-05-29T12:53:46.000Z] INFO: message from child logger
FAQs
Plugin for loglevel message prefixing
The npm package loglevel-plugin-prefix receives a total of 1,636,031 weekly downloads. As such, loglevel-plugin-prefix popularity was classified as popular.
We found that loglevel-plugin-prefix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.