-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
executable file
·138 lines (110 loc) · 3.05 KB
/
logger.js
File metadata and controls
executable file
·138 lines (110 loc) · 3.05 KB
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
/*
* ExerciseCheck logger
*/
'use strict';
const Fs = require('fs');
const logConfig = require('./config.js').logConfig;
const logfile = undefined;
/* private helper functions */
const getTime = () => {
return new Date().getTime().toString();
};
const _logPrefix = (app) => {
return '[' + getTime() + '] ' + app + ': ';
};
const _error = (app, str) => {
return console.error(_logPrefix(app) + str);
};
// ---
function loggerBase(app, toOut, toFile, attemptWrite) {
this.app = app;
this.toOut = toOut;
this.toFile = toFile;
this.attemptWrite = attemptWrite;
/* log entry for this logger instance gets a custom prefix based on the name of the app */
const logPrefix = () => {
return _logPrefix(app);
};
const __writeToLog = (logstr) => {
if (attemptWrite && toFile) {
try {
Fs.appendFileSync(logfile, logstr, 'utf8');
}
catch (err) {
_error('LOGGER', err);
// ENOENT? The log file disappeared somehow!
if (err.code === 'ENOENT') {
_error('LOGGER', 'the log file has disappeared; will stop attempting to write to file');
attemptWrite = false;
logfile = undefined;
};
}
}
};
this.log = (str) => {
const logstr = logPrefix() + str.toString();
__writeToLog(logstr + '\n');
if (toOut) {
console.log(logstr);
}
};
this.error = (str) => {
const logstr = logPrefix() + str.toString();
__writeToLog(logstr + '\n');
if (toOut) {
console.error(logstr);
}
};
this.close = () => {
attemptWrite = false;
logfile = undefined;
};
};
const logger = {
/**
* a generic logger
* @param {string} app -- the application writing to the log
* @param {bool} toOut -- enable writing to stdout and stderr (default: true)
* @param {bool} toFile -- enable writing to log file (default: true)
*/
Logger: (app, toOut, toFile) => {
if (toOut !== null) {
toOut = true;
}
if (toFile !== null) {
toFile = true;
}
let attemptWrite = true;
// touch logging directory and log file if neither exist
if (toFile && !logfile) {
try {
Fs.statSync(logConfig.dir);
}
catch (err) {
if (err.code === 'ENOENT') {
_error('LOGGER', 'mkdir ' + logConfig.dir);
Fs.mkdirSync(logConfig.dir, (err) => {
_error('LOGGER', err);
});
}
else {
attemptWrite = false;
_error('LOGGER', 'failed to stat ' + logConfig.dir + ', error ' + err.code);
_error('LOGGER', 'will not write to file');
}
}
logfile = logConfig.dir + logConfig.pre + '.' + getTime() + '.log';
if (attemptWrite) {
Fs.openSync(logfile, 'a', (err) => {
if (err) {
attemptWrite = false;
_error('LOGGER', 'could not open file at ' + logfile);
_error('LOGGER', err);
}
});
}
}
return new loggerBase(app, toOut, toFile, attemptWrite);
}
};
module.exports = logger;