From 2ce15fe7dcb4b0db5a69b7a1a8f254fae7d7b885 Mon Sep 17 00:00:00 2001 From: Glenn de Haan Date: Wed, 17 Apr 2024 17:19:00 +0200 Subject: [PATCH] Implemented LOG_LEVEL environment variable. Implemented log_level HA variable. Implemented log level based on user preference. Changed UniFi stack trace output from error to debug. Removed duplicate cache error log. Implemented internal build number. Updated README.md. Added account warning to README.md. Output build version on application start --- Dockerfile | 5 +++++ README.md | 4 ++++ docker-compose.yml | 1 + modules/log.js | 31 ++++++++++++++++++++++++++++--- modules/unifi.js | 10 +++++----- package.json | 4 ++-- server.js | 12 +++++++++--- utils/cache.js | 3 +-- 8 files changed, 55 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index c7b2b88..d5b9ad7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -110,3 +110,8 @@ CMD ["dumb-init", "node", "/app/server.js"] COPY --from=dependencies /app/node_modules ./node_modules COPY --from=css /app/public/dist ./public/dist COPY . . + +# +# Set build +# +RUN echo -n `date '+%Y.%m.%d.%H.%M'` > /etc/unifi_voucher_site_build diff --git a/README.md b/README.md index 20ab289..deef8ca 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,12 @@ services: SERVICE_WEB: 'true' # Enable/disable the API SERVICE_API: 'false' + # Sets the application Log Level (Valid Options: error|warn|info|debug|trace) + LOG_LEVEL: 'info' ``` +> Attention!: We recommend only using Local UniFi accounts due to short token lengths provided by UniFi Cloud Accounts. Also UniFi Cloud Accounts using 2FA won't work! + ## Services The project consists of two main services: Web and API. diff --git a/docker-compose.yml b/docker-compose.yml index 820885e..234cc74 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,3 +16,4 @@ services: VOUCHER_CUSTOM: 'true' SERVICE_WEB: 'true' SERVICE_API: 'false' + LOG_LEVEL: 'info' diff --git a/modules/log.js b/modules/log.js index b0ec485..dd67be0 100644 --- a/modules/log.js +++ b/modules/log.js @@ -4,9 +4,14 @@ const log = require('js-logger'); /** - * Check if we are using the dev version + * Import own modules */ -const dev = process.env.NODE_ENV !== 'production'; +const config = require('./config'); + +/** + * Define global variables + */ +const level = config('log_level') || process.env.LOG_LEVEL || "info"; /** * Setup logger @@ -24,6 +29,26 @@ const consoleLogger = log.createDefaultHandler({ } }); +/** + * Log Level converter + */ +const logConvert = (level) => { + switch(level) { + case "error": + return log.ERROR; + case "warn": + return log.WARN; + case "info": + return log.INFO; + case "debug": + return log.DEBUG; + case "trace": + return log.TRACE; + default: + return log.INFO; + } +} + /** * Set all logger handlers */ @@ -34,7 +59,7 @@ log.setHandler((messages, context) => { /** * Set log level */ -log.setLevel(dev ? log.TRACE : log.INFO); +log.setLevel(logConvert(level)); /** * Export the application logger diff --git a/modules/unifi.js b/modules/unifi.js index 0997805..225172d 100644 --- a/modules/unifi.js +++ b/modules/unifi.js @@ -54,7 +54,7 @@ const startSession = () => { // Something went wrong so clear the current controller so a user can retry controller = null; log.error('[UniFi] Error while logging in!'); - log.error(e); + log.debug(e); reject('[UniFi] Error while logging in!'); }); }); @@ -88,13 +88,13 @@ const unifiModule = { resolve(voucher); }).catch((e) => { log.error('[UniFi] Error while getting voucher!'); - log.error(e); + log.debug(e); reject('[UniFi] Error while getting voucher!'); }); } }).catch((e) => { log.error('[UniFi] Error while creating voucher!'); - log.error(e); + log.debug(e); // Check if token expired, if true attempt login then try again if (e.response) { @@ -140,7 +140,7 @@ const unifiModule = { resolve(true); }).catch((e) => { log.error('[UniFi] Error while removing voucher!'); - log.error(e); + log.debug(e); // Check if token expired, if true attempt login then try again if (e.response) { @@ -186,7 +186,7 @@ const unifiModule = { resolve(vouchers); }).catch((e) => { log.error('[UniFi] Error while getting vouchers!'); - log.error(e); + log.debug(e); // Check if token expired, if true attempt login then try again if (e.response) { diff --git a/package.json b/package.json index de035e4..ddb6091 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "NPM packages for unifi-voucher-site", "private": true, "scripts": { - "start": "node server.js", - "dev": "nodemon --watch . --exec 'node server.js'", + "start": "LOG_LEVEL=trace node server.js", + "dev": "nodemon --watch . --exec 'LOG_LEVEL=trace node server.js'", "tailwind": "tailwindcss -i ./css/style.css -o ./public/dist/style.css --watch", "build": "tailwindcss -i ./css/style.css -o ./public/dist/style.css --minify" }, diff --git a/server.js b/server.js index c178fbb..57088b6 100644 --- a/server.js +++ b/server.js @@ -51,6 +51,15 @@ const authDisabled = (process.env.DISABLE_AUTH === 'true') || false; */ logo(); +/** + * Output build version + */ +if(fs.existsSync('/etc/unifi_voucher_site_build')) { + log.info(`[Version] ${fs.readFileSync('/etc/unifi_voucher_site_build', 'utf-8')}`); +} else { + log.info(`[Version] **DEVELOPMENT**`); +} + /** * Log external config */ @@ -211,7 +220,6 @@ if(webService) { const vouchers = await unifi.list().catch((e) => { log.error('[Cache] Error requesting vouchers!'); - log.error(e); res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); }); @@ -235,7 +243,6 @@ if(webService) { const vouchers = await unifi.list().catch((e) => { log.error('[Cache] Error requesting vouchers!'); - log.error(e); res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); }); @@ -366,7 +373,6 @@ if(webService) { const vouchers = await unifi.list().catch((e) => { log.error('[Cache] Error requesting vouchers!'); - log.error(e); res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); }); diff --git a/utils/cache.js b/utils/cache.js index 20107da..3eb2c36 100644 --- a/utils/cache.js +++ b/utils/cache.js @@ -20,9 +20,8 @@ module.exports = { return new Promise(async (resolve) => { log.info('[Cache] Requesting UniFi Vouchers...'); - const vouchers = await unifi.list().catch((e) => { + const vouchers = await unifi.list().catch(() => { log.error('[Cache] Error requesting vouchers!'); - log.error(e); }); if(vouchers) {