@voliware/logger
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,1 +0,1 @@ | ||
class Logger{constructor(e,t={}){return this.name=e,this.options={level:Logger.level.info,timestamp:{state:!1,format:Logger.timestamp.locale},context:null},Object.assign(this.options,t),this.setTimestampFormat(this.options.timestamp.format),this}setContext(e){return this.options.context=e,this}setLogLevel(e){return this.options.level=e,this}setTimestamp(e){return this.options.timestamp=e,this.setTimestampFormat(this.options.timestamp.format),this}setTimestampFormat(e){switch(e){case Logger.timestamp.utc:this.appendTimestamp=this.appendUtcTimestamp;break;case Logger.timestamp.localedate:this.appendTimestamp=this.appendLocaleDateTimestamp;break;case Logger.timestamp.localetime:this.appendTimestamp=this.appendLocaleTimeTimestamp;break;case Logger.timestamp.locale:default:this.appendTimestamp=this.appendLocaleTimestamp}return this}setCustomTimestampFormat(e){return this.appendTimestamp=e,this}appendUtcTimestamp(){return`[${(new Date).toUTCString()}] `}appendLocaleTimestamp(){return`[${(new Date).toLocaleString()}] `}appendLocaleTimeTimestamp(){return`[${(new Date).toLocaleTimeString()}] `}appendLocaleDateTimestamp(){return`[${(new Date).toLocaleDateString()}] `}createMessage(e,t=this.options.level){if(t<this.options.level)return;let s="";return this.options.timestamp.state&&(s+=this.appendTimestamp()),s+=`[${Logger.level.string[t]}] `,s+=`[${this.name}] `,this.options.context&&(s+=`[${this.options.context}] `),s+="string"==typeof e?e:"logging object"}log(e,t=this.options.level){let s=this.createMessage(e,t);"string"==typeof e?console.log(s):(console.log(s),console.log(e))}verbose(e){return this.log(e,Logger.level.verbose)}debug(e){return this.log(e,Logger.level.debug)}info(e){return this.log(e,Logger.level.info)}warning(e){return this.log(e,Logger.level.warning)}error(e){return this.log(e,Logger.level.error)}}Logger.level={verbose:0,debug:1,info:2,warning:3,error:4,string:["VRB","DBG","INF","WRN","ERR"]},Logger.timestamp={utc:0,locale:1,localetime:2,localedate:3},"undefined"!=typeof module&&(module.exports=Logger); | ||
class Logger{constructor(e,t={}){return this.name=e,this.options={level:Logger.level.info,timestamp:{state:!1,format:Logger.timestamp.locale},context:null},Object.assign(this.options,t),this.setTimestampFormat(this.options.timestamp.format),this}setContext(e){return this.options.context=e,this}setLogLevel(e){return this.options.level=e,this}setTimestamp(e){return this.options.timestamp=e,this.setTimestampFormat(this.options.timestamp.format),this}setTimestampFormat(e){switch(e){case Logger.timestamp.utc:this.appendTimestamp=this.appendUtcTimestamp;break;case Logger.timestamp.localedate:this.appendTimestamp=this.appendLocaleDateTimestamp;break;case Logger.timestamp.localetime:this.appendTimestamp=this.appendLocaleTimeTimestamp;break;case Logger.timestamp.locale:default:this.appendTimestamp=this.appendLocaleTimestamp}return this}setCustomTimestampFormat(e){return this.appendTimestamp=e,this}appendUtcTimestamp(){return`[${(new Date).toUTCString()}] `}appendLocaleTimestamp(){return`[${(new Date).toLocaleString()}] `}appendLocaleTimeTimestamp(){return`[${(new Date).toLocaleTimeString()}] `}appendLocaleDateTimestamp(){return`[${(new Date).toLocaleDateString()}] `}createMessage(e,t){let s="";return this.options.timestamp.state&&(s+=this.appendTimestamp()),s+=`[${Logger.level.string[t]}] `,s+=`[${this.name}] `,this.options.context&&(s+=`[${this.options.context}] `),"string"==typeof e&&(s+=e),s}log(e,t=this.options.level){if(t<this.options.level)return!1;let s=this.createMessage(e,t);return"string"==typeof e?console.log(s):(console.log("logging object"),console.log(e)),!0}verbose(e){return this.log(e,Logger.level.verbose)}debug(e){return this.log(e,Logger.level.debug)}info(e){return this.log(e,Logger.level.info)}warning(e){return this.log(e,Logger.level.warning)}error(e){return this.log(e,Logger.level.error)}}Logger.level={verbose:0,debug:1,info:2,warning:3,error:4,string:["VRB","DBG","INF","WRN","ERR"]},Logger.timestamp={utc:0,locale:1,localetime:2,localedate:3},"undefined"!=typeof module&&(module.exports=Logger); |
{ | ||
"name": "@voliware/logger", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A tiny Javascript logger with levels and several options. Supports Node and all modern Browsers", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"test": "mocha", | ||
"build": "build.js" | ||
"build": "node build" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": { |
# logger | ||
Probably the simplest logger out there. | ||
## How to Use it | ||
```js | ||
let logger = new Logger("App", { | ||
level: Logger.level.debug, | ||
timestamp: { | ||
state: true, | ||
format: Logger.timestamp.utc | ||
} | ||
}); | ||
// prints to the console | ||
// [Mon, 01 Jul 2019 23:41:04 GMT] [DBG] [App] Initializing | ||
logger.debug("Initializing"); | ||
``` | ||
## Node | ||
Install with node package manager | ||
```js | ||
npm install @voliware/logger | ||
``` | ||
Include in a file | ||
```js | ||
const Logger = require('@voliware/logger'); | ||
``` | ||
## Browser | ||
Include in a file | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/@voliware/logger/dist/logger.min.js"></script> | ||
``` |
@@ -166,10 +166,6 @@ /** | ||
* @param {string} message - message to log | ||
* @param {number} [level=this.options.level] - log level; current level by default | ||
* @param {number} level - log level | ||
* @return {Logger} | ||
*/ | ||
createMessage(message, level = this.options.level){ | ||
if(level < this.options.level){ | ||
return; | ||
} | ||
createMessage(message, level){ | ||
let msg = ""; | ||
@@ -190,5 +186,2 @@ if(this.options.timestamp.state){ | ||
} | ||
else { | ||
msg += "logging object"; | ||
} | ||
@@ -202,5 +195,9 @@ return msg; | ||
* @param {number} [level=this.options.level] - log level; current level by default | ||
* @return {Logger} | ||
* @return {boolean} true if it logged, false otherwise | ||
*/ | ||
log(message, level = this.options.level){ | ||
if(level < this.options.level){ | ||
return false; | ||
} | ||
let msg = this.createMessage(message, level); | ||
@@ -212,5 +209,7 @@ | ||
else { | ||
console.log(msg); | ||
console.log("logging object"); | ||
console.log(message); | ||
} | ||
return true; | ||
} | ||
@@ -217,0 +216,0 @@ |
@@ -33,5 +33,5 @@ const Logger = require('../index'); | ||
it('logs a message at the verbose level', () => { | ||
it('creates a message at the verbose level', () => { | ||
let level = Logger.level.verbose; | ||
let logger = new Logger("App", {level}); | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test", level); | ||
@@ -41,5 +41,5 @@ assert.strictEqual(msg, "[VRB] [App] Test"); | ||
it('logs a message at the debug level', () => { | ||
it('creates a message at the debug level', () => { | ||
let level = Logger.level.debug; | ||
let logger = new Logger("App", {level}); | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test", level); | ||
@@ -49,5 +49,5 @@ assert.strictEqual(msg, "[DBG] [App] Test"); | ||
it('logs a message at the info level', () => { | ||
it('creates a message at the info level', () => { | ||
let level = Logger.level.info; | ||
let logger = new Logger("App", {level}); | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test", level); | ||
@@ -57,5 +57,5 @@ assert.strictEqual(msg, "[INF] [App] Test"); | ||
it('logs a message at the warning level', () => { | ||
it('creates a message at the warning level', () => { | ||
let level = Logger.level.warning; | ||
let logger = new Logger("App", {level}); | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test", level); | ||
@@ -65,5 +65,5 @@ assert.strictEqual(msg, "[WRN] [App] Test"); | ||
it('logs a message at the error level', () => { | ||
it('creates a message at the error level', () => { | ||
let level = Logger.level.error; | ||
let logger = new Logger("App", {level}); | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test", level); | ||
@@ -73,11 +73,18 @@ assert.strictEqual(msg, "[ERR] [App] Test"); | ||
it('logs a message with no context', () => { | ||
it('should not log debug when the log level is at info', () => { | ||
let level = Logger.level.info; | ||
let logger = new Logger("App", {level}); | ||
let result = logger.debug("Test"); | ||
assert.strictEqual(result, false); | ||
}); | ||
it('creates a message with no context', () => { | ||
let logger = new Logger("App"); | ||
let msg = logger.createMessage("Test"); | ||
let msg = logger.createMessage("Test", Logger.level.info); | ||
assert.strictEqual(msg, "[INF] [App] Test"); | ||
}); | ||
it('logs a message with context', () => { | ||
it('creates a message with context', () => { | ||
let logger = new Logger("App", {context:"User"}); | ||
let msg = logger.createMessage("Test"); | ||
let msg = logger.createMessage("Test", Logger.level.info); | ||
assert.strictEqual(msg, "[INF] [App] [User] Test"); | ||
@@ -84,0 +91,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
16685
365
34