-
Notifications
You must be signed in to change notification settings - Fork 1
/
brolog.ts
185 lines (157 loc) · 5.15 KB
/
brolog.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*!
* Brolog JavaScript Library v0.2.0
* https://github.com/zixia/brolog
*
* Copyright Zhuohuan LI <zixia@zixia.net>
* Released under the ISC license
* https://github.com/zixia/brolog/blob/master/LICENSE
*
* Date: 2016-07
*/
export type LevelName = 'silent'
| 'error'
| 'warn'
| 'info'
| 'verbose'
| 'silly'
export type LevelTitle = 'ERR'
| 'WARN'
| 'INFO'
| 'VERB'
| 'SILL'
export enum LogLevel {
silent = 0,
error = 1,
warn = 2,
info = 3,
verbose = 4,
silly = 5,
}
export class Brolog {
private static logLevel = LogLevel.info
private static prefixFilter: RegExp = /.*/ // Match all by default
constructor() {
// console.log('constructor')
}
public static instance(
levelName?: LevelName,
prefix?: string | RegExp,
): Brolog {
Brolog.level(levelName)
Brolog.prefix(prefix)
return preSetInstance
}
public prefix(filter?: string | RegExp): RegExp { return Brolog.prefix(filter) }
public static prefix(filter?: string | RegExp): RegExp {
if (filter) {
if (typeof filter === 'string') {
Brolog.prefixFilter = new RegExp(filter, 'i')
} else if (filter instanceof RegExp) {
Brolog.prefixFilter = filter
} else {
throw new Error('unsupported prefix filter')
}
}
return Brolog.prefixFilter
}
public level(levelName?: LevelName) { return Brolog.level(levelName) }
public static level(levelName?: LevelName): LevelName {
if (levelName) {
// console.log('levelName: ' + levelName)
// http://stackoverflow.com/a/21294925/1123955
// XXX: fix the any here?
const logLevel = LogLevel[levelName.toLowerCase() as any] as any
if (logLevel === undefined) { // be aware of number 0 here
// console.log(logLevel)
// console.log(LogLevel)
throw new Error('level name error')
}
Brolog.logLevel = logLevel
}
return LogLevel[Brolog.logLevel] as LevelName
}
// private log(levelTitle: LevelTitle, prefix: string, message: string) { return Brolog.log(levelTitle, prefix, message) }
private static log(levelTitle: LevelTitle, prefix: string, message: string) {
if (!Brolog.prefixFilter.test(prefix)) {
return // skip message not match prefix filter
}
const args = Array.prototype.slice.call(arguments, 3) || []
args.unshift(Brolog.timestamp() + ' ' + levelTitle + ' ' + prefix + ' ' + (message || ''))
switch (levelTitle) {
case 'ERR':
console.error.apply(console, args)
break
case 'WARN':
console.warn.apply(console, args)
break
case 'INFO':
console.info.apply(console, args)
break
default:
case 'VERB':
case 'SILL':
console.log.apply(console, args)
}
}
public error(prefix: string, ...args: any[]): void { return Brolog.error.apply(null, arguments)}
public static error(prefix: string, ...args: any[]): void {
if (Brolog.logLevel < LogLevel.error) {
return
}
const argList = Array.prototype.slice.call(arguments)
argList.unshift('ERR')
Brolog.log.apply(null, argList)
}
public warn(prefix: string, ...args: any[]): void { return Brolog.warn.apply(null, arguments)}
public static warn(prefix: string, ...args: any[]): void {
if (Brolog.logLevel < LogLevel.warn) {
return
}
const argList = Array.prototype.slice.call(arguments)
argList.unshift('WARN')
Brolog.log.apply(null, argList)
}
public info(prefix: string, ...args: any[]): void { return Brolog.info.apply(null, arguments)}
public static info(prefix: string, ...args: any[]): void {
if (Brolog.logLevel < LogLevel.info) {
return
}
const argList = Array.prototype.slice.call(arguments)
argList.unshift('INFO')
Brolog.log.apply(null, argList)
}
public verbose(prefix: string, ...args: any[]): void { return Brolog.verbose.apply(null, arguments)}
public static verbose(prefix: string, ...args: any[]): void {
if (Brolog.logLevel < LogLevel.verbose) {
return
}
const argList = Array.prototype.slice.call(arguments)
argList.unshift('VERB')
Brolog.log.apply(null, argList)
}
public silly(prefix: string, ...args: any[]): void { return Brolog.silly.apply(null, arguments)}
public static silly(prefix: string, ...args: any[]): void {
if (Brolog.logLevel < LogLevel.silly) {
return
}
const argList = Array.prototype.slice.call(arguments)
argList.unshift('SILL')
Brolog.log.apply(null, argList)
}
private static timestamp() {
const date = new Date()
const hour = date.getHours()
const min = date.getMinutes()
const sec = date.getSeconds()
let stampStr = ''
stampStr += (sec < 10) ? ('0' + sec) : sec
stampStr += ':'
stampStr += (min < 10) ? ('0' + min) : min
stampStr += ':'
stampStr += (hour < 10) ? ('0' + hour) : hour
return stampStr
}
}
export const preSetInstance = new Brolog()
export { preSetInstance as log }
export default preSetInstance