Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
@jalik/logger
Advanced tools
A flexible logger to log messages to anything you want (console, file, database...), there's nothing more to say about it.
Logging is an important part of an application lifecycle, from development to production, we always need to log messages for debugging or tracing errors and warnings, this lib will hep you taking control of logging in your apps.
The first thing to do is to create a logger, it's deadly simple.
import Logger from '@jalik/logger';
const logger = new Logger({
// Activate the logger
active: true,
// Display message of given types in the console
console: {
debug: true,
error: true,
info: true,
other: true,
warning: true
},
// Display context in the console
displayContext: false,
// Display logger name in the console
displayName: true,
// Give a name to this logger
name: 'main'
});
Note that after creating the logger, you can still change the options via the public attribute
options
, like logger.options.console.debug = false
.
Instead of levels, this lib refers to types of logging, this is useful to distinguish and filter messages, there are 4 well known types of logging :
You can get the string value of each types by importing the types list.
import Types from '@jalik/logger/dist/types';
Types.debug; // used by console.debug()
Types.error; // used by console.error()
Types.info; // used by console.info()
Types.warning; // used by console.warn()
When you log a message, you can also provide an optional context as extra information, you have a dedicated method for each type of logging.
import Logger from '@jalik/logger';
const logger = new Logger();
// Logs a debug message
// Note: you can use string templates available since ES6
// to have dynamic logs.
const user = {name: 'karl'};
logger.debug(`user logged "${user.name}"`, user);
// Logs an error message
logger.error('Forbidden', {error: new Error('forbidden')});
// You can directly pass an Error object
logger.error(new Error('forbidden'));
// Logs an info message
logger.info('Application started', {
date: new Date()
});
// Logs a warning message
logger.warn('Disk usage is above 90%', {
diskUsage: 92.6
});
// Logs a custom type message
const ipAddress = '6.6.6.6';
logger.log(`The IP address ${ipAddress} has failed to login 3 times`, 'suspicious', {ipAddress});
By default a logger is activated, but you can deactivate it anytime you want by using the setActive(Boolean)
method.
import Logger from '@jalik/logger';
const logger = new Logger();
// Activate logger on production environment only
logger.setActive(process.env.NODE_ENV === 'PRODUCTION');
// And to check if the logger is active
logger.isActive();
The logger is flexible enough in the way that you can execute callbacks when an event occurs (debug, error, info, warning), so you could save logs to a database, a file or whatever you want.
import Types from '@jalik/logger/dist/types';
import Logger from '@jalik/logger';
const logger = new Logger();
// With this event listener, you can do something when an error happens
logger.on('log', (message, type, context) => {
if (type === Types.error) {
// do whatever you want here...
// save error to database, send an email...
}
});
// This will trigger the listener defined above
logger.error('Cannot contact DNS server', {
ipAddress: '8.8.8.8'
});
It can be useful to clone an existing logger, thus reusing the same configuration by calling the clone()
method on a logger.
import Logger from '@jalik/logger';
const loggerA = new Logger({name:'A', displayContext: true});
const loggerB = loggerA.clone('B');
// The logger B will display the given context since it has been enabled in the logger A.
loggerB.debug('printed now', {date: new Date()});
History of releases is in the changelog on github.
The code is released under the MIT License.
If you find this lib useful and would like to support my work, donations are welcome :)
v2.2.5 (2020-08-06)
FAQs
A logging utility to log messages to anywhere.
The npm package @jalik/logger receives a total of 19 weekly downloads. As such, @jalik/logger popularity was classified as not popular.
We found that @jalik/logger 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
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.