uci-utils-logger/readme.md

4.1 KiB

`# uCOMmandIt JSON Logger

json logger based on pino.

How to by Example

see example for explanation of options and use in a es6 class allowing logging in any method of the class

      // pretty: {translateTime:true, colorize:true, levelFirst:true } // options for pino pretty printer
      // env:'', // 'dev' or 'pro' -- can be use to set/override UCI_ENV environment variable
      // enForce: false, // only used with .evn.  if true will override UCI_ENV if it is set, otherwise no
      // level:'info', // info is default level, set level to lowest visible
      // clear: false, // true for log files will clear the current log file on restart
      // logFileName:'test', // if not supplied log filename will be generated from timestamp
      // below properties are optional and are passed to child logger instance and will be part of json log entry
      // libraryName: 'UCI', // will be logged as name:  can be used to identify logs of related packages,  can be set via UCI_LOG_NAME env variable
      appName:'uci-example-logger', //will be used for logging directory name if supplied and will be logged as well
      package: '@uci/logger', // name of package with scope per package.json
      // repo: // will use scope-name from package prop if not supplied
      id: opts.id, // can pass a unique if for easy later search/filtering
      // file: 'example/example.js', // path (to repo root) of actual file running this logger, default is ./src/<package without scope>.js
      class: 'LogTest', // The class that created this logger if any
      // one can pass through additional props to log for every log
      // should not use any keys above or
      //  level,time,msg,pid,hostname
      // logPath,instanceCreatedHR,instanceCreated as keys will be merged
      // NOTE: more props can be added at run time by passing an object of props
      additional: {anotherprop:'test'} // should do not use any keys above or 'level,logPath,package,instanceCreatedHR,instanceCreated' as keys will be merged

to run the example clone reop and then npm install from root

then

npm run [script]

available scripts for example file

  • none: no logs to console or file
  • dev: all levels pretty printed to console/stdout
  • dev:info: show/filter only level 30 (info) logs pretty printed to console
  • dev:fatal: show/filer only level 60 (fatal) logs pretty printed to console
  • json: log to console but as raw json
  • pro: log to a file in the userspace default log directory location
  • pro:alt: log to a supplied/alternate file (path) location in this case ./example/example.log

Environment Variables

  • UCI_ENV='dev'|'pro'|'node', dev outputs to console, pro outputs to file, node will follow whatever NODE_ENV is set to.
  • UCI_LOG_PATH=<some file path> force logging to go to some particular file, also setable via options, see above
  • UCI_LOG_JSON=true will not pretty output to console but rather as raw json. Useful for piping to a json log processor such as pino-colada
  • UCI_LOG_PRETTY={} the pretty option for pino pretty, also setable via options, see above
  • UCI_LOG_SEARCH='key == `value`' will filter pretty/dev logs. note: ALL search values regardless of type MUST be escaped with ``

Usage

standalone (not in a class constructor as in example) lilke this.

import logger from '../src/logger' opts = {} // see above let log = logger(opts)

once you have created a Logger as a global in your module you can log per http://getpino.io/#/docs/api?id=logger

like so

log.trace('this is a trace level logged message')
log.debug('this is a debug level logged message')
log.info({runtimeprop:'somevalue', msg:'this is a info level logged message'})
log.warn('this is a warn level logged message')
log.error('this is a error level logged message')
log.fatal('this is a fatal level logged message')

you can change visible levels on the fly http://getpino.io/#/docs/api?id=level

like so

log.level('debug') or in the options, see above

More Help

this package/model being a simple extension of the pino json logger check their documentation http://getpino.io/#/