-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.js
220 lines (185 loc) · 4.69 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
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
/**
* var log = logger(logger.LOG, "ui");
*
* ## .pp(object)
*
* debugs
*
* ## .later
*
* promise.then(log.error.later("foo bar"));
*/
"use strict";
var _ = require("lodash");
var util = require("util");
var inspect = require("util").inspect;
var os = require('./os');
var universal = require("./universal");
var fs = universal.isRenderer() ? universal.requireMain("fs") : require("fs");
var DEBUG = 0;
var INFO = 1;
var LOG = 2;
var WARN = 3;
var ERROR = 4;
var levels = {
debug: DEBUG,
info: INFO,
log: LOG,
warn: WARN,
error: ERROR,
};
var root = module.exports = exports = logger(
LOG,
""
);
root.browserWrite = browserWrite;
root.writeToPath = writeToPath;
root.logToConsole = defaultWriter;
exports.DEBUG = DEBUG;
exports.INFO = INFO;
exports.LOG = LOG;
exports.WARN = WARN;
exports.ERROR = ERROR;
exports.includeFileAndLine = true;
/* /tmp/sidekick on posix seems fine but windows likes explicit creation*/
if(!os.isPosix()){
ensureLogPathExists();
}
var pp = function(x) {
return inspect(x, { depth: null });
};
function logger(level, prefix, write) {
level = level == null ? LOG : level;
prefix = prefix == null ? "" : prefix;
write = write || defaultWriter();
function log(logLevel, msg) {
if(logLevel >= level) {
var formatted = fileAndLine() + " " + util.format.apply(null, [msg].concat(_.slice(arguments, 2)));
write(logLevel, new Date().toISOString() + " " + prefix + formatted);
}
}
function fileAndLine() {
if(!exports.includeFileAndLine) {
return "";
}
var stack = new Error().stack;
return stack.split("\n")[3]
// replace preamble
.replace(/^\s*at /, "")
// just file please
.replace(/^.*?\/app/, "")
// remove column name
.replace(/:\d+\)?$/, "");
}
var api = log.bind(null, LOG);
api.derive = function(setup) {
setup = _.defaults(setup || {}, {
level: null,
prefix: null,
tags: [],
});
if(typeof setup !== "object") {
throw new Error("use a setup object to derive a new logger");
}
var prefixSuffix = setup.tags.length === 0 ? "" : "[" + setup.tags.join("] [") + "] ";
var newPrefix = (setup.prefix == null ? prefix : setup.prefix) + prefixSuffix;
var newLevel = setup.level == null ? level : setup.level;
return logger(newLevel, newPrefix, setup.write || write);
};
api.trace = function(msg) {
var error = new Error;
api((msg || "") + error.stack.split("\n").slice(3));
};
api.create = function(prefix) {
return api.derive({
tags: [prefix],
});
};
api.setPrefix = function(to) {
prefix = to;
};
api.setWriter = function(to) {
write = to;
};
api.setLevel = function(to) {
level = to;
};
Object.keys(levels).forEach(function(key) {
var level = levels[key];
var fn = api[key] = log.bind(null, level);
fn.pp = function(x) {
fn(pp(x));
};
fn.later = function later(msg) {
return function() {
fn(msg);
};
};
});
api.later = api.log.later;
api.pp = api.log.pp;
return api;
}
function defaultWriter() {
return logToConsole;
}
function browserWrite(path) {
var pathWriter = writeToPath(path);
return function combinedWrite(level, msg) {
pathWriter(level, msg);
// special case - browser console only logs at warn
if(level >= WARN) {
logToConsole(level, msg);
}
}
}
function writeToPath(path) {
var stream;
return function pathWriter(level, msg) {
if(!stream) {
if(process.type === "renderer") {
stream = fs.createWriteStream(path, {
flags: "a+",
});
} else {
var fd = fs.openSync(path, 'a+');
stream = new fs.SyncWriteStream(fd, {
autoClose: false,
});
stream._type = 'fs';
}
}
stream.write(msg + "\n");
}
}
function logToConsole(level, msg) {
switch(level) {
case ERROR:
return console.error(msg);
case WARN:
return console.warn(msg);
default:
return console.log(msg);
}
}
function ensureLogPathExists(){
var logFilePath = os.getTempDir();
var WE_NEED_WRITE_ACCCESS = '0744';
ensureExists(logFilePath, WE_NEED_WRITE_ACCCESS, function(err) {
if (err) {
throw new Error('Unable to access log dir: ' + logFilePath);
}
});
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // allow the `mask` parameter to be optional
cb = mask;
mask = '0777';
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // ignore the error if the folder already exists
else cb(err); // something else went wrong
} else cb(null); // successfully created folder
});
}
}