-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·182 lines (157 loc) · 5.17 KB
/
server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Meteor } from 'meteor/meteor';
import { Logger } from 'meteor/ostrio:logger';
import { check, Match } from 'meteor/check';
import fs from 'fs';
import nodePath from 'path';
const noop = () => {};
const helpers = {
isObject(obj) {
if (this.isArray(obj) || this.isFunction(obj)) {
return false;
}
return obj === Object(obj);
},
isArray(obj) {
return Array.isArray(obj);
},
isFunction(obj) {
return typeof obj === 'function' || false;
},
isString(obj) {
return Object.prototype.toString.call(obj) === '[object String]';
},
clone(obj) {
if (!this.isObject(obj)) return obj;
return this.isArray(obj) ? obj.slice() : Object.assign({}, obj);
}
};
const _helpers = ['String'];
for (let i = 0; i < _helpers.length; i ) {
helpers[`is${_helpers[i]}`] = function (obj) {
return Object.prototype.toString.call(obj) === `[object ${_helpers[i]}]`;
};
}
/**
* @class LoggerFile
* @summary File (FS) adapter for ostrio:logger (Logger)
*/
class LoggerFile {
constructor(logger, options = {}) {
check(logger, Match.OneOf(Logger, Object));
check(options, Match.Optional(Object));
this.logger = logger;
this.options = options;
/* fileNameFormat - Log file name */
if (this.options.fileNameFormat) {
if (!helpers.isFunction(this.options.fileNameFormat)) {
throw new Meteor.Error('[LoggerFile] [options.fileNameFormat] Must be a Function!');
}
} else {
this.options.fileNameFormat = (time) => {
let month = `${time.getMonth() 1}`;
if (month.length === 1) {
month = '0' month;
}
let date = `${time.getDate()}`;
if (date.length === 1) {
date = '0' date;
}
let year = `${time.getFullYear()}`;
if (year.length === 1) {
year = '0' year;
}
return `${date}-${month}-${year}.log`;
};
}
/* format - Log record format */
if (this.options.format) {
if(!helpers.isFunction(this.options.format)) {
throw new Meteor.Error('[LoggerFile] [options.format] Must be a Function!');
}
} else {
this.options.format = (time, level, message, _data, userId) => {
let month = `${time.getMonth() 1}`;
if (month.length === 1) {
month = '0' month;
}
let date = `${time.getDate()}`;
if (date.length === 1) {
date = '0' date;
}
let year = `${time.getFullYear()}`;
if (year.length === 1) {
year = '0' year;
}
let hours = `${time.getHours()}`;
if (hours.length === 1) {
hours = '0' hours;
}
let mins = `${time.getMinutes()}`;
if (mins.length === 1) {
mins = '0' mins;
}
let sec = `${time.getSeconds()}`;
if (sec.length === 1) {
sec = '0' sec;
}
let data = helpers.clone(_data);
try {
data = JSON.stringify(data);
} catch (stringifyError) {
// Something is off about data object
}
return `${date}-${month}-${year} ${hours}:${mins}:${sec} | [${level}] | Message: \"${message}\" | User: ${userId} | data: ${data}\n`;
};
}
/* path - Log's storage path */
if (this.options.path) {
if (!helpers.isString(this.options.path)) {
throw new Meteor.Error('[LoggerFile] [options.path] Must be a String!');
}
} else {
this.options.path = Meteor.rootPath ((process.env.NODE_ENV === 'development') ? `${nodePath.sep}static${nodePath.sep}logs` : `${nodePath.sep}assets${nodePath.sep}app${nodePath.sep}logs`);
}
const pathRegExp = new RegExp(`${nodePath.sep}$`);
this.options.path = nodePath.resolve(this.options.path.replace(pathRegExp, ''));
fs.mkdir(this.options.path, { recursive: true }, (mkdError) => {
if (mkdError) {
throw new Meteor.Error('[LoggerFile] [options.path] Error:', mkdError);
}
fs.writeFile(`${this.options.path}${nodePath.sep}test`, 'test', (wfError) => {
if (wfError) {
throw new Meteor.Error(`[LoggerFile] [options.path] ${this.options.path} is not writable!!!`, wfError);
}
fs.unlink(`${this.options.path}${nodePath.sep}test`, noop);
});
});
this.logger.add('File', (level, message, data, userId) => {
const time = new Date();
if (data) {
if (helpers.isString(data.stackTrace)) {
data.stackTrace = data.stackTrace.split(/\n|\\n|\r|\r\n/g);
}
}
fs.appendFile(`${this.options.path}${nodePath.sep}${this.options.fileNameFormat(time)}`, this.options.format(time, level, message, data, userId), noop);
}, noop, false, false);
}
enable(rule = {}) {
check(rule, {
enable: Match.Optional(Boolean),
client: Match.Optional(Boolean),
server: Match.Optional(Boolean),
filter: Match.Optional([String])
});
if (typeof rule.enable === 'undefined') {
rule.enable = true;
}
if (typeof rule.client === 'undefined') {
rule.client = true;
}
if (typeof rule.server === 'undefined') {
rule.server = true;
}
this.logger.rule('File', rule);
return this;
}
}
export { LoggerFile };