1.0.4 • Published 1 year ago

commons-logger v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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 KeyDefault ValueOther ExamplesDescription
serviceNamecommons-loggerany service nameThis is used as a reference if there are multiple apis or service
localLevelnullerror, warn, info, http, log, debug, sillyThe temporary level to store when runtime level is changed
globalLevelinfoerror, warn, info, http, log, debug, sillyThe default global level. If at runtime level is changed it will be defaulted to global
rotationFrequency1d2h, 1d, 1w, 1MIt consists of 2 parts: . periods supported are: h | d | w | M. hour, day, week, month respectively
writeLogsToFiletruetrue falseIf false it will only output to your console. If true it will also maintain the .log files in your persistent drive storage
basePath/devopsmountany pathIf 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]})