-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
163 lines (137 loc) Β· 3.93 KB
/
index.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var assert = require('assert')
var emojis = {
trace: 'π',
debug: 'π',
info: 'β¨',
warn: 'β οΈ',
error: 'π¨',
fatal: 'π'
}
var levels = {
trace: 10,
debug: 20,
info: 30,
warn: 40,
error: 50,
fatal: 60
}
var defaultColors = {
foreground: '#d3c0c8',
background: '#2d2d2d',
black: '#2d2d2d',
red: '#f2777a',
green: '#99cc99',
yellow: '#ffcc66',
blue: '#6699cc',
magenta: '#cc99cc',
cyan: '#66cccc',
white: '#d3d0c8',
brightBlack: '#747369'
}
module.exports = Nanologger
function Nanologger (name, opts) {
opts = opts || {}
if (!(this instanceof Nanologger)) return new Nanologger(name, opts)
assert.equal(typeof opts, 'object', 'nanologger: opts should be type object')
this._name = name || ''
this._colors = Object.assign({}, defaultColors, opts.colors || {})
try {
this.logLevel = window.localStorage.getItem('logLevel') || 'info'
} catch (e) {
this.logLevel = 'info'
}
this._logLevel = levels[this.logLevel]
}
Nanologger.prototype.trace = function () {
var args = [ 'trace' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype.debug = function () {
var args = [ 'debug' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype.info = function () {
var args = [ 'info' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype.warn = function () {
var args = [ 'warn' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype.error = function () {
var args = [ 'error' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype.fatal = function () {
var args = [ 'fatal' ]
for (var i = 0, len = arguments.length; i < len; i++) args.push(arguments[i])
this._print.apply(this, args)
}
Nanologger.prototype._print = function (level) {
if (levels[level] < this._logLevel) return
var time = getTimeStamp()
var emoji = emojis[level]
var name = this._name || 'unknown'
var msgColor = (level === 'error' || level.fatal)
? this._colors.red
: level === 'warn'
? this._colors.yellow
: this._colors.green
var objs = []
var args = [ null ]
var msg = '%c%s ' + emoji + ' %c%s'
args.push(color(this._colors.brightBlack), time)
args.push(color(this._colors.magenta), name)
for (var i = 1, len = arguments.length; i < len; i++) {
var arg = arguments[i]
if (typeof arg === 'string') {
if (i === 1) {
// first string argument is in color
msg += ' %c%s'
args.push(color(msgColor))
args.push(arg)
} else if (/ms$/.test(arg)) {
// arguments finishing with 'ms', grey out
msg += ' %c%s'
args.push(color(this._colors.brightBlack))
args.push(arg)
} else {
// normal colors
msg += ' %c%s'
args.push(color(this._colors.white))
args.push(arg)
}
} else if (typeof arg === 'number') {
msg += ' %c%d'
args.push(color(this._colors.magenta))
args.push(arg)
} else {
objs.push(arg)
}
}
args[0] = msg
objs.forEach(function (obj) {
args.push(obj)
})
// In IE/Edge console functions don't inherit from Function.prototype
// so this is necessary to get all the args applied.
Function.prototype.apply.apply(console.log, [console, args])
}
function color (color) {
return 'color: ' + color + ';'
}
function getTimeStamp () {
var date = new Date()
var hours = pad(date.getHours().toString())
var minutes = pad(date.getMinutes().toString())
var seconds = pad(date.getSeconds().toString())
return hours + ':' + minutes + ':' + seconds
}
function pad (str) {
return str.length !== 2 ? 0 + str : str
}