Skip to content

Commit a0764a3

Browse files
committed
Add --access-log
1 parent 69181e2 commit a0764a3

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Options:
7373
--ssl-key <file> Path to .pem SSL key file
7474
--ssl-cert <file> Path to SSL .pem certificate
7575
--asr <file> Path to configuration for enabling the autoscaler. This is combined with the provider's default configuration (default: none)
76-
76+
--access-log <file> Path where to store access log file (default: none)
77+
7778
Log Levels:
7879
error | debug | info | verbose | debug | silly
7980
`);
@@ -105,6 +106,10 @@ config.logger.maxFileSize = 1024 * 1024 * 100; // Max file size in bytes of each
105106
config.logger.maxFiles = 10; // Max number of log files kept
106107
config.logger.logDirectory = '' // Set this to a full path to a directory - if not set logs will be written to the application directory.
107108

109+
config.accessLog = {};
110+
config.accessLog.maxFileSize = 1024 * 1024 * 100;
111+
config.accessLog.logFile = readConfig("access-log");
112+
108113
for (let k in argv){
109114
if (k === '_' || k.length === 1) continue;
110115
let ck = k.replace(/-/g, "_");

libs/accessLog.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* ClusterODM - A reverse proxy, load balancer and task tracker for NodeODM
3+
* Copyright (C) 2018-present MasseranoLabs LLC
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
"use strict";
19+
20+
let config = require('../config');
21+
let winston = require('winston');
22+
let fs = require('fs');
23+
let path = require('path');
24+
25+
let accessLog = () => {};
26+
27+
if (config.accessLog.logFile){
28+
const logPath = path.dirname(config.accessLog.logFile);
29+
30+
try {
31+
fs.accessSync(logPath, fs.W_OK);
32+
} catch (e) {
33+
console.log( "Access log directory '" + logPath + "' cannot be written to" );
34+
throw e;
35+
}
36+
37+
38+
let logger = winston.createLogger({ transports: [
39+
new winston.transports.File({
40+
format: winston.format.printf(info => `${new Date().toISOString()} ${info.message}`),
41+
filename: config.accessLog.logFile,
42+
json: false,
43+
maxsize: config.accessLog.maxFileSize,
44+
maxFiles: 1,
45+
level: "info"
46+
})
47+
]});
48+
49+
accessLog = (remoteIp, url) => {
50+
logger.info(`[${remoteIp}] ${url}`);
51+
}
52+
}
53+
54+
55+
56+
module.exports = accessLog;

libs/proxy.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const utils = require('./utils');
3030
const routetable = require('./routetable');
3131
const tasktable = require('./tasktable');
3232
const logger = require('./logger');
33+
const accessLog = require('./accessLog');
3334
const statusCodes = require('./statusCodes');
3435
const taskNew = require('./taskNew');
3536
const async = require('async');
@@ -204,12 +205,14 @@ module.exports = {
204205
try{
205206
const urlParts = url.parse(req.url, true);
206207
const { query, pathname } = urlParts;
207-
208+
208209
if (publicPath(pathname)){
209210
forwardToReferenceNode(req, res);
210211
return;
211212
}
212213

214+
accessLog(req.socket.remoteAddress, req.url);
215+
213216
if (req.method === 'POST' && pathname === '/commit'){
214217
const body = await getReqBody(req);
215218
try{

0 commit comments

Comments
 (0)