@voliware/logger
Advanced tools
Comparing version 2.0.11 to 2.0.12
{ | ||
"name": "@voliware/logger", | ||
"version": "2.0.11", | ||
"version": "2.0.12", | ||
"description": "A tiny Javascript logger with levels and several options. Supports Node, MongoDB, and all modern Browsers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,3 +31,3 @@ const EOL = require('os').EOL; | ||
enabled = true, | ||
context = null, | ||
context = undefined, | ||
timestamp = LoggerMessage.timestamp.locale, | ||
@@ -34,0 +34,0 @@ maxlogs = 0, |
@@ -43,3 +43,3 @@ if(typeof module !== "undefined"){ | ||
enabled = true, | ||
context = null, | ||
context = undefined, | ||
timestamp = LoggerMessage.timestamp.locale, | ||
@@ -46,0 +46,0 @@ maxlogs = 0, |
@@ -8,22 +8,22 @@ /** | ||
* Constructor | ||
* @param {Object} options | ||
* @param {String|Null} options.context | ||
* @param {Number|Null} options.level | ||
* @param {String|Null} options.name | ||
* @param {String} options.text | ||
* @param {Number|Boolean} options.timestamp | ||
* @param {Object} [options={}] | ||
* @param {String|Null} [options.context=undefined] | ||
* @param {Number|Null} [options.level=undefined] | ||
* @param {String|Null} [options.name=undefined] | ||
* @param {String} [options.text=undefined] | ||
* @param {Number|Boolean} [options.timestamp=undefined] | ||
*/ | ||
constructor({ | ||
context = null, | ||
level = null, | ||
name = null, | ||
text = '', | ||
timestamp = null | ||
}){ | ||
context = undefined, | ||
level = undefined, | ||
name = undefined, | ||
text = undefined, | ||
timestamp = undefined | ||
}={}){ | ||
this.context = context ? `[${context}] ` : ''; | ||
this.level = typeof level !== null ? `[${LoggerMessage.level.string[level]}] ` : ''; | ||
this.name = name ? `[${name}] ` : ''; | ||
this.context = context; | ||
this.level = typeof level !== 'undefined' ? LoggerMessage.level.string[level] : level; | ||
this.name = name; | ||
this.text = text; | ||
this.timestamp = typeof timestamp !== null ? this.generateTimestamp(timestamp) : ''; | ||
this.timestamp = typeof timestamp !== 'undefined' ? this.generateTimestamp(timestamp) : timestamp; | ||
} | ||
@@ -38,13 +38,14 @@ | ||
case LoggerMessage.timestamp.utc: | ||
return this.generateUtcTimestamp(); | ||
return new Date().toUTCString(); | ||
case LoggerMessage.timestamp.localedate: | ||
return this.generateLocaleDateTimestamp(); | ||
return new Date().toLocaleDateString(); | ||
case LoggerMessage.timestamp.localetime: | ||
return this.generateLocaleTimeTimestamp(); | ||
return new Date().toLocaleTimeString(); | ||
case LoggerMessage.timestamp.numeric: | ||
return this.generateNumericTimestamp(); | ||
generateNumericTimestamp(); | ||
case LoggerMessage.timestamp.locale: | ||
return this.generateLocaleTimestamp(); | ||
return new Date().toLocaleString(); | ||
case LoggerMessage.timestamp.none: | ||
default: | ||
return ''; | ||
return undefined; | ||
} | ||
@@ -54,47 +55,2 @@ } | ||
/** | ||
* Generate a UTC timestamp. | ||
* eg "Mon, 01 Jul 2019 14:43:35 GMT" | ||
* @return {String} | ||
*/ | ||
generateUtcTimestamp(){ | ||
return `[${(new Date().toUTCString())}] `; | ||
} | ||
/** | ||
* Generate a locale timestamp. | ||
* eg "7/1/2019, 10:43:26 AM" | ||
* @return {String} | ||
*/ | ||
generateLocaleTimestamp(){ | ||
return `[${(new Date().toLocaleString())}] `; | ||
} | ||
/** | ||
* Generate a locale time timestamp. | ||
* eg "10:43:06 AM" | ||
* @return {String} | ||
*/ | ||
generateLocaleTimeTimestamp(){ | ||
return `[${(new Date().toLocaleTimeString())}] `; | ||
} | ||
/** | ||
* Generate a locale date timestamp. | ||
* eg "7/1/2019" | ||
* @return {String} | ||
*/ | ||
generateLocaleDateTimestamp(){ | ||
return `[${(new Date().toLocaleDateString())}] `; | ||
} | ||
/** | ||
* Generate a numerical timestamp. | ||
* eg 1628828513146 | ||
* @return {String} | ||
*/ | ||
generateNumericTimestamp(){ | ||
return `[${Date.now()}] `; | ||
} | ||
/** | ||
* Convert the message to a string | ||
@@ -104,3 +60,9 @@ * @returns {String} | ||
toString(){ | ||
return `${this.timestamp}${this.level}${this.name}${this.context}${this.text}`; | ||
const timestamp = this.timestamp ? `[${this.timestamp}] ` : ''; | ||
const level = this.level ? `[${this.level}] ` : ''; | ||
const name = this.name ? `[${this.name}] ` : ''; | ||
const context = this.context ? `[${this.context}] ` : ''; | ||
const text = this.text ? this.text : ''; | ||
return `${timestamp}${level}${name}${context}${text}`; | ||
} | ||
@@ -144,7 +106,8 @@ } | ||
LoggerMessage.timestamp = { | ||
utc: 0, | ||
locale: 1, | ||
localetime: 2, | ||
localedate: 3, | ||
numeric: 4 | ||
none: 0, | ||
utc: 1, | ||
locale: 2, | ||
localetime: 3, | ||
localedate: 4, | ||
numeric: 5 | ||
}; | ||
@@ -157,2 +120,3 @@ | ||
LoggerMessage.timestamp.stringmap = new Map() | ||
.set("none", LoggerMessage.timestamp.none) | ||
.set("utc", LoggerMessage.timestamp.utc) | ||
@@ -159,0 +123,0 @@ .set("locale", LoggerMessage.timestamp.locale) |
@@ -28,3 +28,3 @@ const {ObjectId} = require('mongodb'); | ||
enabled = true, | ||
context = null, | ||
context = undefined, | ||
timestamp = LoggerMessage.timestamp.locale, | ||
@@ -78,3 +78,2 @@ maxlogs = 0, | ||
/** | ||
@@ -81,0 +80,0 @@ * Process a message just before logging |
@@ -142,7 +142,7 @@ const Assert = require('assert'); | ||
const data = await this.collection.findOne(); | ||
Assert.strictEqual(data.context, ''); | ||
Assert.strictEqual(data.level, '[INF] '); | ||
Assert.strictEqual(data.name, '[App] '); | ||
Assert.strictEqual(data.context, null); | ||
Assert.strictEqual(data.level, 'INF'); | ||
Assert.strictEqual(data.name, 'App'); | ||
Assert.strictEqual(data.text, 'Test 1'); | ||
Assert.strictEqual(data.timestamp, ''); | ||
Assert.strictEqual(data.timestamp, null); | ||
}); | ||
@@ -154,7 +154,7 @@ | ||
const data = await this.collection.findOne(); | ||
Assert.strictEqual(data.context, ''); | ||
Assert.strictEqual(data.level, '[INF] '); | ||
Assert.strictEqual(data.name, '[App] '); | ||
Assert.strictEqual(data.context, null); | ||
Assert.strictEqual(data.level, 'INF'); | ||
Assert.strictEqual(data.name, 'App'); | ||
Assert.strictEqual(data.text, 'Test 2'); | ||
Assert.strictEqual(data.timestamp, ''); | ||
Assert.strictEqual(data.timestamp, null); | ||
}); | ||
@@ -161,0 +161,0 @@ |
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
44220
995