Comparing version 1.1.21 to 1.1.22
376
lib/index.js
@@ -1,214 +0,214 @@ | ||
const winston = require('winston'); | ||
var _ = require('lodash'); | ||
var domain = require('domain'); | ||
const { MESSAGE } = require('triple-beam'); | ||
/* eslint-disable no-plusplus,node/no-unsupported-features */ | ||
const winston = require('winston'); | ||
const _ = require('lodash'); | ||
const { MESSAGE } = require('triple-beam'); | ||
var defaultOptions = { | ||
filePath: null, | ||
console: true, | ||
handleExceptions: false, | ||
showNamespace: false, | ||
level: "debug", | ||
namespaces: "codefresh,codefresh:*", | ||
consoleOptions: null, | ||
basePath: "", | ||
baseNamespace: "codefresh", | ||
fields: {}, | ||
const defaultOptions = { | ||
handleExceptions: false, | ||
showNamespace: false, | ||
level: 'debug', | ||
namespaces: 'codefresh,codefresh:*', | ||
consoleOptions: null, | ||
basePath: '', | ||
baseNamespace: 'codefresh', | ||
fields: {}, | ||
}; | ||
var globalOptions = defaultOptions; | ||
var names = []; | ||
var skips = []; | ||
let globalOptions = defaultOptions; | ||
const names = []; | ||
const skips = []; | ||
var setGlobalOptions = function(options){ | ||
if (!options){ | ||
throw new Error("failed to set globalOptions because no options object was provided"); | ||
} | ||
const setGlobalOptions = function (options) { | ||
if (!options) { | ||
throw new Error('failed to set globalOptions because no options object was provided'); | ||
} | ||
if (options.level){ | ||
if (typeof options.level !== 'string'){ | ||
throw new Error("failed to set globalOptions because debug must be a string"); | ||
} | ||
var levels = /error|warn|info|debug/; | ||
if (!levels.test(options.level)){ | ||
throw new Error("failed to set globalOptions because debug can only be one of these values: error/warn/info/debug"); | ||
} | ||
} | ||
if (options.level) { | ||
if (typeof options.level !== 'string') { | ||
throw new Error('failed to set globalOptions because debug must be a string'); | ||
} | ||
const levels = /error|warn|info|debug/; | ||
if (!levels.test(options.level)) { | ||
throw new Error('failed to set globalOptions because debug can only be one of these values: error/warn/info/debug'); | ||
} | ||
} | ||
globalOptions = _.assign(_.cloneDeep(defaultOptions), options); | ||
defaultLogger.resetLevel(); | ||
globalOptions = _.assign(_.cloneDeep(defaultOptions), options); | ||
defaultLogger.resetLevel(); | ||
}; | ||
var parseNamespacesFromEnvironment = function parseNamespacesFromEnvironment() { | ||
var namespaces = process.env.DEBUG || globalOptions.namespaces; | ||
var split = (namespaces || '').split(/[\s,]+/); | ||
var len = split.length; | ||
const parseNamespacesFromEnvironment = function () { | ||
let namespaces = process.env.DEBUG || globalOptions.namespaces; | ||
const split = (namespaces || '').split(/[\s,]+/); | ||
const len = split.length; | ||
for (var i = 0; i < len; i++) { | ||
if (!split[i]) continue; // ignore empty strings | ||
namespaces = split[i].replace(/\*/g, '.*?'); | ||
if (namespaces[0] === '-') { | ||
skips.push(new RegExp('^' + namespaces.substr(1) + '$')); | ||
} else { | ||
names.push(new RegExp('^' + namespaces + '$')); | ||
} | ||
} | ||
for (let i = 0; i < len; i++) { | ||
// eslint-disable-next-line no-continue | ||
if (!split[i]) continue; // ignore empty strings | ||
namespaces = split[i].replace(/\*/g, '.*?'); | ||
if (namespaces[0] === '-') { | ||
skips.push(new RegExp(`^${namespaces.substr(1)}$`)); | ||
} else { | ||
names.push(new RegExp(`^${namespaces}$`)); | ||
} | ||
} | ||
}; | ||
var isNamespaceEnabled = function enabled(namespace) { | ||
var i, len; | ||
for (i = 0, len = skips.length; i < len; i++) { | ||
if (skips[i].test(namespace)) { | ||
return false; | ||
} | ||
} | ||
for (i = 0, len = names.length; i < len; i++) { | ||
if (names[i].test(namespace)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
const isNamespaceEnabled = function (namespace) { | ||
let i; | ||
let len; | ||
for (i = 0, len = skips.length; i < len; i++) { | ||
if (skips[i].test(namespace)) { | ||
return false; | ||
} | ||
} | ||
for (i = 0, len = names.length; i < len; i++) { | ||
if (names[i].test(namespace)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
var Logger = function(namespace) { | ||
if (!namespace){ | ||
throw new Error("failed to create a new logger because namespace was not provided"); | ||
} | ||
if (typeof namespace !== 'string'){ | ||
throw new Error("failed to create a new logger because namespace must be a String"); | ||
} | ||
const Logger = function (namespace) { | ||
if (!namespace) { | ||
throw new Error('failed to create a new logger because namespace was not provided'); | ||
} | ||
if (typeof namespace !== 'string') { | ||
throw new Error('failed to create a new logger because namespace must be a String'); | ||
} | ||
if (globalOptions.basePath && | ||
namespace.indexOf(globalOptions.basePath) === 0) { | ||
if (globalOptions.basePath && | ||
namespace.indexOf(globalOptions.basePath) === 0) { | ||
var baseNamespace = globalOptions.baseNamespace || ""; | ||
var generatedNamespace = | ||
namespace.replace(new RegExp(globalOptions.basePath, 'g'), "").replace(/\//g, ":").replace(/\.js/g, ""); | ||
if (generatedNamespace[0] != ':') { | ||
generatedNamespace = ':' + generatedNamespace; | ||
} | ||
namespace = baseNamespace + generatedNamespace; | ||
} | ||
const baseNamespace = globalOptions.baseNamespace || ''; | ||
let generatedNamespace = | ||
namespace.replace(new RegExp(globalOptions.basePath, 'g'), '') | ||
.replace(/\//g, ':') | ||
.replace(/\.js/g, ''); | ||
if (generatedNamespace[0] !== ':') { | ||
generatedNamespace = `:${generatedNamespace}`; | ||
} | ||
namespace = baseNamespace + generatedNamespace; | ||
} | ||
parseNamespacesFromEnvironment(); | ||
var logger = winston.createLogger(); | ||
logger.level = globalOptions.level; | ||
parseNamespacesFromEnvironment(); | ||
const logger = winston.createLogger(); | ||
logger.level = globalOptions.level; | ||
//Log to file | ||
if (globalOptions && globalOptions.filePath){ | ||
if (globalOptions.handleExceptions){ | ||
logger.add(new winston.transports.File( { | ||
filename: globalOptions.filePath, | ||
handleExceptions: true | ||
})); | ||
} | ||
else { | ||
logger.add(new winston.transports.File( { | ||
filename: globalOptions.filePath | ||
})); | ||
} | ||
} | ||
if (globalOptions && globalOptions.consoleOptions && globalOptions.consoleOptions.formatter) { | ||
const formatter = { | ||
transform: (info, opts) => { | ||
info.timestamp = () => { | ||
return info.time; | ||
}; | ||
info[MESSAGE] = globalOptions.consoleOptions.formatter(Object.assign({}, info, opts)); | ||
return info; | ||
}, | ||
}; | ||
globalOptions.consoleOptions.format = formatter; | ||
} | ||
if (process.env.FORMAT_LOGS_TO_NORMAL === 'true') { | ||
globalOptions.consoleOptions = { format: standardFormatter }; | ||
} | ||
if (globalOptions && globalOptions.handleExceptions) { | ||
globalOptions.consoleOptions.handleExceptions = true; | ||
logger.add(new winston.transports.Console(globalOptions.consoleOptions)); | ||
} else { | ||
logger.add( | ||
new winston.transports.Console(Object.assign({}, globalOptions.consoleOptions)) | ||
); | ||
// logger.configure({transports: [new winston.transports.Console(Object.assign({}, globalOptions.consoleOptions))]}); | ||
} | ||
//Log to console | ||
if (!globalOptions || globalOptions.console){ | ||
if (globalOptions && globalOptions.consoleOptions && globalOptions.consoleOptions.formatter) { | ||
const formatter = { | ||
transform : (info, opts) => { | ||
info.timestamp = () => { return info.time; }; | ||
info[MESSAGE] = globalOptions.consoleOptions.formatter(Object.assign({}, info, opts)); | ||
return info; | ||
} | ||
}; | ||
globalOptions.consoleOptions.format = formatter; | ||
} | ||
if (globalOptions && globalOptions.handleExceptions){ | ||
consoleOptions.handleExceptions = true; | ||
logger.add(new winston.transports.Console(globalOptions.consoleOptions)); | ||
} | ||
else{ | ||
logger.add( | ||
new winston.transports.Console(Object.assign({}, globalOptions.consoleOptions)) | ||
); | ||
//logger.configure({transports: [new winston.transports.Console(Object.assign({}, globalOptions.consoleOptions))]}); | ||
} | ||
} | ||
logger.exitOnError = true; | ||
const addSpecificArguments = function (oldArguments) { | ||
if (isNamespaceEnabled(namespace) || namespace === 'CF_LOGS_DEFAULT') { | ||
const data = {}; | ||
if (globalOptions.showNamespace && namespace !== 'CF_LOGS_DEFAULT') { | ||
data.namespace = namespace; | ||
} | ||
if (globalOptions.fields) { | ||
_.map(globalOptions.fields, (value, key) => { | ||
if (_.isFunction(value)) { | ||
data[key] = value(); | ||
} else { | ||
data[key] = value; | ||
} | ||
}); | ||
} | ||
oldArguments.push({ meta: data }); | ||
return oldArguments; | ||
} | ||
return false; | ||
}; | ||
const resetLevel = function () { | ||
logger.level = globalOptions.level; | ||
}; | ||
logger.exitOnError = true; | ||
const log = function (...args) { | ||
const mainArguments = addSpecificArguments(args); | ||
if (mainArguments) { | ||
logger.log(...mainArguments); | ||
} | ||
}; | ||
const error = function (...args) { | ||
const mainArguments = addSpecificArguments(args); | ||
if (mainArguments) { | ||
logger.error(...mainArguments); | ||
} | ||
}; | ||
const warn = function (...args) { | ||
const mainArguments = addSpecificArguments(args); | ||
if (mainArguments) { | ||
logger.warn(...mainArguments); | ||
} | ||
}; | ||
const info = function (...args) { | ||
const mainArguments = addSpecificArguments(args); | ||
if (mainArguments) { | ||
logger.info(...mainArguments); | ||
} | ||
}; | ||
const debug = function (...args) { | ||
const mainArguments = addSpecificArguments(args); | ||
if (mainArguments) { | ||
logger.debug(...mainArguments); | ||
} | ||
}; | ||
var addSpecificArguments = function(oldArguments){ | ||
if (isNamespaceEnabled(namespace) || namespace === "CF_LOGS_DEFAULT"){ | ||
var data = {}; | ||
if (globalOptions.showNamespace && namespace !== "CF_LOGS_DEFAULT"){ | ||
data.namespace = namespace; | ||
} | ||
if(globalOptions.fields){ | ||
_.map(globalOptions.fields, (value, key) => { | ||
if(_.isFunction(value)){ | ||
data[key] = value(); | ||
} else { | ||
data[key] = value; | ||
} | ||
}); | ||
} | ||
oldArguments.push({meta: data}); | ||
return oldArguments; | ||
} | ||
return false; | ||
}; | ||
return { | ||
log, | ||
error, | ||
warn, | ||
info, | ||
debug, | ||
Logger, | ||
newLoggerFromFilename: Logger, | ||
setGlobalOptions, | ||
init: setGlobalOptions, | ||
isNamespaceEnabled, | ||
resetLevel, | ||
}; | ||
}; | ||
var resetLevel = function(){ | ||
logger.level = globalOptions.level; | ||
}; | ||
const standardFormatter = winston.format.printf((info) => { | ||
const { meta, level, message } = info; | ||
const { authenticatedEntity, time } = (meta || {}); | ||
const userName = authenticatedEntity && authenticatedEntity.name; | ||
const accountName = authenticatedEntity && authenticatedEntity.activeAccount && authenticatedEntity.activeAccount.name; | ||
const namespace = meta && meta.namespace; | ||
info.message = `${time} ${level} ` + | ||
`${namespace ? `[${meta.namespace}]` : ''}` + | ||
`${accountName ? `[${accountName}]` : ''}` + | ||
`${userName ? `[${userName}]` : ''}` + | ||
` ${message}`; | ||
return info.message; | ||
}); | ||
var log = function() { | ||
var mainArguments = addSpecificArguments(Array.prototype.slice.call(arguments)); | ||
if (mainArguments){ | ||
logger.log.apply(logger, mainArguments); | ||
} | ||
}; | ||
var error = function(){ | ||
var mainArguments = addSpecificArguments(Array.prototype.slice.call(arguments)); | ||
if (mainArguments){ | ||
logger.error.apply(logger, mainArguments); | ||
} | ||
}; | ||
var warn = function(){ | ||
var mainArguments = addSpecificArguments(Array.prototype.slice.call(arguments)); | ||
if (mainArguments){ | ||
logger.warn.apply(logger, mainArguments); | ||
} | ||
}; | ||
var info = function(){ | ||
var mainArguments = addSpecificArguments(Array.prototype.slice.call(arguments)); | ||
if (mainArguments){ | ||
logger.info.apply(logger, mainArguments); | ||
} | ||
}; | ||
var debug = function(){ | ||
var mainArguments = addSpecificArguments(Array.prototype.slice.call(arguments)); | ||
if (mainArguments){ | ||
logger.debug.apply(logger, mainArguments); | ||
} | ||
}; | ||
return { | ||
log: log, | ||
error: error, | ||
warn: warn, | ||
info: info, | ||
debug: debug, | ||
Logger: Logger, | ||
newLoggerFromFilename: Logger, | ||
setGlobalOptions: setGlobalOptions, | ||
init: setGlobalOptions, | ||
isNamespaceEnabled: isNamespaceEnabled, | ||
resetLevel: resetLevel | ||
}; | ||
}; | ||
var defaultLogger = Logger("CF_LOGS_DEFAULT"); | ||
const defaultLogger = Logger('CF_LOGS_DEFAULT'); | ||
module.exports = defaultLogger; |
{ | ||
"name": "cf-logs", | ||
"version": "1.1.21", | ||
"version": "1.1.22", | ||
"description": "codefresh logs", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -39,4 +39,2 @@ [![Coverage Status](https://coveralls.io/repos/github/codefresh-io/cf-logs/badge.svg?branch=develop)](https://coveralls.io/github/codefresh-io/cf-logs?branch=develop) | ||
var options = { | ||
filePath: String, | ||
console: Boolean, | ||
showNamespace: Boolean, | ||
@@ -62,8 +60,2 @@ env_module: String, | ||
``` | ||
### Logging to file | ||
Set 'filePath' field to an absolute path to a file save the logs to a file. | ||
The default is set to null which means no logging to file will happen. | ||
### Logging to console | ||
Set 'console' filed to true or false to log to the console. | ||
The default is set to true | ||
### Logging levels | ||
@@ -93,2 +85,2 @@ The logging level is an additional filter that can be set, mainly for production usage. | ||
namespace: projectName | ||
newLogger(//home/user/workspace/projectDir/internalDir/file.js) will get the namespace: projectName:internalDir:file | ||
newLogger(//home/user/workspace/projectDir/internalDir/file.js) will get the namespace: projectName:internalDir:file |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
161057
626
84
13