uci-utils-logger/example/example.js

98 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

import logger from '../src/logger.js'
import testsrc from './test.js'
2019-01-25 12:36:57 -08:00
let log = {}
const options = {
// custom UCI logger options
// noID: true,
// IDKey: '_id' // default
hide: ['meta', 'machine'], // array of properties to hide
// 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
// 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
//
// override logger default and or add any pino options
pino: {
// level:'info', // info is default level, overrides UCI_LOG_LEVEL, can be changed at runtime with .level method
// name: either libraryName above or UCI_LOG_NAME or default 'UCI',
// enabled: only if UCI_ENV is set
// nestedKey: 'props' // nest the runtime custom log properties
// safe: true,
// timestamp: pino.stdTimeFunctions.epochTime,
// serializers: {
// req: pino.stdSerializers.req,
// res: pino.stdSerializers.res
// },
// prettyPrint: pretty,
// prettifier: filter // only call prefilter if some filter is supplied, otherwise see line 53
2019-01-25 12:36:57 -08:00
}
}
const meta = {
// meta properties added to all log output
// the following base meta props and will be generated with defaults if not set
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
// library: 'uci', // will be stripped from @ organization of package if not supplied
// repo: // will use scope-name from package prop if not supplied
// file: 'example/example.js', // path (to repo root) of actual file running this logger, default is ./src/<package without scope>.js
// file: import.meta.url // use this to get a dynmic absolute path to the file where logger is created
// class: 'LogTest' // otherwise will be generated from package
// class: false // set to false to disable meta class property
// add any additional properties to meta data here
// an example
// id: // maybe a unique id to make is easier to search and/or filter these logs
}
2019-01-25 12:36:57 -08:00
class LogTest {
constructor () {
log = logger(meta, options)
} // end constructor
logit () {
log.trace('this is a trace level logged message')
log.debug({ line: log.locate(), msg: 'this is a debug level logged message with a line number' })
log.info({ runtimeprop: 'some propertey 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')
}
2019-01-25 12:36:57 -08:00
}
// let test = new LogTest({id:'id-via new instance', pretty:{include:'all'}})
const test = new LogTest()
2019-01-25 12:36:57 -08:00
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()
console.log('\n-------- hide props------------')
log.info({ test1: 'test1', test2: 'test2' }, 'testing')
log.hide('test1')
console.log('hiding test1')
log.info({ test1: 'test1', test2: 'test2' }, 'hiding test1')
log.hide('test2')
console.log('hiding test2')
log.info({ test1: 'test1', test2: 'test2' }, 'hiding test2')
log.unhide(['test1', 'test2'])
console.log('unhiding both')
log.info({ test1: 'test1', test2: 'test2' }, 'hiding test2')
console.log('\n-------- include line and file ------------')
log.info({ loc: log.locate() }, 'log line:file')
log.info({ line: log.locate('n') }, 'log line')
log.info({ file: log.locate('f') }, 'log file')
log.info({ file: log.locate(1) }, 'log loc alternate stack')
testsrc(log)