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

commons-logger

v1.0.4

Published

Note: The recommend way of using this logger is by initializing it in centralize way i.e. global way

Downloads

154

Readme

Commons Logger for application

Quick Start

Step 1: Import the package in your app init step

Note: The recommend way of using this logger is by initializing it in centralize way i.e. global way

const { winstonInit, setConfig, changeLevel } = require('commons-logger')

const config = {
    serviceName: "commons-logger",
    localLevel: null,
    globalLevel: 'info',
    rotationFrequency: "1d",
    writeLogsToFile: true,
    basePath: '/devopsmount'
}

// Only if you wanna change the default values
setConfig(config)

Supported config can be found in the table below

| Config Key | Default Value | Other Examples | Description | |-------------------|-------------------|--------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------| | serviceName | commons-logger | any service name | This is used as a reference if there are multiple apis or service | | localLevel | null | error, warn, info, http, log, debug, silly | The temporary level to store when runtime level is changed | | globalLevel | info | error, warn, info, http, log, debug, silly | The default global level. If at runtime level is changed it will be defaulted to global | | rotationFrequency | 1d | 2h, 1d, 1w, 1M | It consists of 2 parts: . periods supported are: h | d | w | M. hour, day, week, month respectively | | writeLogsToFile | true | true false | If false it will only output to your console. If true it will also maintain the .log files in your persistent drive storage | | basePath | /devopsmount | any path | If writeLogsToFile key is set to true it will write the files to disk in this path |

Step 2 Use winston logger anywhere you need

Once config is defined you can use this logger anywhere. There are 2 ways to use this file.

  1. Temporary
  2. Globally
// Temporary Use in very few files

const { winstonInit } = require('commons-logger')
const logger = winstonInit()

const message = 'Hi from logger'

// Use the logger methods to suit your needs
logger.error(message)
logger.warn(message)
logger.info(message)
logger.http(message)
logger.log(message)
logger.debug(message)
logger.silly(message)

// Output:
// ERROR   05/01/2023, 12:24:36 pm : Hi from logger
// WARN    05/01/2023, 12:24:36 pm : Hi from logger
// INFO    05/01/2023, 12:24:36 pm : Hi from logger
// HTTP    05/01/2023, 12:24:36 pm : Hi from logger
// VERBOSE 05/01/2023, 12:24:36 pm : Hi from logger
// DEBUG   05/01/2023, 12:24:36 pm : Hi from logger
// SILLY   05/01/2023, 12:24:36 pm : Hi from logger

After setting the config you can directly invoke the init method and assign that to a global variable.
It will be helpful if you want to use logger in many files

// Global way
const { winstonInit } = require('commons-logger')
global.logger = winstonInit()

// Use logger function anywhere in your app without importing winston package 
logger.error(message)

How log level works?

The available log levels are:

Logging levels in winston conform to the severity ordering specified by RFC5424: severity of all levels is assumed to be numerically ascending from most important to least important.

const levels = {
  error: 0,
  warn: 1,
  info: 2,
  http: 3,
  verbose: 4,
  debug: 5,
  silly: 6
}

So logger will output all the logs that matches it numerical level and the number less than that.
Eg: level = 'info' // numerical value is 2
so will output all the levels that are <= 'info'
i.e. error, warn and info in our case

Logging to file in case of winston fails or before init winston

Now what if we got an error/exception and the nodejs app shutdown before winston has the change to log?
In such case we have fileLog method at our disposable!

```javascript
const { fileLog } = require('commons-logger')

fileLog('THe error')
// this function will accept a string(as of this version) so if passing an object make sure to stringify

winston.fileLog(JSON.stringify({status:false, message: 'AN unknown error occured', data: [1,2,3]}))
```

Changing log level at runtime

There is also a function to change the loglevel runtime. You need to call the below api and pass in a winston supported loglevel and timeOut key.

const { changeLevel } = require('commons-logger')

const resp = changeLevel('debug', 60000)
console.log(resp) // { before: 'info', after: 'debug' }

// It accepts 2 arguments:
// logLevel :     This will be set at runtime
// timeOut  :     The number of milliseconds after which level will be reverted back. 60 seconds in above example

Argument supported in logger function

logger.function() accepts the set of arguments and it extends the winston arguments for extra arguments. YOu can pass any combination of arguments which you'd normally pass in javascript default console class

For Example:

const name = 'winston'
console.log("Hey there. I am from ", name, {array: [1,2,3]})

// You can use same args for logger 
logger.info("Hey there. I am from ", name, {array: [1,2,3]})