-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathlogging.js
More file actions
39 lines (33 loc) · 1.17 KB
/
Copy pathlogging.js
File metadata and controls
39 lines (33 loc) · 1.17 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
import moment from 'moment';
import CONFIG from './config.loader.js';
// Host-injectable logging sink. When iframely is used as a library
// inside another application, the host may call setLogger(l) with a
// logger object ({info, warn, error}) to route every iframely log
// line into its own logging pipeline. Standalone installs never call
// setLogger and keep the classic prefixed console output below.
let sink = null;
export function setLogger(l) {
sink = l;
}
export default function log() {
var args = Array.prototype.slice.apply(arguments);
// Add ip if request provided.
var request = args[0];
if (request && request.headers) {
args.shift();
var remote_addr = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
if (remote_addr) {
args.splice(0, 0, remote_addr, '-');
}
}
if (sink) {
sink.info.apply(sink, args);
return;
}
if (CONFIG.LOG_DATE_FORMAT) {
args.splice(0, 0, "--", moment().utc().format(CONFIG.LOG_DATE_FORMAT) + process.pid);
} else {
args.splice(0, 0, "--", "pid:" + process.pid);
}
console.log.apply(console, args);
};