Skip to content

Generic logging utility focused around a plugin-architecture to be used in projects and APIs.

License

Notifications You must be signed in to change notification settings

ExpressiveJS/logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logger

Generic logging utility focused around a plugin-architecture to be used in projects and APIs.


Features:

  • Plugin architecture allows it to be hooked up to external logging services, databases, files, etc
  • Plugin can provide optional config (with sane defaults)
  • User can overwrite config options
  • Plugins receive a sender identifier object to point to code paths, plugins, functions, etc

Install:

npm install @expressivejs/logger

Use in project:

const logger = require('@expressivejs/logger')
logger.load([ 'examplePlugin' ])

// Somewhere else in project
const logAPI = logger.API()
logAPI.log('I am Logging')

Output:

$ I am a Logging

Example Plugin:

module.exports = {
  extends: ['log'],
  
  init: function(config) {
    // Every logger must impement an init function.
  },

  log: function(sender, message) {
    if (sender.name)
      console.log(`[${sender.name} in ${sender.type}] ${message}`)
    else
      console.log(`[${sender.type}] ${message}`)
  }
}




Example user config:

const exampleUserConfig = {
  examplePlugin: { 
    someProp: true,
  },
}

logger.load([ 'examplePlugin' ], exampleUserConfig)

Path to loggers:

Uses path.resolve() behind the scenes.

logger.load([ 'examplePlugin' ], null, './loggers')

Example Plugin Config:

module.exports = {
  someProp: false,
  otherProp: true,
}

Merged Config Output:

 { someProp: true, otherProp: true }

Given the above examples, a Plugin provides default config options - however, the user overwrites props provided to exampleUserConfig.

  1. In this case, someProp is defined false by the plugin
  2. The user config (exampleUserConfig) says they want someProp to be true
  3. Other default config options defined by the Plugin are untouched.



Sender identifier params:

Sender can be an object if you require more properties or string. If Sender is a string, an additional param can be used for more information.


Sender identifier Example Use:

const logAPI = logger.API('someModule')
logAPI.log('I am a Logging')

Output:

$ [someModule] I am a Logging

Sender identifier Extra Param:

const logAPI = logger.API('someModule', 'someFunction')
logAPI.log('I am a Logging')

Output:

$ [someFunction in someModule] I am a Logging

Sender as object:

const logAPI = logger.API({ functionName: 'someFunction', line: 11 })
logAPI.log('I am a Logging')

About

Generic logging utility focused around a plugin-architecture to be used in projects and APIs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published