uci-utils-logger/example/example.js

58 lines
2.8 KiB
JavaScript

import logger from '../src/logger'
let log = {}
class LogTest {
constructor(opts) {
log = logger(
Object.assign({
// 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:'debug', // 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 are BASE properties which 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/test', // name of package with scope per package.json
// repo: // will use scope-name from package prop if not supplied
id: 'id set in package', // 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
/*----------------
Can pass through additional props for every log by including this `additional` property object
should not use any keys above or level,time,msg,pid,hostname,
logPath,instanceCreatedHR,instanceCreated as keys in this object will be merged
------------------*/
// additional: {anotherprop:'test'}
},opts)
)
}
logit() {
log.trace('this is a trace level logged message')
log.debug({line:343,msg:'this is a debug level logged message with a line number'})
log.info({runtimeprop:'some propetery in context', 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')
}
}
// let test = new LogTest({id:'id-via new instance', pretty:{include:'all'}})
let test = new LogTest()
log.div(`the default log level based on option or UCI_LOG_LEVEL ${log.level}`)
test.logit()
log.lvlset('warn')
log.div(`now changing log level manually in code to ${log.level}`)
test.logit()
log.lvlset('trace')
log.div(`now changing log level to lowest (view all levels) ${log.level}`)
test.logit()
log.lvlset()
log.div(`reset level back current default ${log.level}`)
test.logit()
log.clear('This call to log.clear could clear the file log when implemented')