-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (98 loc) · 2.55 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
/*
* Logger API for logging things
*/
const fs = require('fs')
const colour = require('colour')
/*
* F Helpers
*/
const identity = x => x
/*
* Environment setup
*/
const DEBUG_LVL = process.env.DEBUG_LVL || 0
const USER = process.env.USER || ''
const LOGFILE = process.env.LOGFILE || 'logs.txt'
/*
* Logger helpers
*/
const logType = (type) =>
type == 1 ? 'INFO ' :
type == 2 ? 'WARN ' :
type == 3 ? 'DEBUG' :
''
const formatNow = (msg, user, lvl) => logType(lvl) + '[' + (new Date()).toUTCString() + '][' + user + '] ' + msg
const printLog = (msg, lvl, code) => {
if (DEBUG_LVL >= lvl) {
console.log(colour[code].apply(1, [formatNow(msg, USER, lvl)]))
}
}
const printErr = (err) => {
if (err) {
console.log(err)
}
}
const appendLog = (msg, lvl) => {
if (DEBUG_LVL >= lvl) {
fs.appendFile(LOGFILE, formatNow(msg + '\n', USER, lvl), printErr)
}
}
/*
Logger exports
*/
const INFO = (msg) => printLog(msg, 1, 'blue')
const WARN = (msg) => printLog(msg, 2, 'red')
const DEBUG = (msg) => printLog(msg, 3, 'yellow')
const LOG_INFO = (msg) => appendLog(msg, 1)
const LOG_WARN = (msg) => appendLog(msg, 2)
const LOG_DEBUG = (msg) => appendLog(msg, 3)
/*
* Log API
*/
const getLogs = () => {
const logFileContent = fs.existsSync(LOGFILE) ? fs.readFileSync(LOGFILE) : undefined
return logFileContent ? logFileContent.toString().split('\n').filter(identity) : logFileContent
}
const entryIs = (lvl) => (entry) => {
const matches = entry.match(/(.*?)\[/)
return matches ? matches[1] === logType(lvl) : false
}
const getLogsOf = (lvl) => {
const logs = getLogs()
return logs ? logs.filter(entryIs(lvl)) : []
}
const getInfos = () => getLogsOf(1)
const getWarns = () => getLogsOf(2)
const getDebugs = () => getLogsOf(3)
/*
* NetLogger
*/
const net = require('net')
function NetLogger(users) {
this.users = users
this.connections = Object.keys(users).reduce((acc, user) => (
acc[user] = net.connect(...users[user]), acc
), {})
}
NetLogger.prototype.INFO = function(username, message) {
const user = this.connections[username]
if (!user) return console.log('Error: No such user.')
user.write(formatNow(message + '\n', username, 1))
}
NetLogger.prototype.stop = function() {
const connections = this.connections
Object.keys(connections).forEach(connection => connections[connection].end())
}
module.exports = {
INFO: INFO,
WARN: WARN,
DEBUG: DEBUG,
LOG_INFO: LOG_INFO,
LOG_WARN: LOG_WARN,
LOG_DEBUG: LOG_DEBUG,
getLogs: getLogs,
getInfos: getInfos,
getWarns: getWarns,
getDebugs: getDebugs,
NetLogger: NetLogger,
}