This repository was archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (126 loc) · 3.31 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
function Log (options) {
this.levels = {
'DEBUG': 5,
'INFO': 4,
'NOTICE': 3,
'WARN': 2,
'ERROR': 1,
'FATAL': 0
}
this.options = options || {}
if (this.options.loglevel) { // DEBUG, INFO, NOTICE, WARN, ERROR, FATAL
this.loglevel = this.levels[this.options.loglevel]
} else {
this.loglevel = 2
}
if (this.options.name) {
this.name = this.options.name
} else {
this.name = require('./package.json').name
}
if (this.options.hostname) {
this.hostname = this.options.hostname
} else {
let os = require('os')
this.hostname = os.hostname()
}
if (this.options.file) {
this.fs = require('fs')
this.path = require('path')
this.options.file = this.path.resolve(this.options.file)
if (!this.fs.lstatSync(this.options.file).isFile()) {
throw new Error(this.options.file + ' is not a File...')
}
try {
this.fs.accessSync(this.options.file, this.fs.constants.R_OK | this.fs.constants.W_OK)
} catch (err) {
try {
this.fs.writeFileSync(this.options.file, '')
} catch (err) {
throw new Error('Could not Create File ' + this.options.file + '\n' + err.toString())
}
}
}
this.info = function (message) {
if (this.loglevel >= this.levels.INFO) {
return this.log('INFO', message)
} else {
return ''
}
}
this.notice = function (message) {
if (this.loglevel >= this.levels.NOTICE) {
return this.log('NOTICE', message)
} else {
return ''
}
}
this.fatal = function (message) {
if (this.loglevel >= this.levels.FATAL) {
return this.log('FATAL', message)
} else {
return ''
}
}
this.warn = function (message) {
if (this.loglevel >= this.levels.WARN) {
return this.log('WARN', message)
} else {
return ''
}
}
this.error = function (message) {
if (this.loglevel >= this.levels.ERROR) {
return this.log('ERROR', message)
} else {
return ''
}
}
this.debug = function (message) {
if (this.loglevel >= this.levels.DEBUG) {
return this.log('DEBUG', message)
} else {
return ''
}
}
this.log = function (tag, message) {
let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message
switch (tag) {
case 'INFO':
console.info(msg)
break
case 'NOTICE':
console.log(msg)
break
case 'WARN':
console.warn(msg)
break
case 'ERROR':
console.error(msg)
break
case 'FATAL':
console.error(msg)
break
case 'DEBUG':
console.log(msg)
break
default:
console.log(msg)
}
if (this.options.file) {
this.fs.appendFile(this.options.file, msg + '\n', function (error) {
if (error) {
console.error('Cannot write to File ' + this.options.file)
}
})
}
return msg
}
this.getDate = function () {
var tzoffset = (new Date()).getTimezoneOffset() * 60000 // offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1)
return localISOTime.split('.')[0].trim()
}
return this
}
module.exports = Log