@voliware/logger
Advanced tools
Comparing version 2.0.8 to 2.0.9
{ | ||
"name": "@voliware/logger", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "A tiny Javascript logger with levels and several options. Supports Node, MongoDB, and all modern Browsers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,3 @@ const EOL = require('os').EOL; | ||
* @param {Boolean} [options.console=true] - Whether to also log to the console | ||
* @param {Boolean} [params.objects_to_string=false] - Whether to log plain objects as a string | ||
*/ | ||
@@ -37,9 +38,10 @@ constructor(name, { | ||
multiplefiles = false, | ||
console = true | ||
console = true, | ||
objects_to_string = false | ||
}={}) | ||
{ | ||
super(name, {level, enabled, context, maxlogs, timestamp, filepath, maxsize}); | ||
super(name, {level, enabled, context, maxlogs, timestamp, objects_to_string}); | ||
this.maxsize = maxsize; | ||
this.multiplefiles = multiplefiles; | ||
this.console_logger = new Logger(name, {level, enabled: console, context, maxlogs, timestamp}); | ||
this.console_logger = new Logger(name, {level, enabled: console, context, maxlogs, timestamp, objects_to_string}); | ||
@@ -46,0 +48,0 @@ // todo |
@@ -38,2 +38,3 @@ if(typeof module !== "undefined"){ | ||
* @param {Number} [params.maxlogs=0] - Maximum number of logs until the first log is deleted | ||
* @param {Boolean} [params.objects_to_string=false] - Whether to log plain objects as a string | ||
*/ | ||
@@ -45,3 +46,4 @@ constructor(name, { | ||
timestamp = LoggerMessage.timestamp.locale, | ||
maxlogs = 0 | ||
maxlogs = 0, | ||
objects_to_string = false | ||
}={}) | ||
@@ -55,2 +57,3 @@ { | ||
this.maxlogs = maxlogs; | ||
this.objects_to_string = objects_to_string; | ||
this.log_count = 0; | ||
@@ -120,3 +123,3 @@ } | ||
* log level is greater than the specified one. | ||
* @param {String} message - message to log | ||
* @param {LoggerMessage|Object|String} message - message to log | ||
* @param {Number|String} [level=this.level] - log level; current level by default | ||
@@ -143,2 +146,7 @@ * @return {Promise} | ||
} | ||
else if (typeof message === "object" && !(message instanceof LoggerMessage)){ | ||
if(this.objects_to_string){ | ||
message = this.createMessage(JSON.stringify(message), level); | ||
} | ||
} | ||
@@ -148,3 +156,13 @@ await this.checkLogCount(); | ||
this.log_count++; | ||
return this.logToLevel(message, level); | ||
} | ||
/** | ||
* Log to the appropriate output level | ||
* @param {LoggerMessage|Object|String} message - message to log | ||
* @param {Number|String} [level=this.level] - log level; current level by default | ||
* @returns {Promise} | ||
*/ | ||
async logToLevel(message, level){ | ||
switch(level){ | ||
@@ -151,0 +169,0 @@ case LoggerMessage.level.verbose: |
@@ -0,1 +1,2 @@ | ||
const {ObjectId} = require('mongodb'); | ||
const Logger = require('./logger'); | ||
@@ -22,2 +23,3 @@ const LoggerMessage = require('./LoggerMessage'); | ||
* @param {Boolean} [options.console=true] - Whether to also log to the console | ||
* @param {Boolean} [params.objects_to_string=false] - Whether to log plain objects as a string | ||
*/ | ||
@@ -31,9 +33,10 @@ constructor(name, { | ||
collection = null, | ||
console = true | ||
console = true, | ||
objects_to_string = false | ||
}={}) | ||
{ | ||
super(name, {level, enabled, context, maxlogs, timestamp}); | ||
super(name, {level, enabled, context, maxlogs, timestamp, objects_to_string}); | ||
this.collection = collection; | ||
this.log_count = 0; | ||
this.console_logger = new Logger(name, {level, enabled: console, context, maxlogs, timestamp}); | ||
this.console_logger = new Logger(name, {level, enabled: console, context, maxlogs, timestamp, objects_to_string}); | ||
} | ||
@@ -69,12 +72,27 @@ | ||
// If an _id was passed, we should clone | ||
if(message._id){ | ||
message._id_ = message._id; | ||
delete message._id; | ||
if(typeof message.text === 'object'){ | ||
message.text = this.cloneAndFilterObject(message.text); | ||
} | ||
this.collection.insertOne(message); | ||
} | ||
/** | ||
* Process a message just before logging | ||
* If the incoming message is a plain old object, the intention | ||
* is to log this object directly to the MongoDB | ||
* @param {LoggerMessage|Object|String} message - message to log | ||
* @param {Number} level | ||
*/ | ||
processMessage(message, level){ | ||
if(typeof message === 'object' && !(message instanceof LoggerMessage)){ | ||
if(!this.objects_to_string){ | ||
message = this.createMessage(message, level); | ||
} | ||
} | ||
return message; | ||
} | ||
/** | ||
* The output function to log a verbose message. | ||
@@ -86,2 +104,3 @@ * @param {LoggerMessage|Object} message | ||
this.console_logger.enabled && this.console_logger._verbose(message); | ||
message = this.processMessage(message, LoggerMessage.level.verbose); | ||
return this.insertMessage(message); | ||
@@ -97,2 +116,3 @@ } | ||
this.console_logger.enabled && this.console_logger._debug(message); | ||
message = this.processMessage(message, LoggerMessage.level.debug); | ||
return this.insertMessage(message); | ||
@@ -108,2 +128,3 @@ } | ||
this.console_logger.enabled && this.console_logger._info(message); | ||
message = this.processMessage(message, LoggerMessage.level.info); | ||
return this.insertMessage(message); | ||
@@ -119,2 +140,3 @@ } | ||
this.console_logger.enabled && this.console_logger._warning(message); | ||
message = this.processMessage(message, LoggerMessage.level.warning); | ||
return this.insertMessage(message); | ||
@@ -130,2 +152,3 @@ } | ||
this.console_logger.enabled && this.console_logger._error(message); | ||
message = this.processMessage(message, LoggerMessage.level.error); | ||
return this.insertMessage(message); | ||
@@ -164,4 +187,29 @@ } | ||
} | ||
/** | ||
* Clone object helper | ||
* @param {Object} object | ||
* @returns {Object} | ||
*/ | ||
cloneAndFilterObject(object) { | ||
const clone = {}; | ||
for(const i in object) { | ||
if(object[i] instanceof ObjectId){ | ||
clone[i] = object[i].toString() | ||
} | ||
else { | ||
const clean_key = i.replace(/[$.]/g, ''); | ||
if(object[i] !== null && typeof(object[i]) === 'object'){ | ||
clone[clean_key] = this.cloneAndFilterObject(object[i]); | ||
} | ||
else{ | ||
clone[clean_key] = object[i]; | ||
} | ||
} | ||
} | ||
return clone; | ||
} | ||
} | ||
module.exports = MongoDbLogger; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45558
1050