Skip to content

Commit

Permalink
audit update
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed May 14, 2018
1 parent 835b57e commit a831300
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 266 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ sudo: false

node_js:
- '4'
- '5'
- '6'
- '7'
- '8'
- '10'

env:
global:
Expand Down Expand Up @@ -37,7 +36,8 @@ before_script:

script:
- npm test
- npm audit



after_script:
- npm run coveralls
Expand Down
94 changes: 92 additions & 2 deletions lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
var Util = require('util')

var Stringify = require('json-stringify-safe')
var LogFilter = require('seneca-log-filter')
var _ = require('lodash')

var Print = require('./print')
Expand Down Expand Up @@ -41,7 +40,7 @@ logging.preload = function() {
}
}

var logrouter = LogFilter(logspec)
var logrouter = logfilter(logspec)

var logger = function(seneca, data) {
if (logrouter(data)) {
Expand Down Expand Up @@ -144,3 +143,94 @@ logging.preload = function() {
}
}
}


// TODO: needs massive refactor

function logfilter(options) {
let level = options.level || 'info+'

let calculatedLevels = []

if (level_exists(level)) {
calculatedLevels.push(level)
} else if (_.endsWith(level, '+')) {
// Level + notation
calculatedLevels = log_level_plus(level.substring(0, level.length - 1))
} else {
// No level nor level+... it must be a custom alias
let processedAliases = Object.assign({}, aliases, options.aliases)
let aliasInfo = processedAliases[level]
if (aliasInfo) {
let handled = _.get(aliasInfo, 'handled', true)
if (handled) {
calculatedLevels = aliasInfo.handler(options)
}
}
}

return function filter(data) {
if (calculatedLevels.indexOf(data.level) !== -1) {
let cloned = _.clone(data)
if (options['omit-metadata']) {
cloned = _.omit(cloned, ['seneca', 'level', 'when'])
}
if (options.omit && _.isArray(options.omit)) {
cloned = _.omit(cloned, options.omit)
}
return cloned
}
return null
}
}


var aliases = {
silent: {
handled: true,
handler: function() {
return []
}
},
all: {
handled: true,
handler: function() {
return ['debug', 'info', 'warn', 'error', 'fatal']
}
},
test: {
handled: true,
handler: function() {
return ['error', 'fatal']
}
}
}


const log_levels = ['debug', 'info', 'warn', 'error', 'fatal']

/**
* It returns the levels above the argument
* @param {String} logLevel the log level to calculate
* @return {Array} the list of logs above the argument
*/
function log_level_plus(logLevel) {
let index = log_levels.indexOf(logLevel)
if (index < 0) {
return []
} else {
return log_levels.slice(index, log_levels.length)
}
}

/**
* Checks if a log level exists
* @param {string} level the level itself
* @return {boolean} true if the level exists
*/
function level_exists(level) {
return log_levels.indexOf(level) !== -1
}

module.exports.log_level_plus = log_level_plus
module.exports.level_exists = level_exists
Loading

0 comments on commit a831300

Please sign in to comment.