-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
147 lines (114 loc) · 3.65 KB
/
index.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
import { EventEmitter } from 'events'
import * as helpers from './helpers'
import * as defaults from './defaults'
import * as formatter from './formatter'
import consoleTransport from './transports/console'
import fileTransport from './transports/file'
import * as t from './types'
export class LoggerNeue extends EventEmitter {
[key: string]: any
console: t.NormalizedConsoleOptions
file: t.NormalizedFileOptions | false
levels: t.NormalizedLevelMap
constructor (options: t.Options = {}) {
if (!helpers.isObject(options)) {
throw new TypeError('options must be an object')
}
super()
this.levels = defaults.normalizeLevels(options.levels)
this.file = defaults.normalizeFileOptions(options.file, this.levels)
this.console = defaults.normalizeConsoleOptions(options.console, this.levels)
for (const name of Object.keys(this.levels)) {
this.addLevel(name, this.levels[name])
}
}
addLevel (name: string, properties: t.LevelDescriptor) {
if (name in this) return this
this.levels[name] = defaults.normalizeDefinition(properties)
this[name] = (...args: any[]) =>
this.log(name, ...args)
this[name].format = (template: string, ...args: any[]) => {
if (args.length === 1) {
const obj = args[0]
if (obj == Object(obj)) {
args = obj
}
}
return this.log(name, formatter.format(template, args))
}
return this
}
log (level: string, ...args: any[]) {
let action: string
let number: number
let logged = false
if (typeof level == 'string') {
if (!(level in this.levels)) {
throw new Error(`no such log level is defined ('${level}')`)
}
action = level
number = this.levels[level].level
} else {
const res = this.getLevelByNumber(level)
if (!res) {
throw new Error(`could not find method name for level ${level}`)
}
action = res
number = level
}
if (this.console.level >= number) {
consoleTransport(this as TransportContext, action, formatter, args)
logged = true
}
if (this.file && this.file.level >= number) {
fileTransport(this as TransportContext, action, formatter, args)
logged = true
}
if (logged) {
const props = { name: action, level: number, args }
this.emit('log', props)
this.emit(`log:${action}`, props)
}
}
getLevelByNumber (n: number): string | void {
for (const name of Object.keys(this.levels)) {
if (this.levels[name].level === n) return name
}
}
getNumberOfLevel (name: string): number | void {
const logLevel = this.levels[name]
if (logLevel) return Number(logLevel.level) || 0
}
getLevelNames (): string[] {
return Object.keys(this.levels)
}
getConsoleLevel (): number {
return this.console.level
}
setConsoleLevel (level: string | number) {
const res = typeof level == 'string'
? this.getNumberOfLevel(level)
: Number(level)
this.console.level = typeof res === 'number' ? res : 2
}
getFileLevel (): number {
return this.file ? this.file.level : -1
}
setFileLevel (level: string | number) {
if (!this.file) return
this.file.level = typeof level === 'string'
? this.getNumberOfLevel(level) || 0
: Number(level) || 0
}
}
export interface TransportContext extends LoggerNeue {
file: t.NormalizedFileOptions
}
export interface LogFunction {
(...args: any[]): void,
format (template: string, ...args: any[]): void
}
export default <T extends t.Options> (options?: T) => {
return new LoggerNeue(options) as LoggerNeue & { [K in keyof T['levels']]: LogFunction }
}
export * from './types'