forked from plugCubed/plugAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
84 lines (71 loc) · 2.28 KB
/
Copy pathlogger.js
File metadata and controls
84 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var fs, chalk, months, levels;
fs = require('fs');
chalk = require('chalk');
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
levels = {
success: ['green'],
info: ['white'],
warning: ['yellow', 'underline'],
error: ['bgRed', 'white', 'bold']
};
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()), pad(d.getMinutes()), pad(d.getSeconds())].join(':');
return [pad(d.getDate()), months[d.getMonth()], time].join(' ');
}
function chalkPaint(sidewalkChalks, text) {
var combinedChalk = chalk;
for (var i in sidewalkChalks) {
if (!sidewalkChalks.hasOwnProperty(i)) continue;
combinedChalk = combinedChalk[sidewalkChalks[i]];
}
return combinedChalk(text);
}
function Logger(channel) {
/**
* Settings for Logger
* @type {{fileOutput: boolean, filePath: string, consoleOutput: boolean}}
*/
var settings = {
fileOutput: false,
filePath: '',
consoleOutput: true
};
function createLogFunc(level) {
return function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(level);
log.apply(this, args);
}
}
function log() {
var args = Array.prototype.slice.call(arguments);
var level = 'info';
if (levels[args[0]] !== undefined) {
level = args.shift();
}
args.unshift(new Array(10 - channel.length - 2).join(' '));
args.unshift('[' + channel + ']');
args.unshift(new Array(10 - level.length - 2).join(' '));
args.unshift(chalkPaint(levels[level], '[' + level.toUpperCase() + ']'));
args.unshift(timestamp());
if (settings.consoleOutput) {
console.log.apply(console, args);
}
if (settings.fileOutput) {
fs.appendFileSync(settings.filePath, chalk.stripColor(args.join(' ')) + '\n');
}
}
return {
settings: settings,
success: createLogFunc('success'),
info: createLogFunc('info'),
warn: createLogFunc('warning'),
warning: createLogFunc('warning'),
error: createLogFunc('error')
};
}
module.exports = Logger;