.default value,
.clear method (does nothing yet)
.div method adds a divider using console log but only on output to console.
.lvlset  a method to set the level that doesn't get called if logging is disabled which turns on logging.
wrapped all the prelim stuff in enabled valued so it doesn't get called with logger is disabled.
master
David Kebler 2019-02-05 17:57:19 -08:00
parent 6a0b07dd40
commit 72adfffbeb
3 changed files with 57 additions and 40 deletions

View File

@ -7,23 +7,24 @@ class LogTest {
// pretty: {translateTime:true, colorize:true, levelFirst:true } // options for pino pretty printer // 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 // 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 // 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 // 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 // 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 // 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 // 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 // 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 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 package: '@uci/test', // name of package with scope per package.json
// repo: // will use scope-name from package prop if not supplied // repo: // will use scope-name from package prop if not supplied
id: opts.id, // can pass a unique if for easy later search/filtering 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 // 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 // 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 Can pass through additional props for every log by including this `additional` property object
// logPath,instanceCreatedHR,instanceCreated as keys will be merged should not use any keys above or level,time,msg,pid,hostname,
// NOTE: more props can be added at run time by passing an object of props logPath,instanceCreatedHR,instanceCreated as keys in this object will be merged
additional: {anotherprop:'test'} // should do not use any keys above or 'level,logPath,package,instanceCreatedHR,instanceCreated' as keys will be merged ------------------*/
// additional: {anotherprop:'test'}
}) })
} }
@ -34,11 +35,17 @@ class LogTest {
log.warn('this is a warn level logged message') log.warn('this is a warn level logged message')
log.error('this is a error level logged message') log.error('this is a error level logged message')
log.fatal('this is a fatal level logged message') log.fatal('this is a fatal level logged message')
log.clear('This would clear the log')
} }
} }
let test = new LogTest({id:'logtest-via-new'}) let test = new LogTest({id:'logtest-via-new'})
log.div(`the default log level based on option or UCI_LOG_LEVEL ${log.default}`)
test.logit() test.logit()
log.lvlset('fatal')
log.div(`now changing log level manually in code to ${log.level}`)
test.logit()
log.div(`reset level back current default ${log.default}`)
log.lvlset()
test.logit()
log.clear('This call to log.clear could clear the file log when implemented')

View File

