This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (143 loc) · 4.56 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
164
'use strict'
// Native API
const path = require('path')
const fs = require('fs')
const { stdout } = require('process')
const events = require('events')
// Third-party dependencies
const strftime = require('strftime')
const { indexOf, concat, isObject, has, isString, pull, keys, isUndefined } = require('lodash')
class Log {
constructor(opts={}) {
// Internal properties
this._req_levels = [
'emergency'
, 'alert'
, 'critical'
, 'error'
, 'warn'
, 'notice'
, 'info'
, 'debug'
]
this._custom_levels = []
this._level = this._prefix = ''
// Create methods for each level
for (let i in this.getLevels())
this._addPriorityMethod(this.getLevels()[i])
// Configured properties
this.level = opts.level || 'notice'
this.prefix = opts.prefix || '%Y-%m-%d %H:%M:%S -'
this.dir = opts.dir || path.join('./', 'logs')
this.filename = opts.filename || `log_${strftime('%Y%m%d_%H%M%S')}.txt`
this.showLabel = isUndefined(opts.showLabel) ? true : opts.showLabel
this.stdout = isUndefined(opts.stdout) ? true : opts.stdout
this.file = isUndefined(opts.file) ? false : opts.file
events.EventEmitter.call(this)
}
_addPriorityMethod(level) {
this[level] = msg => {
if (isObject(msg)) {
if (has(msg, this.level) && this.getLevels().includes(this.level))
return this[level](msg[this.level])
else if (has(msg, 'default')) {
return this[level](msg['default'])
}
}
if (indexOf(this.getLevels(), this.level) >= indexOf(this.getLevels(), level)) {
msg = this.showLabel ? `[${level.toUpperCase()}] ${msg}` : msg
this.write(msg)
// Emit event with log info
this.emit('entry', {
timestamp: new Date()
, levelIndex: indexOf(this.getLevels(), level)
, level: level.toUpperCase()
, prefix: this.prefix
, msg: msg
})
return msg
}
}
}
get level() { // Current level
return this._level
}
set level(logLevel) { // Set level
logLevel = logLevel.toLowerCase()
if (!this.getLevels().includes(logLevel)) {
throw new Error(`Cannot set log level to '${logLevel}'`)
} else {
this._level = logLevel
}
}
get prefix() { // Prepended string to output
// Render timestamps
return strftime(this._prefix)
}
set prefix(logPrefix) {
this._prefix = logPrefix
}
getFilepath() { // Output filepath
return path.resolve(path.join(this.dir, this.filename))
}
getLevels() { // Required & custom levels
return concat(this._req_levels, this._custom_levels)
}
get levelIndex() {
return indexOf(this.getLevels(), this.level)
}
set levelIndex(index) {
if (this.getLevels()[index])
this.level = this.getLevels()[index]
return this.level
}
levelIndex() { // Deprecated in favor of log.levelIndex property
return this.levelIndex
}
write(options) { // Handle output
// Output non-object
if (!isObject(options)) {
let output = this.prefix ? this.prefix + ' ' + options : options
// File output
if (this.file) {
// Create directory and file if they do not exist
if (!fs.existsSync(this.dir)) fs.mkdirSync(this.dir);
if (!fs.existsSync(this.getFilepath()))
fs.writeFileSync(this.getFilepath(), '');
// Append message to log file
fs.appendFile(this.getFilepath(), output + '\r\n', err => {
if (err) throw new Error(lerr)
})
}
// Standard out
if (this.stdout) {
stdout.write(output + '\r\n')
}
// Split-output handling
} else if (isObject(options)) {
if (has(options, this.level) && isObject(options[this.level]))
this[this.level](options[this.level])
}
}
addLevel(level) { // Add user-defined level
if (isString(level)) {
this._custom_levels.push(level)
// Define method for custom level
this._addPriorityMethod(level)
return this.getLevels()
} else
throw new Error('Custom level names must be type String')
}
removeLevel(level) { // Remove user-defined level
if (this._custom_levels.includes(level)) {
pull(this._custom_levels, level)
// Remove method for level
delete this[level]
// Set level to DEBUG if current level has been removed
this.level = this.level === level ? 'debug' : this.level
return level
}
}
}
Log.prototype.__proto__ = events.EventEmitter.prototype;
module.exports = Log