From 05f8ef95d944eff155b068c30e26d5556c1b1402 Mon Sep 17 00:00:00 2001 From: ngosang Date: Sun, 30 May 2021 11:28:43 +0200 Subject: [PATCH] Configure timezone with TZ env var. Resolves #109 --- README.md | 3 ++- docker-compose.yml | 1 + src/log.ts | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a821183..4756b81 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,8 @@ Name | Default | Notes |--|--|--| LOG_LEVEL | info | Verbosity of the logging. Use `LOG_LEVEL=debug` for more information. LOG_HTML | false | Only for debugging. If `true` all HTML that passes through the proxy will be logged to the console in `debug` level. -CAPTCHA_SOLVER | none | Captcha solving method. It used when a captcha is encountered. See the Captcha Solvers section. +CAPTCHA_SOLVER | none | Captcha solving method. It is used when a captcha is encountered. See the Captcha Solvers section. +TZ | UTC | Timezone used in the logs and the web browser. Example: `TZ=Europe/London`. HEADLESS | true | Only for debugging. To run the web browser in headless mode or visible. PORT | 8191 | Listening port. You don't need to change this if you are running on Docker. HOST | 0.0.0.0 | Listening interface. You don't need to change this if you are running on Docker. diff --git a/docker-compose.yml b/docker-compose.yml index 3c33784..be0c4af 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - LOG_LEVEL=${LOG_LEVEL:-info} - LOG_HTML=${LOG_HTML:-false} - CAPTCHA_SOLVER=${CAPTCHA_SOLVER:-none} + - TZ=Europe/London ports: - "${PORT:-8191}:8191" restart: unless-stopped diff --git a/src/log.ts b/src/log.ts index 443adad..3b2e11d 100644 --- a/src/log.ts +++ b/src/log.ts @@ -2,6 +2,25 @@ let requests = 0 const LOG_HTML: boolean = process.env.LOG_HTML == 'true'; +function toIsoString(date: Date) { + // this function fixes Date.toISOString() adding timezone + let tzo = -date.getTimezoneOffset(), + dif = tzo >= 0 ? '+' : '-', + pad = function(num: number) { + let norm = Math.floor(Math.abs(num)); + return (norm < 10 ? '0' : '') + norm; + }; + + return date.getFullYear() + + '-' + pad(date.getMonth() + 1) + + '-' + pad(date.getDate()) + + 'T' + pad(date.getHours()) + + ':' + pad(date.getMinutes()) + + ':' + pad(date.getSeconds()) + + dif + pad(tzo / 60) + + ':' + pad(tzo % 60); +} + export default { incRequests: () => { requests++ }, html(html: string) { @@ -12,7 +31,7 @@ export default { {level: process.env.LOG_LEVEL || 'info', prefix(level: string) { const req = (requests > 0) ? ` REQ-${requests}` : ''; - return `${new Date().toISOString()} ${level.toUpperCase()}${req}` + return `${toIsoString(new Date())} ${level.toUpperCase()}${req}` } } )