diff --git a/src/cache.js b/src/cache.js index 3076a23ab432dc93b9615c9f38660900a2b32934..1d64904c780082ae633e5df908a9e73b5b617c72 100644 --- a/src/cache.js +++ b/src/cache.js @@ -1,8 +1,10 @@ +import { logger } from './logger.js' import * as redis from './redis.js' const cache = {} export async function setAsync (key, asyncValue) { + logger.trace(`[Cache] Set async ${key}`) cache[key] = asyncValue const value = await asyncValue await set(key, value) @@ -10,6 +12,7 @@ export async function setAsync (key, asyncValue) { } export function set (key, value) { + logger.trace(`[Cache] Set ${key}`) if (cache[key] === value) return cache[key] = value if (redis.isEnabled()) { @@ -28,11 +31,17 @@ export async function clear () { } export async function get (key, { method = 'get' } = {}) { - if (cache[key]) return cache[key] + if (cache[key]) { + logger.trace(`[Cache] Resolve "${key}" from memory`) + return cache[key] + } if (redis.isEnabled()) { const result = await redis.client[method]?.(key) - cache[key] = result + if (result) { + logger.trace(`[Cache] Resolve "${key}" from redis`) + cache[key] = result + } return result } } diff --git a/src/config.js b/src/config.js index b6e21d97a71bbd395c81da2f5a575772450788bd..0719fa889f808a80fd42c9abb8ceb4ef7a476ca8 100644 --- a/src/config.js +++ b/src/config.js @@ -1,10 +1,12 @@ import fs from 'fs/promises' import yaml from 'js-yaml' +import { logger } from './logger.js' class Config { async load () { const urlsSource = await fs.readFile('./config/config.yaml', 'utf8') this._urls = yaml.load(urlsSource).baseUrls + logger.trace('[Config] Config has been loaded') } get urls () { diff --git a/src/files.js b/src/files.js index dfe10b898c349d5886ba2ecf759bf25cad7be7aa..6f1261ed35a1a9720020bee18c72a475c2557587 100644 --- a/src/files.js +++ b/src/files.js @@ -43,7 +43,7 @@ export async function fetchFileWithHeaders ({ path, version }) { try { return fetchFileWithHeadersFromBaseUrl(path, module.meta.baseUrl, version) } catch (err) { - logger.debug(`File ${path} had a baseUrl but could not be found on that server: ${err}`) + logger.debug(`[Files] File ${path} had a baseUrl but could not be found on that server: ${err}`) } } diff --git a/src/index.js b/src/index.js index 6cbdee3bc426a6f8becc06c2cb6d64830f398f9a..796ef767e10befaf5befb46b7ea35d4b3f976e9f 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ createQueues() // Binds and listens for connections on the specified host and port root.listen(process.env.PORT, () => { - logger.info(`ui-middleware listening on port ${process.env.PORT}`) + logger.info(`[Server] ui-middleware listening on port ${process.env.PORT}`) }) process.on('uncaughtException', err => { diff --git a/src/manifests.js b/src/manifests.js index 9cb1bdd1d4e4a4855f5b6a241b85e8c4f5b1a4dc..94bd2dd406f1ed617acf055b95ef4cff779c2522 100644 --- a/src/manifests.js +++ b/src/manifests.js @@ -14,7 +14,7 @@ export async function fetchViteManifests () { try { const manifest = await result.json() for (const file in manifest) { - logger.debug(`retrieved ${file} from ${baseUrl}`) + logger.debug(`[Manifest] Retrieved ${file} from ${baseUrl}`) manifest[file].meta = manifest[file].meta || {} manifest[file].meta.baseUrl = baseUrl } diff --git a/src/redis.js b/src/redis.js index f0c513fa891034ca373f3c4d9c50fa472aada68a..0c1656761ba4125f440d5bd62f69b3b3fdafc109 100644 --- a/src/redis.js +++ b/src/redis.js @@ -20,8 +20,8 @@ const createClient = (type, options = {}) => { password: process.env.REDIS_PASSWORD, ...options }) - client.on('ready', () => logger.info(`Connected ${type} to redis on ${process.env.REDIS_HOST}`)) - client.on('error', (err) => logger.error(`Redis connect error: ${err}`)) + client.on('ready', () => logger.info(`[Redis] Connected ${type} to redis on ${process.env.REDIS_HOST}`)) + client.on('error', (err) => logger.error(`[Redis] Connect error: ${err}`)) return client } diff --git a/src/version.js b/src/version.js index 0496d32d08a3bc4d95880e44eb0dc1d6166298c3..45c39916ee92ec1baa4c363e3a1db4cab47e7816 100644 --- a/src/version.js +++ b/src/version.js @@ -16,7 +16,7 @@ export const fetchLatestVersion = async () => { if (!version) throw new Error() return version } catch (err) { - logger.debug(`UI container at ${baseUrl} does not have meta.json. Fall back to version hash based on manifest.`) + logger.debug(`[Version] UI container at ${baseUrl} does not have meta.json. Fall back to version hash based on manifest.`) } try { const response = await fetch(new URL('manifest.json', baseUrl)) @@ -24,7 +24,7 @@ export const fetchLatestVersion = async () => { const manifest = await response.json() return hash(manifest) } catch (err) { - logger.error(`Cannot fetch manifest from ${baseUrl}. Version info will not be correct.`) + logger.error(`[Version] Cannot fetch manifest from ${baseUrl}. Version info will not be correct.`) } })) return hash(infos) @@ -51,7 +51,7 @@ export async function getLatestVersion () { export function registerLatestVersionListener (client) { if (redis.isEnabled()) { const key = getRedisKey({ name: 'updateLatestVersion' }) - client.subscribe(key, (errs, count) => logger.info(`Subscribed to ${key}.`)) + client.subscribe(key, (errs, count) => logger.info(`[Redis] Subscribed to ${key}.`)) client.on('message', async (channel, message) => { if (channel !== key) return await config.load()