move to native esm
simplify custom filter function
master
Kebler Network System Administrator 2021-04-24 09:09:07 -07:00
parent 89abc92833
commit e776937738
5 changed files with 33 additions and 45 deletions

View File

@ -1,4 +1,4 @@
import logger from '../src/logger' import logger from '../src/logger.js'
let log = {} let log = {}
class LogTest { class LogTest {
@ -16,9 +16,9 @@ class LogTest {
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/test', // 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: 'id set in package', // can pass a unique if for easy later search/filtering 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 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
/*---------------- /*----------------
Can pass through additional props for every log by including this `additional` property object Can pass through additional props for every log by including this `additional` property object

View File

@ -1,13 +1,13 @@
{ {
"name": "@uci-utils/logger", "name": "@uci-utils/logger",
"version": "0.0.18", "version": "0.1.0",
"description": "Parent Logger for all UCI modules", "description": "Parent Logger for all UCI modules",
"main": "./src/logger.js", "main": "./src/logger.js",
"exports": "./src/logger.js", "type": "module",
"scripts": { "scripts": {
"testd": "./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000'", "testd": "./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha --timeout 30000' || exit 1",
"test": "./node_modules/.bin/mocha -r esm --timeout 30000 || exit 0", "test": "./node_modules/.bin/mocha --timeout 30000 || exit 0",
"dev": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm example/example", "dev": "UCI_ENV=dev ./node_modules/.bin/nodemon example/example",
"dev:verbose": "UCI_LOG_PRETTY='verbose' npm run dev", "dev:verbose": "UCI_LOG_PRETTY='verbose' npm run dev",
"dev:terse": "UCI_LOG_PRETTY='terse' npm run dev", "dev:terse": "UCI_LOG_PRETTY='terse' npm run dev",
"dev:where": "UCI_LOG_PRETTY='where' npm run dev", "dev:where": "UCI_LOG_PRETTY='where' npm run dev",
@ -18,7 +18,7 @@
"dev:info:only": "UCI_LOG_SEARCH='level==`30`' npm run dev", "dev:info:only": "UCI_LOG_SEARCH='level==`30`' npm run dev",
"dev:fatal:only": "UCI_LOG_SEARCH='level==`60`' npm run dev", "dev:fatal:only": "UCI_LOG_SEARCH='level==`60`' npm run dev",
"dev:json": "UCI_LOG_LEVEL=trace UCI_LOG_JSON=true npm run dev", "dev:json": "UCI_LOG_LEVEL=trace UCI_LOG_JSON=true npm run dev",
"pro": "UCI_ENV='pro' node -r esm example/example", "pro": "UCI_ENV='pro' node example/example",
"pro:path": "UCI_LOG_PATH=./example/example.log npm run pro" "pro:path": "UCI_LOG_PATH=./example/example.log npm run pro"
}, },
"author": "David Kebler", "author": "David Kebler",
@ -37,16 +37,15 @@
}, },
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme", "homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
"dependencies": { "dependencies": {
"env-paths": "^2.2.0", "env-paths": "^2.2.1",
"make-dir": "^3.1.0", "make-dir": "^3.1.0",
"pino": "^6.3.0", "pino": "^6.11.3",
"pino-pretty": "^4.0.0" "pino-pretty": "^4.7.1"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.2.0", "chai": "^4.3.4",
"esm": "^3.2.25", "mocha": "^8.3.2",
"mocha": "^7.2.0", "nodemon": "^2.0.7",
"nodemon": "^2.0.4",
"test-console": "^1.1.0" "test-console": "^1.1.0"
} }
} }

View File

@ -1,4 +1,4 @@
# uCOMmandIt JSON Logger # UCommandIt JSON Logger
### json logger based on [pino](getpino.io). ### json logger based on [pino](getpino.io).

View File

@ -11,8 +11,8 @@ function child (opts) {
let pretty; let filter; let LOG_PATH; let DATE_TIME let pretty; let filter; let LOG_PATH; let DATE_TIME
const PRETTY_DEFAULTS = {translateTime:true, colorize:true, levelFirst:true} const PRETTY_DEFAULTS = {translateTime:true, colorize:true, levelFirst:true}
const DEFAULT_FILTER_PROPS = ['level','time','v','pid','msg'] const DEFAULT_FILTER_PROPS = ['level','time','pid','msg']
const WHERE_FILTER_PROPS = ['package','file','method','function','line'] const WHERE_FILTER_PROPS = ['package','file','class','method','function','line']
let logOpts = { let logOpts = {
level:opts.level || process.env.UCI_LOG_LEVEL, level:opts.level || process.env.UCI_LOG_LEVEL,
@ -27,21 +27,18 @@ function child (opts) {
instanceCreated:Date.now() instanceCreated:Date.now()
} }
// logOpts = Object.assign(logOpts,opts.additional)
// console.log('log opts', logOpts)
if (enabled) { if (enabled) {
if (opts.env) (opts.envForce) ? (process.env.UCI_ENV = opts.env) : (process.env.UCI_ENV = process.env.UCI_ENV || opts.env) 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_ENV === 'node') process.env.UCI_ENV = process.env.NODE_ENV if (process.env.UCI_ENV === 'node') process.env.UCI_ENV = process.env.NODE_ENV
if((process.env.UCI_ENV.indexOf('dev')>-1 || process.env.UCI_LOG_PRETTY) && process.env.UCI_LOG_JSON !=='true' ) { // pretty is on if((process.env.UCI_ENV.indexOf('dev')>-1 || process.env.UCI_LOG_PRETTY) && process.env.UCI_LOG_JSON !=='true' ) { // pretty is on
// process UCI_LOG_PRETTY
try { try {
pretty = JSON.parse(process.env.UCI_LOG_PRETTY) pretty = JSON.parse(process.env.UCI_LOG_PRETTY)
} }
catch (error) { catch (error) {
// console.log('LOGGER: could not parse',process.env.UCI_LOG_PRETTY ) // console.log('LOGGER: could not JSON parse',process.env.UCI_LOG_PRETTY )
if (process.env.UCI_LOG_PRETTY==='verbose') pretty ={include:'all'}
} }
if (process.env.UCI_LOG_PRETTY==='verbose') pretty ={include:'all'}
pretty = pretty || opts.pretty || {} pretty = pretty || opts.pretty || {}
pretty = Object.assign({},PRETTY_DEFAULTS,pretty) pretty = Object.assign({},PRETTY_DEFAULTS,pretty)
pretty.search = process.env.UCI_LOG_SEARCH pretty.search = process.env.UCI_LOG_SEARCH
@ -55,6 +52,7 @@ function child (opts) {
pretty.ignore = null pretty.ignore = null
} }
} }
DATE_TIME = new Date().toString() DATE_TIME = new Date().toString()
LOG_PATH = ( process.env.UCI_ENV.indexOf('pro') > -1 || process.env.UCI_ENV === 'logfile' ) LOG_PATH = ( process.env.UCI_ENV.indexOf('pro') > -1 || process.env.UCI_ENV === 'logfile' )
@ -65,10 +63,9 @@ function child (opts) {
} }
} // end enabled } // end enabled
// console.dir(pretty)
// console.dir(logOpts)
// console.log('pretty terse logger', pretty.terse)
// console.log('making logger \n(env,json,pretty,pretty-search,path)\n',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: enabled, enabled: enabled,
@ -101,33 +98,25 @@ function child (opts) {
} }
export default child export default child
const levels = pino.levels
export {child as logger, levels}
function capitalize (s) { return s.charAt(0).toUpperCase() + s.slice(1)} function capitalize (s) { return s.charAt(0).toUpperCase() + s.slice(1)}
function filterPretty (options) { function filterPretty (options) {
// console.log('filter options',options)
return function prettifier (inputData) { return function prettifier (inputData) {
// console.log('calling filter',inputData)
let log let log
let parsedData let parsedData
if (typeof inputData === 'string') { if (typeof inputData === 'string') {
try { parsedData = JSON.Parse(inputData)} try { log = JSON.Parse(inputData)}
catch(err){ return inputData } catch(err){ return inputData }
log = (isPinoLog(parsedData)) ? parsedData : undefined
} else if (isObject(inputData) && isPinoLog(inputData)) {
log = inputData
} }
if (!log) return inputData else log = inputData
let filteredLog = {} let filteredLog = {}
options.filter.forEach(prop => filteredLog[prop]= log[prop]) options.filter.forEach(prop => filteredLog[prop]= log[prop])
// console.log('filtered',options.filter, filteredLog) // console.log('filtered',options.filter, filteredLog)
// if (options.pid) console.log('pid:',logObject.pid)
return pinoPretty(options)(filteredLog) return pinoPretty(options)(filteredLog)
} }
function isObject (input) {
return Object.prototype.toString.apply(input) === '[object Object]'
}
function isPinoLog (log) {
return log && (log.hasOwnProperty('v') && log.v === 1)
}
} }

View File

@ -1,4 +1,4 @@
import logger from '../src/logger' import logger from '../src/logger.js'
import { expect } from 'chai' import { expect } from 'chai'
import { it } from 'mocha' import { it } from 'mocha'
import { stdout } from 'test-console' import { stdout } from 'test-console'
@ -16,7 +16,7 @@ describe('JSON Logging Utility Testing ',async ()=> {
log = logger({ log = logger({
name: 'test', name: 'test',
id: opts.id, id: opts.id,
// file: 'example/example.js', file: 'example/example.js',
// class: 'LogTest', // class: 'LogTest',
// repo:'test repo' // repo:'test repo'
package:'@uci-utils/test' package:'@uci-utils/test'