-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog.js
149 lines (128 loc) · 4.29 KB
/
log.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
/**
* Feedability: Node.js Feed Proxy With Readability
* Copyright (c) 2011, Matthias -apoc- Hecker <http://apoc.cc/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Class for console and file logging, works sync/async, supports
* log levels and is customizable by the feedability configuration
* file.
*
* @fileOverview
*/
// built in libraries
var fs = require('fs'),
uri = require('url');
// internal libraries
var func = require('./func.js');
/**
* Usage Example:
*
* <pre>
* var log = new require('./log.js').Logger('filters');
* log.warn('no need to mention warning here its prefixed', domain);
* </pre>
*/
var Logger = function(module) {
this.module = module;
if(!func.file_exists(Logger.options.path)) {
fs.mkdirSync(Logger.options.path, 0750);
}
}
Logger.levels = {
'error': 1,
'warn': 2,
'info': 3,
'debug': 4
}
Logger.options = {
console: true, // wherever the log messages may be logged to console
stderr: true, // log messages are logged to stderr (blocking!)
file: true, // activates file logging
file_seperate: true, // seperate logfiles per loglevel
syncronized: false, // using sync methods for writing to logfiles
use_domain: true, // write seperate logfile for each domain
path: './logs', // write logfiles in this directory
console_level: 3, // 0: no logging 1: error 2: warn 3: info 4: debug
file_level: 4,
modules: null // array of modules to log (other modules are ignored)
};
Logger.merge_options = function(new_options) {
Logger.options = func.object_merge(Logger.options, new_options);
}
Logger.openfds = {};
Logger.close = function() {
func.array_foreach(Logger.openfds, function(filename, fd) {
fs.closeSync(fd);
})
}
Logger.prototype.log = function(level, message, domain) {
if(Logger.options.modules &&
!func.array_includes(Logger.options.modules, this.module))
return; // ignore log messages if not on explicit list
lnum = Logger.levels[level];
// domain maybe a url?
var logtype;
if(domain && Logger.options.use_domain) {
if(domain.substr(0, 7) == 'http://') {
domain = uri.parse(domain).hostname;
}
logtype = level.toUpperCase() + '] [' + domain;
}
else {
logtype = level.toUpperCase();
}
// formatting of log message
var time = (new Date()).toLocaleString();
message = time + ' [' + this.module + '] [' + logtype + '] '+message+'\n';
// console logging:
if(Logger.options.console && Logger.options.console_level >= lnum) {
var console_stream = (Logger.options.stderr) ? process.stderr
: process.stdout;
console_stream.write(message);
}
// file logging
if(Logger.options.file && Logger.options.file_level >= lnum) {
var path = Logger.options.path;
path += (path.substr(path.length-2) != '/') ? '/' : '';
var filename = path + (domain || 'feedability');
if(Logger.options.file_seperate) {
filename += '-' + level.toLowerCase();
}
filename += '.log';
if(!Logger.openfds[filename]) {
Logger.openfds[filename] = fs.openSync(filename, 'a+', 0600);
}
if(Logger.options.syncronized) {
fs.writeSync(Logger.openfds[filename], message, null, 'utf8');
}
else {
fs.write(Logger.openfds[filename], message, null, 'utf8', function() {});
}
} // if file logging
}
Logger.prototype.error = function(message, domain) {
this.log('error', message, domain);
}
Logger.prototype.warn = function(message, domain) {
this.log('warn', message, domain);
}
Logger.prototype.info = function(message, domain) {
this.log('info', message, domain);
}
Logger.prototype.debug = function(message, domain) {
this.log('debug', message, domain);
}
exports.Logger = Logger;