Skip to content

Commit 8177807

Browse files
committed
feat: add option for log level
1 parent 3d3ddf0 commit 8177807

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ The Docker images are available for multiple architectures, including armv7.
5050

5151
The following can be configured as environment variables:
5252

53+
- **ACCESS_LOGS** if access logs are showed (default `false`).
5354
- **DEFAULT_INDEX_FILE** name of file served on paths ending with a trailing
5455
slash (default `index.html`).
5556
- **FALLBACK_FILE** name of file served as a fallback if requested file is not
5657
found. Set to `index.html` to enable client-side routing or to e.g.
5758
`error.html` to display an error page (note that response still returns a 200
5859
status code). It is undefined by default and no fallback file will be served.
60+
- **LOG_LEVEL** used log level. Available values are `debug`, `info`, `warning`
61+
and `error` (default `info`).
5962
- **PORT** the port the server should listen on (default 8080).
6063
- **ROOT_FILE_PATH** root directory of served file tree (default `build`).
6164

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const path = require("node:path");
44

55
const mime = require("mime");
66

7+
const logger = require("./logger");
8+
79
const DEFAULT_INDEX_FILE = process.env.DEFAULT_INDEX_FILE || "index.html";
810
const FALLBACK_FILE = process.env.FALLBACK_FILE || null;
911
const PORT = process.env.PORT || 8080;
@@ -14,7 +16,7 @@ function getFile(filePath) {
1416
const file = fs.readFileSync(filePath);
1517
return file;
1618
} catch {
17-
console.error(`Could not find file "${filePath}"`);
19+
logger.error(`Could not find file "${filePath}"`);
1820
return;
1921
}
2022
}
@@ -44,20 +46,21 @@ function createFilePath(domain, filePath) {
4446
function handleRequest(req, res) {
4547
const domain = getDomainFromRequest(req);
4648
const filePath = getFilePathFromRequest(req);
47-
console.debug("Requesting", domain, filePath);
49+
logger.debug(`Requesting ${domain}${filePath}`);
4850

4951
const actualFilePath = createFilePath(domain, filePath);
5052
let file = getFile(actualFilePath);
5153

5254
if (!file && FALLBACK_FILE) {
5355
const fallbackFilePath = createFilePath(domain, FALLBACK_FILE);
54-
console.debug("Serving fallback file:", fallbackFilePath);
56+
logger.debug(`Serving fallback file: ${fallbackFilePath}`);
5557
file = getFile(fallbackFilePath);
5658
}
5759

5860
if (!file) {
5961
res.writeHead(404);
6062
res.end();
63+
logger.accessLog(req, res);
6164
return;
6265
}
6366

@@ -66,9 +69,10 @@ function handleRequest(req, res) {
6669
res.setHeader("Content-Type", mimeType);
6770
res.writeHead(200);
6871
res.end(file);
72+
logger.accessLog(req, res);
6973
}
7074

7175
const server = http.createServer(handleRequest);
7276
server.listen(PORT);
7377

74-
console.info(`Listening on port ${PORT}`);
78+
logger.info(`Listening on port ${PORT}`);

src/logger.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const ACCESS_LOGS = process.env.ACCESS_LOGS || false;
2+
const LOG_LEVEL = process.env.LOG_LEVEL || "info";
3+
4+
const LOG_LEVELS = {
5+
DEBUG: 1,
6+
INFO: 2,
7+
WARNING: 3,
8+
ERROR: 4,
9+
};
10+
11+
const LEVEL = LOG_LEVELS[LOG_LEVEL.toUpperCase()];
12+
13+
exports.debug = function (message) {
14+
if (LEVEL <= LOG_LEVELS.DEBUG) {
15+
console.debug(`DEBUG: ${message}`);
16+
}
17+
};
18+
19+
exports.info = function (message) {
20+
if (LEVEL <= LOG_LEVELS.INFO) {
21+
console.info(`INFO: ${message}`);
22+
}
23+
};
24+
25+
exports.warn = function (message) {
26+
if (LEVEL <= LOG_LEVELS.WARNING) {
27+
console.warn(`WARNING: ${message}`);
28+
}
29+
};
30+
31+
exports.error = function (message) {
32+
if (LEVEL <= LOG_LEVELS.ERROR) {
33+
console.error(`ERROR: ${message}`);
34+
}
35+
};
36+
37+
exports.accessLog = function (req, res) {
38+
if (ACCESS_LOGS) {
39+
console.info(`${req.headers.host}${req.url} ${res.statusCode}`);
40+
}
41+
};

0 commit comments

Comments
 (0)