Comparing version 0.2.2 to 0.2.3
@@ -27,2 +27,11 @@ 'use strict'; | ||
function isDefined(v) { | ||
// filter `null` and `void 0` | ||
return v !== null; | ||
} | ||
function isDev() { | ||
return process.env['NODE_ENV'] !== 'production'; | ||
} | ||
/** | ||
@@ -32,12 +41,14 @@ * Main method for create logger. The logger is just a function to recieve | ||
* | ||
* @param {Writable} writable A writable stream for logging | ||
* @param {string} prefix The prefix to write | ||
* @param {string | bool} date The date format | ||
* @param {Writable} writable A writable stream for logging | ||
* @param {string} prefix The prefix to write | ||
* @param {string | bool} date The date format | ||
* @return {function} A function use like `console.log` | ||
*/ | ||
function createLogger(writable, prefix, dateFormat) { | ||
if (!(writable && typeof writable.write === 'function')) { | ||
throw Error("The logger expected a writable stream."); | ||
} | ||
function createLogger(writables, prefix, dateFormat) { | ||
if (!Array.isArray(writables)) writables = [writables]; | ||
writables = writables.filter(function (w) { | ||
return isDefined(w) && isFunction(w.write); | ||
}); | ||
var pre = '[' + prefix + ']'; | ||
@@ -51,3 +62,5 @@ // default timestamp format | ||
return function (msg) { | ||
writable.write(pre + ' ' + msg + '\n'); | ||
writables.forEach(function (w) { | ||
return w.write(pre + ' ' + msg + '\n'); | ||
}); | ||
}; | ||
@@ -78,2 +91,9 @@ } | ||
// The value of `logToConsole` default to true in dev mode | ||
var logToConsole = opts.logToConsole; | ||
if (isDev() && !isDefined(logToConsole)) { | ||
logToConsole = true; | ||
} | ||
var flag = append ? 'a' : 'w'; | ||
@@ -91,3 +111,5 @@ var infoStream = infoFile && _fs2.default.createWriteStream(infoFile, { flags: flag }); | ||
}); | ||
stream && createLogger(stream, 'Verbose', timestamp).apply(this, arguments); | ||
if (logToConsole) stream = [process.stdout, stream]; | ||
createLogger(stream, 'Verbose', timestamp).apply(this, arguments); | ||
}, | ||
@@ -98,3 +120,5 @@ info: function info() { | ||
}); | ||
stream && createLogger(stream, 'Info', timestamp).apply(this, arguments); | ||
if (logToConsole) stream = [process.stdout, stream]; | ||
createLogger(stream, 'Info', timestamp).apply(this, arguments); | ||
}, | ||
@@ -105,3 +129,5 @@ warning: function warning() { | ||
}); | ||
stream && createLogger(stream, 'Warning', timestamp).apply(this, arguments); | ||
if (logToConsole) stream = [process.stderr, stream]; | ||
createLogger(stream, 'Warning', timestamp).apply(this, arguments); | ||
}, | ||
@@ -112,3 +138,5 @@ error: function error() { | ||
}); | ||
stream && createLogger(stream, 'Error', timestamp).apply(this, arguments); | ||
if (logToConsole) stream = [process.stderr, stream]; | ||
createLogger(stream, 'Error', timestamp).apply(this, arguments); | ||
} | ||
@@ -115,0 +143,0 @@ }; |
{ | ||
"name": "elecpen", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "a simple logger", | ||
@@ -31,7 +31,9 @@ "main": "lib/index.js", | ||
"babel-cli": "^6.4.0", | ||
"babel-eslint": "^5.0.0-beta6", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"babel-register": "^6.3.13", | ||
"eslint": "^1.10.3", | ||
"mocha": "^2.2.5" | ||
} | ||
} |
@@ -14,3 +14,3 @@ # ElecPen | ||
|-----|-----------| | ||
|writable|A writable stream for logging| | ||
|writables|One or an array of writable stream for logging| | ||
|prefix|The prefix of the record| | ||
@@ -31,2 +31,3 @@ |dateFormat|Date format for logger or pass `true` to use the default format| | ||
|append|boolean|If file exists, append new entries to it instead of truncating| | ||
|logToConsole|boolean|Output the message to console, it will be default to true in dev mode| | ||
@@ -33,0 +34,0 @@ ### Methods |
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
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11575
9
171
98
0
8
3