Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Logging: All key-value pairs as JSON and fix colours. Fixes #34.

In Splunk only the first word of the error message was detected:

 action=warning message=Foo bar baz quux info=some_data_here

 as action: warning, message: Foo, info: some_data_here

By doing it all as JSON, this is now fixed.

Also, this required fixing #34 at the same time, due to the colour
codes invalidating the JSON.
  • Loading branch information...
commit d42cf8a5957b3c1002e25f9b140502527691f374 1 parent f1b80b5
@Krinkle Krinkle authored
Showing with 19 additions and 25 deletions.
  1. +19 −25 src/util.js
View
44 src/util.js
@@ -1,5 +1,7 @@
-var logColors;
-require('colors');
+var colors = require('colors'),
+ logColors;
+
+colors.mode = require('tty').isatty(process.stdout.fd) ? 'console' : 'none';
/**
* Like typeof === 'object' but more accurate.
@@ -102,34 +104,26 @@ logColors = {
};
function log(data) {
- var prefix, msg, key, value, dryRun;
-
- prefix = new Date() + ':';
- msg = '';
+ var action, prefix, msg;
- if (!data.action) {
- data.action = 'unspecified';
- }
-
- if (data.dryRun) {
- delete data.dryRun;
- dryRun = true;
+ if (data.action) {
+ action = data.action;
+ delete data.action;
+ } else {
+ action = 'unspecified';
}
- for (key in data) {
- value = data[key];
- if (isObject(value)) {
- value = JSON.stringify(value);
- }
- msg += ' ' + key + '=' + String(value).trim();
- }
+ prefix = '[' + new Date().toUTCString() + '] ';
- if (logColors[data.action]) {
- msg = msg[logColors[data.action]];
- }
+ msg = [
+ 'action=' + action,
+ JSON.stringify(data)
+ ].join(' ');
- if (dryRun) {
- msg = ' dryRun=true'[logColors.dryRun] + msg;
+ if (data.dryRun) {
+ msg = msg[logColors.dryRun];
+ } else if (logColors[action]) {
+ msg = msg[logColors[action]];
}
console.log(prefix + msg);
Please sign in to comment.
Something went wrong with that request. Please try again.