npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

digio-logger

v1.0.8

Published

An opinionated winston v3 logger wrapper

Downloads

37

Readme

digio-logger

This is just an opinionated winston v3 logger wrapper. There are several ways of using this logger. Just choose the best it suits you.

One of many uses (default logger)

const Log = require('digio-logger')

// This will create a default logger using the configuration 
// in config.js inside this package
const logger = Log.logger();

// Log an info message in a file called logs.log
logger.info('Sending messages to log', {
    extraData: 'extra extra!!',
    moreContextData: 'this is so funny!'
});

// Same as the first one
Log.log(Log.levels.info, 'test', {
    extraData: 'extra extra!!',
    moreContextData: 'this is so funny!'
});

Every time you make use of Log.logger({/*winstonSettings*/}, {/*logSettings*/}) you are defining a custom log and its instance is used for every next log, until you add a new one. If you don't add any settings to the logger it will use the default settings provided by config.js, placed into the package folder.

Opinionated stuff

In order to identify each log message properly from any log processor, we do believe in logger tagging with "actions" and "logTypes". Actions are defined constants strings that give a log some meaning. Log Types basically add more context information so you can group all actions under the same log type.

By default digio-logger make use of JSON logs, which basically means that it creates log files where each line is a JSON object.

Custom logger example (opinionated)

const Log = require('digio-logger')
const transports = Log.winstonTransports;
            
// Create a custom logger
const logger2 = Log.logger({
    transports: [
        new transports.Console({
            // Define the logs format
            level: 'debug',
            format: Log.templates.console
        })
    ]
},{
    channel: 'logger2',
    filename: 'another_log_file.log',
    path: './logs/'
});

// Even though we're not using 'logger2' we use the same logger 
// as it was set as the default logger in the lines before
Log.info(Log.actions.worker.timeout, 'logtype',{
    extra: 'extra info del error',
    whatever: 'whatever extra info'
},'This is an irrelevant message');
    

In this case we can add a second set of settings to customize the log name and a channel tag, this will be added to the base log the logger produces. By default the path is empty, so it will set the log into the root folder of your index.js file if there is no custom transport defined.

{
    level: level,
    msg: msg,
    action: action,
    logType: logType,
    context: context,
    channel: defaultConfig.log.channel || 'default-channel'
}

Useful resources

const Log = require('digio-logger')

// You can find useful data in the form of constants inside Log

// These are the levels supported by the logger
const levels = Log.levels;
// Here you can find predifined and formated templates for your logs
const templates = Log.templates;   
// These are useful to add new transports
const transports = Log.winstonTransports;
// And as you can read, this is the default config
cosnt defaultConfig = Log.config;

// You can also set 2 default loggers and forget about console.log
Log.logger({
    transports: [
        new Log.winstonTransports.File({
            filename: 'logs2.log',
            format: Log.templates.file
        }),
        new Log.winstonTransports.Console({
            format: Log.templates.console
        })
    ]
});

// This will log in two diferents places at once
Log.info(Log.actions.worker.timeout,'logtype',{contextData:'data'})

Remove the current logger

When a logger is defined, then it is used forever until a new transport is set for the logger. To start clean, we can use the following:

Log.removeCurrentLogger();

This will destroy the current logger, so a new empty logger will use the default configuration. This is usefull when we use the second set of configuration options.

Check an example in test.js, logger4.

Keep in mind, that destroying the current object, and using any log method afterwards will lead to an error or exception.

Take a look to the test.js file

=> node test.js // inside this package

And take a moment to see how it works

It may continue....