@ -1,6 +1,6 @@
`# uCOMmandIt JSON Logger # uCOMmandIt JSON Logger
### json logger based on pino. ### json logger based on [pino](getpino.io).
## How to by Example ## How to by Example

View File

@ -5,32 +5,31 @@ import { dirname, basename } from 'path'
function child (opts) { function child (opts) {
if (opts.env) (opts.envForce) ? (process.env.UCI_ENV = opts.env) : (process.env.UCI_ENV = process.env.UCI_ENV || opts.env) const enabled = !!process.env.UCI_ENV
if (process.env.UCI_ENV === 'node') process.env.UCI_ENV = process.env.NODE_ENV let pretty = false; let LOG_PATH; let DATE_TIME
let pretty = false if (enabled) {
if(process.env.UCI_ENV === 'dev' || process.env.UCI_ENV === 'development') { if (opts.env) (opts.envForce) ? (process.env.UCI_ENV = opts.env) : (process.env.UCI_ENV = process.env.UCI_ENV || opts.env)
if (process.env.UCI_LOG_JSON !=='true') { if (process.env.UCI_ENV === 'node') process.env.UCI_ENV = process.env.NODE_ENV
pretty = process.env.UCI_LOG_PRETTY || opts.pretty || {translateTime:true, colorize:true, levelFirst:true } if(process.env.UCI_ENV.indexOf('dev')>-1) {
pretty.search = process.env.UCI_LOG_SEARCH if (process.env.UCI_LOG_JSON !=='true') {
pretty = process.env.UCI_LOG_PRETTY || opts.pretty || {translateTime:true, colorize:true, levelFirst:true }
pretty.search = process.env.UCI_LOG_SEARCH
}
}
DATE_TIME = new Date(Date.now()).toISOString().replace( /[:.]+/g, '-' )
LOG_PATH = ( process.env.UCI_ENV.indexOf('pro') > -1 || process.env.UCI_ENV === 'logfile' )
? ( process.env.UCI_LOG_PATH || `${envPaths(opts.appName || opts.name || 'default').log}/${opts.logFileName || DATE_TIME}.log`) : undefined
if (LOG_PATH) {
try { mkdir(dirname(LOG_PATH)) }
catch(err) { throw err }
} }
} }
const DATE_TIME = new Date(Date.now()).toISOString().replace( /[:.]+/g, '-' )
const LOG_PATH = (process.env.UCI_ENV=== 'production' || process.env.UCI_ENV=== 'pro' || process.env.UCI_ENV=== 'logfile' )
? ( process.env.UCI_LOG_PATH || `${envPaths(opts.appName || opts.name || 'default').log}/${opts.logFileName || DATE_TIME}.log`) : null
if (LOG_PATH) {
try { mkdir(dirname(LOG_PATH)) }
catch(err) { throw err }
}
// console.log('making logger (env,json,pretty,pretty-search,path)',process.env.UCI_ENV,process.env.UCI_LOG_JSON,pretty,process.env.UCI_LOG_SEARCH,LOG_PATH) // console.log('making logger (env,json,pretty,pretty-search,path)',process.env.UCI_ENV,process.env.UCI_LOG_JSON,pretty,process.env.UCI_LOG_SEARCH,LOG_PATH)
const logger = pino({ const logger = pino({
name: opts.libraryName || process.env.UCI_LOG_NAME || 'UCI', name: opts.libraryName || process.env.UCI_LOG_NAME || 'UCI',
enabled: !!process.env.UCI_ENV, enabled: enabled,
safe: true, safe: true,
serializers: { serializers: {
req: pino.stdSerializers.req, req: pino.stdSerializers.req,
@ -42,16 +41,14 @@ function child (opts) {
LOG_PATH LOG_PATH
) )
logger.clear = value => {console.log(value)}
let logOpts = { let logOpts = {
level:opts.level, level:opts.level || process.env.UCI_LOG_LEVEL,
logPath: LOG_PATH, // if logging to file logPath: LOG_PATH, // if logging to file
appName: opts.appName, appName: opts.appName,
repo: opts.repo || (opts.package) ? opts.package.replace( /[@]+/g, '' ).replace( /[/]+/g, '-' ) : null, repo: opts.repo || (opts.package) ? opts.package.replace( /[@]+/g, '' ).replace( /[/]+/g, '-' ) : undefined,
package: opts.package, package: opts.package,
file: opts.file || `src/${basename(opts.package)}.js`, file: opts.file || (opts.package) ? `src/${basename(opts.package)}.js` : undefined,
class: opts.class || (opts.name.charAt(0).toUpperCase() + opts.name.slice(1)), class: opts.class || (opts.package) ? (basename(opts.package).charAt(0).toUpperCase() + opts.package.slice(1)) : undefined,
id: opts.id || opts.name || 'none', id: opts.id || opts.name || 'none',
instanceCreatedHR:DATE_TIME, instanceCreatedHR:DATE_TIME,
instanceCreated:new Date().getTime() instanceCreated:new Date().getTime()
@ -59,7 +56,20 @@ function child (opts) {
logOpts = Object.assign(logOpts,opts.additional) logOpts = Object.assign(logOpts,opts.additional)
return logger.child(logOpts) let child = logger.child(logOpts)
child.default = opts.level || process.env.UCI_LOG_LEVEL
child.clear = () => {
if (enabled) {
if (process.env.UCI_ENV.indexOf('pro') > -1) return null } // enable feature here
}
child.div = value => {
if (enabled) {
if (process.env.UCI_ENV.indexOf('dev') > -1) console.log(`===== ${value} ========`)}
}
child.lvlset = (level) => { if (enabled) child.level = level || child.default || 'info' }
return child
} }
export default child export default child