Skip to content

Commit

Permalink
Implemented LOG_LEVEL environment variable. Implemented log_level HA …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
glenndehaan committed Apr 17, 2024
1 parent 5867bc4 commit 2ce15fe
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ services:
VOUCHER_CUSTOM: 'true'
SERVICE_WEB: 'true'
SERVICE_API: 'false'
LOG_LEVEL: 'info'
31 changes: 28 additions & 3 deletions modules/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions modules/unifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
12 changes: 9 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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`);
});

Expand All @@ -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`);
});

Expand Down Expand Up @@ -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`);
});

Expand Down
3 changes: 1 addition & 2 deletions utils/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2ce15fe

Please sign in to comment.