index.js   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 1.42

Size

Lines of Code 70
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 17
c 1
b 0
f 0
nc 16
mnd 1
bc 10
fnc 12
dl 0
loc 70
rs 10
bpm 0.8333
cpm 1.4166
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A module.exports.makeCustomLogger(ꞌerrorꞌ) 0 9 3
A ➔ ??? 0 4 1
1
var term = require('terminal-kit').terminal;
2
var moment = require('moment');
3
var stringz = require('stringz'); // for emoji support ❤️
4
5
// keep the native pipe to stdout & stderr
6
module.exports.nativeLog = global.console.log;
7
module.exports.nativeError = global.console.error;
8
9
// enables or disables certain types of logging
10
var loggerTypes = {}
11
module.exports.enable = type => {
12
  module.exports.options.styles[type] = module.exports.options.styles[type] || [term, module.exports.nativeLog];
13
  loggerTypes[type] = true;
14
};
15
module.exports.disable = type => loggerTypes[type] = false;
16
module.exports.isEnabled = type => loggerTypes[type];
17
18
module.exports.options = {
19
  typePadding: '         ',       // dictates the width of the type prefix
20
  styles: {                       // contains term styles for the various prefixes
21
    error: [term.error.bgRed.white, module.exports.nativeError],
22
    warn: [term.error.bgYellow.white, module.exports.nativeError],
23
    info: [term, module.exports.nativeLog],
24
    debug: [term, module.exports.nativeLog],
25
    success: [term.bgGreen.white, module.exports.nativeLog]
26
  },
27
  // a function that takes a date and returns a string
28
  // used to print the date in the prefix
29
  dateFormatter: date => moment(date).format("D/M/YY HH:mm:ss.SSS")
30
}
31
32
var getLogTypePrefix = type => ` [${type}] ${module.exports.options.typePadding.substring(stringz.length(type) + 4)}`;
33
var getPrefix = type => getLogTypePrefix(type) + module.exports.options.dateFormatter(new Date()) + " ";
34
module.exports.printPrefix = (type, t = term) => {t(getPrefix(type));t.styleReset("| ")};
35
36
module.exports.makeSimpleLogger = type => {
37
  module.exports.enable(type);
38
  var TYPE = type == "success" ? "OK" : type.toUpperCase();
39
  global.console[type] = function() {
40
    if (loggerTypes[type]) {
41
      module.exports.printPrefix(TYPE, module.exports.options.styles[type][0]);
42
      module.exports.options.styles[type][1].apply(this, arguments);
43
    }
44
  }
45
}
46
47
module.exports.makeCustomLogger = (type, myfunction) => {
48
  module.exports.enable(type);
49
  global.console[type] = function() {
50
    if (loggerTypes[type]) {
51
      myfunction.apply(this, arguments);
52
    }
53
  };
54
}
55
56
module.exports.makeSimpleLogger("debug");
57
module.exports.makeSimpleLogger("info");
58
global.console.log = global.console.info;
59
module.exports.makeSimpleLogger("warn");
60
module.exports.makeSimpleLogger("success");
61
62
module.exports.makeCustomLogger("error", function() {
63
  var isTrace = typeof arguments[0] == "string" && arguments[0].substring(0, 5) == "Trace";
64
  var type = isTrace ? "TRACE" : "ERROR";
65
  module.exports.printPrefix(type, module.exports.options.styles.error[0]);
66
  if (isTrace) {
67
    arguments[0] = arguments[0].substring(7);
68
  }
69
  module.exports.options.styles.error[1].apply(this, arguments);
70
})
71