-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
262 lines (223 loc) · 9.63 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*global DEBUG*/
/*global v8debug*/
/*global console*/
/*global process*/
var Clrlog = null;
(function () {
"use strict";
/**
* Lightweight colorful JavaScript application logger with stack trace and logfile support for node.js
* It detect by itself if node.js application is in debug mode.
* Debug mode can be also enabled by set global.DEBUG===true
* There's also logfile support to save logmessages including logging time
*
* Clrlog works called as a plain old javascript function and as an object for more comples purposes
*
* @author Bernhard Bezdek <[email protected]>
* @see https://github.com/BernhardBezdek/Clrlog/blob/master/README.md>
* @license MIT https://github.com/BernhardBezdek/Clrlog/blob/master/LICENSE
* @class Clrlog
* @constructor
* @param {Object} mLogdata The logging data (Any datatype allowed
* @param {String} sType default 'message' (message, success, warning, error available)
* @param {Boolean} sLogFile write messages into a log file (default none)
* @param {Boolean} bTraceErrors Determine if errormessages should output as a trace shutdown application (default false)
* @param {String} sProcessEnv Determine a process variable which enables the specific logging (Useful when several parts should have separate logging)
* @return {Object} A Clrlog object (if called as a function
*/
Clrlog = function (mLogdata, sType, sLogFile, sProcessEnv, bTraceErrors) {
// Check if Clrlog was called as a function
if (this !== undefined) {
var fs = require('fs');
if (process.env.DEBUG === undefined) {
process.env.DEBUG = false;
}
if (typeof sProcessEnv === 'string') {
this.processEnv = sProcessEnv;
} else {
sProcessEnv = 'Clrlog';
this.processEnv = 'Clrlog';
}
// The end string for colored messages
var logMethod = 'log';
var logArchive = {};
// Determine the output type
if (this.types[sType] !== undefined) {
this.type = sType;
}
// Enable Tracing for Errors
if (typeof bTraceErrors === 'boolean' || typeof sProcessEnv === 'boolean') {
this.trace = bTraceErrors;
}
if (this.explicityDebug === undefined) {
this.explicityDebug = false;
}
if (process !== undefined && typeof process.env === 'object' && typeof sProcessEnv === 'string' && process.env[sProcessEnv] !== undefined) {
this.explicityDebug = true;
this.namespace = sProcessEnv.replace('LOG_', '') + '::'
} else if (sProcessEnv !== undefined) {
this.namespace = sProcessEnv.replace('LOG_', '') + '::';
this.explicityDebug = false;
}
// Override log method
if (this.trace || ( process.env[sProcessEnv] !== undefined && process.env.TRACE !== undefined)) {
logMethod = 'trace';
}
if (sLogFile !== undefined) {
this.logFile = sLogFile;
}
// Hanlde writing into a logfile
if (this.logFile !== false && this.logLevel.split(',').indexOf(this.type) !== -1) {
var sWriteFile = new Date().toString() + ' - ' + this.namespace + ' | ' + sType.toUpperCase() + ' ᑀ ';
try {
if (['boolean', 'number', 'string'].indexOf(typeof (mLogdata)) !== -1) {
sWriteFile += mLogdata.replace(/"/g, '');
} else {
sWriteFile += JSON.stringify(mLogdata, null, "\t");
}
sWriteFile += "\n";
fs.appendFile(sLogFile, sWriteFile, function (error) {
if (error !== null) {
Clrlog(error);
}
if (logArchive[sLogFile] === undefined) {
logArchive[sLogFile] = setTimeout(function () {
fs.stat(sLogFile, function (error, stats) {
if (error === null) {
if (stats.size > 10000000) {
fs.rename(sLogFile, sLogFile.replace(/.(\w)*$/, function (match) {
return "." + Math.round(new Date().getTime() / 1000) + match;
}), function (error) {
if (error !== null) {
console.log(error);
}
delete logArchive[sLogFile];
});
} else {
delete logArchive[sLogFile];
}
} else {
delete logArchive[sLogFile];
}
});
}, 10000);
}
});
} catch (e) {
Clrlog(e, 'error');
}
}
if (process !== undefined && typeof process.env === 'object' && typeof sProcessEnv === 'string' && process.env[sProcessEnv] !== undefined) {
this.explicityDebug = true;
this.namespace = sProcessEnv.replace('LOG_', '') + '::'
} else if (sProcessEnv !== undefined) {
this.namespace = sProcessEnv.replace('LOG_', '') + '::';
this.explicityDebug = false;
}
// logMethod = 'warn';
// In debug mode colorized messages are dumped out
if (this.explicityDebug === true || process.env.DEBUG === true || process.env.DEBUG === 'true') {
if (['boolean', 'number', 'string'].indexOf(typeof (mLogdata)) !== -1) {
console[logMethod](this.types[this.type] + new Date().toISOString() + ' - ' + this.namespace + ' [' + this.type + '] ' + mLogdata + this.endLog);
} else {
console[logMethod](this.types[this.type]);
console[logMethod](new Date().toISOString() + ' - ' + this.namespace + ' [' + this.type + '] ' );
console[logMethod](mLogdata);
console[logMethod](this.endLog);
}
} else if (this.logLevel.indexOf(sType) !== -1) {
if (['boolean', 'number', 'string'].indexOf(typeof (mLogdata)) !== -1) {
console[logMethod](new Date().toISOString() + ' - ' + this.namespace + ' [' + this.type + '] ' + mLogdata);
} else {
console[logMethod](new Date().toISOString() + ' - ' + this.namespace + ' [' + this.type + '] ');
console[logMethod](mLogdata);
}
}
} else {
// If it was a function call run as a class instance
return new Clrlog(mLogdata, sType, sLogFile, sProcessEnv, bTraceErrors);
}
};
/**
* Determine if trace is used for errors
* @property trace (default false)
* @type {Boolean}
*/
Clrlog.prototype.trace = false;
/**
* Determine specific debugin is enabled
* @property debug (default false)
* @type {Boolean}
*/
Clrlog.prototype.debug = false;
/**
* The default message type
* @property type (default message)
* @type {String}
*/
Clrlog.prototype.type = 'message';
/**
* Available log types
* @property types
* @type {Object}
*/
Clrlog.prototype.types = {
message: '\x1B[34m', // Cyan colored
success: '\x1B[32m', // Green colored
warning: '\x1B[33m', // Yellow colored
error: '\x1B[31m' // Red colored
};
/**
* The finalizing string for noncolored log Messages
* @type {string}
*/
Clrlog.prototype.endLog = '\x1B[39m';
/**
* @property logFile
* @type {undefined}
*/
Clrlog.prototype.logFile = false;
/**
* Specify which types log messages are written into a file
* @property logLevel
* @type {String}
*/
Clrlog.prototype.logLevel = 'error,warning';
/**
* Throw error message with previously defined settings
*
* @method error
* @param message
*/
Clrlog.prototype.error = function (message) {
this.constructor(message, 'error', this.logFile, this.processEnv, this.trace);
};
/**
* Throw warning with previously defined settings
*
* @method warning
* @param message
*/
Clrlog.prototype.warning = function (message) {
this.constructor(message, 'warning', this.logFile, this.processEnv, this.trace);
};
/**
* Throw success message with previously defined settings
*
* @method success
* @param message
*/
Clrlog.prototype.success = function (message) {
this.constructor(message, 'success', this.logFile, this.processEnv, this.trace);
};
/**
* Throw message with previously defined settings
*
* @method message
* @param message
*/
Clrlog.prototype.message = function (message) {
this.constructor(message, 'message', this.logFile, this.processEnv, this.trace);
};
})();
module.exports = Clrlog;