This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
71 lines (56 loc) · 1.76 KB
/
logger.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
"use strict";
var winston = require('winston');
var express = require("express");
var ua = require("ua-parser");
module.exports = function (app, logger) {
app.use(express.favicon());
app.use(express.cookieParser('secret-cookie-key'));
app.use(express.bodyParser());
app.use(function (req, res, next) {
// create session-cookie for logger
var sid;
if (!req.cookies.loggersid) {
// TODO: Better hashing
sid = Math.floor(Math.random() * Math.random() * 100000000);
res.cookie('loggersid', sid, { maxAge: 30 * 60000 });
} else {
sid = req.cookies.loggersid;
}
req.sid = sid;
next();
});
app.use(express.static(__dirname + '/public'));
app.post('/log', function (req, res) {
var userAgent = ua.parse(req.headers['user-agent']);
var data = {
stack : req.param('stack')
, file : req.param('file')
, line : req.param('line')
, time : req.param('time')
};
var msg = req.param('msg');
var level = req.param('level') || 'info';
var meta = {
browser: {
os : userAgent.os.toString()
, browser : userAgent.family.toString()
, version : userAgent.major.toString() + "." + userAgent.minor.toString()
}
, session: req.sid
};
if (data.stack) {
meta.stack = data.stack;
}
if (data.time) {
meta.time = data.time;
}
if (data.file) {
meta.file = data.file;
}
if (data.line) {
meta.line = data.line;
}
logger.log(level, msg, meta);
res.send();
});
};