-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (58 loc) · 1.6 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
/**
*
* User: toby
* Date: 14-5-27
* Time: 上午10:26
*/
var fs = require('fs');
var path = require('path');
var moment = require('moment');
function tLogger(logfile, multifile) {
this.basename = "";
this.extname = "";
this.logFile = logfile;
this.dirname = path.dirname(this.logFile);
this.multiFile = multifile;
if (!fs.existsSync(this.dirname)) {
try {
fs.mkdirSync(this.dirname);
}catch (e) {
console.log('Can not create directory' + this.dirname + '. ' + e);
process.exit(1);
}
}
if (this.multiFile) {
this.extname = path.extname(this.logFile);
this.basename = path.basename(this.logFile, this.extname);
}else {
this.basename = path.basename(this.logFile);
}
this.write = function (type, str) {
str = moment().format() + "\t[" + type +"]\t" + str + "\n";
fs.appendFile(this.getFile(), str, function (err) {
if (err) {
console.log(err);
}
});
}
this.warn = function (str) {
this.write('WARN', str);
}
this.error = function (str) {
this.write('ERROR', str);
}
this.info = function (str) {
this.write('INFO', str);
}
this.notice = function (str) {
this.write('NOTICE', str);
}
this.getFile = function() {
if (this.multiFile) {
return this.dirname + '/' + this.basename + '-' + moment().format('YYYY-MM-DD-HH') + this.extname;
}else {
return this.dirname + '/' + this.basename;
}
}
}
module.exports = tLogger;