-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 834e626
Showing
8 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# formal-console | ||
|
||
Extends console object to present a more useful log output by adding the log type, timestamp, and fancy colors. | ||
|
||
Since this extends the console object, you only need to import & configure it once (probably in your main script) and you're good to go for the whole project. | ||
|
||
## Usage | ||
|
||
```js | ||
var consoleConfig = require('formal-console'); | ||
|
||
console.log("test", {test: "test"}, new Date()); | ||
console.log("test"); | ||
console.error("some error"); | ||
console.warn("some warning"); | ||
console.info("some info"); | ||
console.debug("some debug"); | ||
console.success("success"); | ||
|
||
console.dir({ | ||
test: "test", | ||
arr: [ | ||
"one", | ||
"two" | ||
], | ||
obj: { | ||
test: "test", | ||
arr: [ | ||
{ | ||
test: "test" | ||
} | ||
] | ||
} | ||
}); | ||
console.time("test"); | ||
console.timeEnd("test"); | ||
console.trace("some error"); | ||
``` | ||
|
||
## Custom output types | ||
|
||
```js | ||
var consoleConfig = require('formal-console'); | ||
|
||
// simple | ||
consoleConfig.makeSimpleLogger("fancy"); | ||
console.fancy("fancy message"); | ||
|
||
// custom styling | ||
var term = require('terminal-kit').terminal; | ||
consoleConfig.makeSimpleLogger("fancy"); | ||
// The first style object is for the prefix, and is a terminal-kit object | ||
// Optional: the second style object is for the message, and is | ||
// consoleConfig.nativeLog or consoleConfig.nativeError | ||
consoleConfig.options.styles.fancy[0] = term.bgMagenta.underline.yellow; | ||
console.fancy("even more fancy message"); | ||
|
||
// completely custom logic | ||
consoleConfig.makeCustomLogger("fatal", function() { | ||
// reuse the standard prefix | ||
consoleConfig.printPrefix("fatal", term.error.bgRed.underline.white); | ||
// log normally | ||
consoleConfig.nativeError.apply(this, arguments); | ||
// custom logic: trigger a crash | ||
process.abort(); | ||
}); | ||
console.fatal("test"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
var term = require('terminal-kit').terminal; | ||
var moment = require('moment'); | ||
var stringz = require('stringz'); // for emoji support ❤️ | ||
|
||
// keep the native pipe to stdout & stderr | ||
module.exports.nativeLog = global.console.log; | ||
module.exports.nativeError = global.console.error; | ||
|
||
// enables or disables certain types of logging | ||
var loggerTypes = {} | ||
module.exports.enable = type => { | ||
module.exports.options.styles[type] = module.exports.options.styles[type] || [term, module.exports.nativeLog]; | ||
loggerTypes[type] = true; | ||
}; | ||
module.exports.disable = type => loggerTypes[type] = false; | ||
module.exports.isEnabled = type => loggerTypes[type]; | ||
|
||
module.exports.options = { | ||
typePadding: ' ', // dictates the width of the type prefix | ||
styles: { // contains term styles for the various prefixes | ||
error: [term.error.bgRed.white, module.exports.nativeError], | ||
warn: [term.error.bgYellow.white, module.exports.nativeError], | ||
info: [term, module.exports.nativeLog], | ||
debug: [term, module.exports.nativeLog], | ||
success: [term.bgGreen.white, module.exports.nativeLog] | ||
}, | ||
// a function that takes a date and returns a string | ||
// used to print the date in the prefix | ||
dateFormatter: date => moment(date).format("D/M/YY HH:MM:ss.SSS") | ||
} | ||
|
||
var getLogTypePrefix = type => ` [${type}] ${module.exports.options.typePadding.substring(stringz.length(type) + 4)}`; | ||
var getPrefix = type => getLogTypePrefix(type) + module.exports.options.dateFormatter(new Date()) + " "; | ||
module.exports.printPrefix = (type, t = term) => {t(getPrefix(type));t.styleReset("| ")}; | ||
|
||
module.exports.makeSimpleLogger = type => { | ||
module.exports.enable(type); | ||
var TYPE = type == "success" ? "OK" : type.toUpperCase(); | ||
global.console[type] = function() { | ||
if (loggerTypes[type]) { | ||
module.exports.printPrefix(TYPE, module.exports.options.styles[type][0]); | ||
module.exports.options.styles[type][1].apply(this, arguments); | ||
} | ||
} | ||
} | ||
|
||
module.exports.makeCustomLogger = (type, myfunction) => { | ||
module.exports.enable(type); | ||
global.console[type] = function() { | ||
if (loggerTypes["error"]) { | ||
myfunction.apply(this, arguments); | ||
} | ||
}; | ||
} | ||
|
||
module.exports.makeSimpleLogger("debug"); | ||
module.exports.makeSimpleLogger("info"); | ||
global.console.log = global.console.info; | ||
module.exports.makeSimpleLogger("warn"); | ||
module.exports.makeSimpleLogger("success"); | ||
|
||
module.exports.makeCustomLogger("error", function() { | ||
var isTrace = arguments[0] && arguments[0].substring(0, 5) == "Trace"; | ||
var type = isTrace ? "TRACE" : "ERROR"; | ||
module.exports.printPrefix(type, module.exports.options.styles.error[0]); | ||
if (isTrace) { | ||
arguments[0] = arguments[0].substring(7); | ||
} | ||
module.exports.options.styles.error[1].apply(this, arguments); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "formal-console", | ||
"version": "1.0.0", | ||
"description": "Overrides console functions in Node.js to have a more formal structure", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "test/test.sh" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Juravenator/formal-console.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"nodejs", | ||
"console", | ||
"logging" | ||
], | ||
"author": "Glenn Dirkx", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Juravenator/formal-console/issues" | ||
}, | ||
"homepage": "https://github.com/Juravenator/formal-console#readme", | ||
"dependencies": { | ||
"moment": "^2.17.0", | ||
"stringz": "^0.1.1", | ||
"terminal-kit": "^0.25.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[43m[37m [WARN] 17/12/95 03:12:04.000 [39m[49m[43m[37m[0m| [39m[49msomething is fishy | ||
[41m[37m [ERROR] 17/12/95 03:12:04.000 [39m[49m[41m[37m[0m| [39m[49msomething went wrong | ||
[41m[37m [TRACE] 17/12/95 03:12:04.000 [39m[49m[41m[37m[0m| [39m[49mwe detected some serious error and would like a stack trace | ||
at Object.<anonymous> (/Users/glenn/formal-console/test/test.js:43:9) | ||
at Module._compile (module.js:573:32) | ||
at Object.Module._extensions..js (module.js:582:10) | ||
at Module.load (module.js:490:32) | ||
at tryModuleLoad (module.js:449:12) | ||
at Function.Module._load (module.js:441:3) | ||
at Module.runMain (module.js:607:10) | ||
at run (bootstrap_node.js:420:7) | ||
at startup (bootstrap_node.js:139:9) | ||
at bootstrap_node.js:535:3 | ||
[41m[4m[37m [FATAL] 17/12/95 03:12:04.000 [39m[24m[49m[41m[4m[37m[0m| [39m[24m[49mwe went too fancy ☹️ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[INFO] 17/12/95 03:12:04.000 [0m| Hello, World! | ||
[INFO] 17/12/95 03:12:04.000 [0m| test with object { test: 'test' } 1995-12-17T02:24:04.000Z | ||
[INFO] 17/12/95 03:12:04.000 [0m| test with substitution: ok? | ||
[INFO] 17/12/95 03:12:04.000 [0m| info message, log = info | ||
[DEBUG] 17/12/95 03:12:04.000 [0m| a debugging message | ||
[DEBUG] 17/12/95 03:12:04.000 [0m| second debug message | ||
[42m[37m [OK] 17/12/95 03:12:04.000 [39m[49m[42m[37m[0m| [39m[49mwe did it! (so far) | ||
{ test: 'test', | ||
arr: [ 'one', 'two' ], | ||
obj: { test: 'test', arr: [ [Object] ] } } | ||
[FANCY] 17/12/95 03:12:04.000 [0m| fancy message | ||
[❤️ ] 17/12/95 03:12:04.000 [0m| even more fancy message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env node | ||
var consoleConfig = require('../index.js'); | ||
var oldFormatter = consoleConfig.options.dateFormatter; | ||
var fixedDate = new Date(1995, 11, 17, 3, 24, 4); | ||
consoleConfig.options.dateFormatter = date => oldFormatter(fixedDate); | ||
|
||
console.log("Hello, World!"); | ||
console.log("test with object", {test: "test"}, fixedDate); | ||
console.log("%s: %s", "test with substitution", "ok?"); | ||
|
||
console.info("info message, log = info"); | ||
|
||
console.warn("something is fishy"); | ||
|
||
console.error("something went wrong"); | ||
|
||
console.debug("a debugging message"); | ||
consoleConfig.disable("debug"); | ||
console.debug("this should not show up"); | ||
consoleConfig.enable("debug"); | ||
console.debug("second debug message"); | ||
|
||
console.success("we did it! (so far)"); | ||
|
||
console.dir({ | ||
test: "test", | ||
arr: [ | ||
"one", | ||
"two" | ||
], | ||
obj: { | ||
test: "test", | ||
arr: [ | ||
{ | ||
test: "test" | ||
} | ||
] | ||
} | ||
}); | ||
// timers fluctuate with each run, cannot be used for tests ☹️ | ||
// console.time("test"); | ||
// console.timeEnd("test"); | ||
console.trace("we detected some serious error and would like a stack trace"); | ||
|
||
consoleConfig.makeSimpleLogger("fancy"); | ||
console.fancy("fancy message"); | ||
|
||
var term = require('terminal-kit').terminal; | ||
consoleConfig.makeSimpleLogger("❤️ "); | ||
// The first style object is for the prefix, and is a terminal-kit object | ||
// Optional: the second style object is for the message, and is | ||
// consoleConfig.nativeLog or consoleConfig.nativeError | ||
consoleConfig.options.styles.fancy[0] = term.bgMagenta.underline.yellow; | ||
console["❤️ "]("even more fancy message"); | ||
|
||
consoleConfig.makeCustomLogger("fatal", function() { | ||
consoleConfig.printPrefix("FATAL", term.error.bgRed.underline.white); | ||
consoleConfig.nativeError.apply(this, arguments); | ||
}); | ||
console.fatal("we went too fancy ☹️"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/sh | ||
template_err_file=$(find . -name "expected_test_err") | ||
template_out_file=$(find . -name "expected_test_out") | ||
test_script=$(find . -name "formal-console-test.js") | ||
|
||
rm -f test_out test_err template_out_filtered test_out_filtered template_err_filtered test_err_filtered | ||
|
||
$test_script >> test_out 2>> test_err | ||
|
||
grep -v " at " $template_out_file > template_out_filtered | ||
grep -v " at " test_out > test_out_filtered | ||
cmp template_out_filtered test_out | ||
if [ $? -ne 0 ]; then | ||
echo "💩 test 1 failed" | ||
echo "expected:" | ||
cat template_out_filtered | ||
echo "actually:" | ||
cat test_out_filtered | ||
else | ||
echo "✅ test 1 passed" | ||
fi | ||
|
||
grep -v " at " $template_err_file > template_err_filtered | ||
grep -v " at " test_err > test_err_filtered | ||
cmp template_err_filtered test_err_filtered | ||
if [ $? -ne 0 ]; then | ||
echo "💩 test 2 failed" | ||
echo "expected:" | ||
cat template_err_filtered | ||
echo "actually:" | ||
cat test_err_filtered | ||
else | ||
echo "✅ test 2 passed" | ||
fi | ||
|
||
rm -f test_out test_err template_out_filtered test_out_filtered template_err_filtered test_err_filtered |