Skip to content

Instantly share code, notes, and snippets.

@sureshnath
Created May 1, 2019 10:29
Show Gist options
  • Save sureshnath/c8e968ead5a25774fb0b603727acd7c3 to your computer and use it in GitHub Desktop.
Save sureshnath/c8e968ead5a25774fb0b603727acd7c3 to your computer and use it in GitHub Desktop.
postman-logging-with-level
// [email protected] - start
var util = require('util')
var levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
var noop = function () {}
var getLogger = function (opts) {
opts = opts || {}
opts.level = opts.level || 'info'
var logger = {}
var shouldLog = function (level) {
return levels.indexOf(level) >= levels.indexOf(opts.level)
}
levels.forEach(function (level) {
logger[level] = shouldLog(level) ? log : noop
function log () {
var prefix = opts.prefix
var normalizedLevel
if (opts.stderr) {
normalizedLevel = 'error'
} else {
switch (level) {
case 'trace': normalizedLevel = 'info'; break;
case 'debug': normalizedLevel = 'info'; break;
case 'fatal': normalizedLevel = 'error'; break;
default: normalizedLevel = level
}
}
if (prefix) {
if (typeof prefix === 'function') prefix = prefix(level);
arguments[0] = util.format(prefix, arguments[0]);
}
console[normalizedLevel](util.format.apply(util, arguments))
}
})
return logger
}
// [email protected] - end
var logLevel = pm.globals.get("logLevel") || 'info'
var log = getLogger({ level: logLevel })
var logp = getLogger({
prefix: function (level) {
return new Date().toISOString()
},
level: logLevel
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment