From c6f9d78ce916a20fa6f67949a22c5cdc0009564c Mon Sep 17 00:00:00 2001 From: Pierre Demailly Date: Sat, 30 Nov 2024 22:42:54 +0100 Subject: [PATCH] chore: improve logs --- public/core/search-nav.js | 1 - public/main.js | 2 +- src/http-server/cache.js | 10 +++++----- src/http-server/config.js | 19 +++++++++++++------ src/http-server/endpoints/data.js | 10 +++++----- src/http-server/endpoints/search.js | 10 +++++----- src/http-server/index.js | 2 +- src/http-server/logger.js | 6 ++++-- src/http-server/websocket/init.js | 4 ++-- src/http-server/websocket/remove.js | 10 +++++----- src/http-server/websocket/search.js | 12 ++++++------ 11 files changed, 47 insertions(+), 39 deletions(-) diff --git a/public/core/search-nav.js b/public/core/search-nav.js index e0dc822a..3793642c 100644 --- a/public/core/search-nav.js +++ b/public/core/search-nav.js @@ -82,7 +82,6 @@ function addRemoveButton(pkgElement, options) { removeButton.textContent = "x"; removeButton.addEventListener("click", (event) => { event.stopPropagation(); - // we remove the x button from textContent const pkgToRemove = pkgElement.dataset.name; window.socket.send(JSON.stringify({ action: "REMOVE", pkg: pkgToRemove })); diff --git a/public/main.js b/public/main.js index f60c1c9d..858ec044 100644 --- a/public/main.js +++ b/public/main.js @@ -149,7 +149,7 @@ async function init(options = { navigateToNetworkView: false }) { const searchNavElement = document.getElementById("search-nav"); const pkgs = searchNavElement.querySelectorAll(".package"); for (const pkg of pkgs) { - if (pkg.textContent.startsWith(window.activePackage)) { + if (pkg.dataset.name.startsWith(window.activePackage)) { pkg.classList.add("active"); } else { diff --git a/src/http-server/cache.js b/src/http-server/cache.js index ea0316b3..c2b9a753 100644 --- a/src/http-server/cache.js +++ b/src/http-server/cache.js @@ -42,7 +42,7 @@ class _AppCache { return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), "utf-8")); } catch (err) { - logger.error(`[CACHE | GET_PAYLOAD](pkg: ${pkg}|cache: not found)`); + logger.error(`[cache|get](pkg: ${pkg}|cache: not found)`); throw err; } @@ -68,7 +68,7 @@ class _AppCache { return JSON.parse(data.toString()); } catch (err) { - logger.error(`[CACHE | PAYLOADS_LIST](cache: not found)`); + logger.error(`[cache|get](cache: not found)`); throw err; } @@ -87,8 +87,8 @@ class _AppCache { }, root: formatted }; - // eslint-disable-next-line @stylistic/max-len - logger.info(`[CACHE | INIT_PAYLOADS_LIST](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); + + logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify(payloadsList)); this.updatePayload(formatted.replaceAll("/", "-"), payload); } @@ -102,7 +102,7 @@ class _AppCache { } const list = packagesInFolder.map(({ name }) => name); - logger.info(`[CACHE | INIT_PAYLOADS_LIST](list: ${list})`); + logger.info(`[cache|init](list: ${list})`); await cacache.put(CACHE_PATH, kPayloadsCache, JSON.stringify({ list, current: list[0] })); } diff --git a/src/http-server/config.js b/src/http-server/config.js index d71a4fed..e194efb1 100644 --- a/src/http-server/config.js +++ b/src/http-server/config.js @@ -12,30 +12,37 @@ export async function get() { try { const config = await appCache.getConfig(); - logger.info(`[CONFIG | GET](config: ${config})`); + const { + defaultPackageMenu, + ignore: { + flags, + warnings + } + } = config; + logger.info(`[config|get](defaultPackageMenu: ${defaultPackageMenu}|ignore-flag: ${flags}|ignore-warnings: ${warnings})`); return config; } catch (err) { - logger.error(`[CONFIG | GET](error: ${err.message})`); + logger.error(`[config|get](error: ${err.message})`); await appCache.updateConfig(kDefaultConfig); - logger.info(`[CONFIG | GET](fallback to default: ${JSON.stringify(kDefaultConfig)})`); + logger.info(`[config|get](fallback to default: ${JSON.stringify(kDefaultConfig)})`); return kDefaultConfig; } } export async function set(newValue) { - logger.info(`[CONFIG | SET](config: ${newValue})`); + logger.info(`[config|set](config: ${newValue})`); try { await appCache.updateConfig(newValue); - logger.info(`[CONFIG | SET](sucess)`); + logger.info(`[config|set](sucess)`); } catch (err) { - logger.error(`[CONFIG | SET](error: ${err.message})`); + logger.error(`[config|set](error: ${err.message})`); throw err; } diff --git a/src/http-server/endpoints/data.js b/src/http-server/endpoints/data.js index 06fb8f55..02194a86 100644 --- a/src/http-server/endpoints/data.js +++ b/src/http-server/endpoints/data.js @@ -15,14 +15,14 @@ const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json"); export async function get(req, res) { try { const { current, lru } = await appCache.payloadsList(); - logger.info(`[DATA | GET](current: ${current})`); - logger.debug(`[DATA | GET](lru: ${lru})`); + logger.info(`[data|get](current: ${current})`); + logger.debug(`[data|get](lru: ${lru})`); const formatted = current.replaceAll("/", "-"); send(res, 200, await appCache.getPayload(formatted)); } catch { - logger.error(`[DATA | GET](No cache yet. Creating one...)`); + logger.error(`[data|get](No cache yet. Creating one...)`); const payload = JSON.parse(fs.readFileSync(kDefaultPayloadPath, "utf-8")); const version = Object.keys(payload.dependencies[payload.rootDependencyName].versions)[0]; @@ -36,11 +36,11 @@ export async function get(req, res) { }, root: formatted }; - logger.info(`[DATA | GET](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); + logger.info(`[data|get](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`); await appCache.updatePayloadsList(payloadsList); appCache.updatePayload(formatted.replaceAll("/", "-"), payload); - logger.info(`[DATA | GET](cache: created|payloadsList: ${payloadsList.lru})`); + logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`); send(res, 200, payload); } diff --git a/src/http-server/endpoints/search.js b/src/http-server/endpoints/search.js index bf0d95f5..2f221981 100644 --- a/src/http-server/endpoints/search.js +++ b/src/http-server/endpoints/search.js @@ -7,12 +7,12 @@ import { logger } from "../logger.js"; export async function get(req, res) { const { packageName } = req.params; - logger.info(`[SEARCH: GET](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`); + logger.info(`[search|get](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`); const { objects, total } = await npm.search({ text: decodeURIComponent(packageName) }); - logger.debug(`[SEARCH: GET](npmSearchResult: ${JSON.stringify(objects.map((pkg) => pkg.package.name))})`); + logger.debug(`[search|get](npmSearchResult: ${JSON.stringify(objects.map((pkg) => pkg.package.name))})`); send(res, 200, { count: total, @@ -29,13 +29,13 @@ export async function get(req, res) { export async function versions(req, res) { const { packageName } = req.params; - logger.info(`[SEARCH: VERSIONS](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`); + logger.info(`[search|versions](packageName: ${packageName}|formatted: ${decodeURIComponent(packageName)})`); const packument = await npm.packument(decodeURIComponent(packageName)); const versions = Object.keys(packument.versions); - logger.info(`[SEARCH: VERSIONS](packageName: ${packageName}|versions: ${versions})`); - logger.debug(`[SEARCH: VERSIONS](packument: ${packument})`); + logger.info(`[search|versions](packageName: ${packageName}|versions: ${versions})`); + logger.debug(`[search|versions](packument: ${packument})`); send(res, 200, versions); } diff --git a/src/http-server/index.js b/src/http-server/index.js index ebf0b865..bab1b82d 100644 --- a/src/http-server/index.js +++ b/src/http-server/index.js @@ -67,7 +67,7 @@ export function buildServer(dataFilePath, options = {}) { websocket.on("connection", async(socket) => { socket.on("message", async(rawMessage) => { const message = JSON.parse(rawMessage); - logger.info(`[WEBSOCKET](message: ${JSON.stringify(message)})`); + logger.info(`[ws](message: ${JSON.stringify(message)})`); if (message.action === "SEARCH") { wsHandlers.search(socket, message.pkg); diff --git a/src/http-server/logger.js b/src/http-server/logger.js index a6f58926..e313792e 100644 --- a/src/http-server/logger.js +++ b/src/http-server/logger.js @@ -1,9 +1,11 @@ // Import Third-party Dependencies import pino from "pino"; +// CONSTANTS +const kDefaultLogLevel = "info"; + const logger = pino({ - // TODO: info - level: "debug", + level: process.env.LOG_LEVEL ?? kDefaultLogLevel, transport: { target: "pino-pretty" } diff --git a/src/http-server/websocket/init.js b/src/http-server/websocket/init.js index 1b03068d..fe3a84ac 100644 --- a/src/http-server/websocket/init.js +++ b/src/http-server/websocket/init.js @@ -5,7 +5,7 @@ import { logger } from "../logger.js"; export async function init(socket, lock = false) { try { const { current, lru, older, root } = await appCache.payloadsList(); - logger.info(`[WEBSOCKET | INIT](lru: ${lru}|older: ${older}|current: ${current}|root: ${root})`); + logger.info(`[ws|init](lru: ${lru}|older: ${older}|current: ${current}|root: ${root})`); if (lru === void 0 || current === void 0) { throw new Error("Payloads list not found in cache."); @@ -20,7 +20,7 @@ export async function init(socket, lock = false) { })); } catch { - logger.error(`[WEBSOCKET | INIT](No cache yet. Creating one...)`); + logger.error(`[ws|init](No cache yet. Creating one...)`); if (lock) { return; diff --git a/src/http-server/websocket/remove.js b/src/http-server/websocket/remove.js index 929f0e01..005529fe 100644 --- a/src/http-server/websocket/remove.js +++ b/src/http-server/websocket/remove.js @@ -4,11 +4,11 @@ import { logger } from "../logger.js"; export async function remove(ws, pkg) { const formattedPkg = pkg.replace("/", "-"); - logger.info(`[WEBSOCKET | REMOVE](pkg: ${pkg}|formatted: ${formattedPkg})`); + logger.info(`[ws|remove](pkg: ${pkg}|formatted: ${formattedPkg})`); try { const { lru, older, current, lastUsed, root } = await appCache.payloadsList(); - logger.debug(`[WEBSOCKET | REMOVE](lru: ${lru}|current: ${current})`); + logger.debug(`[ws|remove](lru: ${lru}|current: ${current})`); if (lru.length === 1 && older.length === 0) { throw new Error("Cannot remove the last package."); @@ -22,7 +22,7 @@ export async function remove(ws, pkg) { } if (lruIndex > -1) { - logger.info(`[WEBSOCKET | REMOVE](remove from lru)`); + logger.info(`[ws|remove](remove from lru)`); const updatedLru = lru.filter((pkgName) => pkgName !== pkg); if (older.length > 0) { // We need to move the first older package to the lru list @@ -54,7 +54,7 @@ export async function remove(ws, pkg) { })); } else { - logger.info(`[WEBSOCKET | REMOVE](remove from older)`); + logger.info(`[ws|remove](remove from older)`); const updatedOlder = older.filter((pkgName) => pkgName !== pkg); const updatedList = { lru, @@ -77,7 +77,7 @@ export async function remove(ws, pkg) { appCache.removePayload(formattedPkg.replaceAll("/", "-")); } catch (error) { - logger.error(`[WEBSOCKET | REMOVE](error: ${error.message})`); + logger.error(`[ws|remove](error: ${error.message})`); logger.debug(error); throw error; diff --git a/src/http-server/websocket/search.js b/src/http-server/websocket/search.js index 9a825e77..8837b045 100644 --- a/src/http-server/websocket/search.js +++ b/src/http-server/websocket/search.js @@ -6,14 +6,14 @@ import { logger } from "../logger.js"; import { appCache } from "../cache.js"; export async function search(ws, pkg) { - logger.info(`[WEBSOCKET | SEARCH](pkg: ${pkg})`); + logger.info(`[ws|search](pkg: ${pkg})`); const cache = await appCache.getPayloadOrNull(pkg); if (cache) { - logger.info(`[WEBSOCKET | SEARCH](payload: ${pkg} found in cache)`); + logger.info(`[ws|search](payload: ${pkg} found in cache)`); const cacheList = await appCache.payloadsList(); if (cacheList.lru.includes(pkg)) { - logger.info(`[WEBSOCKET | SEARCH](payload: ${pkg} is already in the LRU)`); + logger.info(`[ws|search](payload: ${pkg} is already in the LRU)`); const updatedList = { ...cacheList, current: pkg, @@ -44,7 +44,7 @@ export async function search(ws, pkg) { } // at this point we don't have the payload in cache so we have to scan it. - logger.info(`[WEBSOCKET | SEARCH](scan ${pkg})`); + logger.info(`[ws|search](scan ${pkg} in progress)`); ws.send(JSON.stringify({ status: "SCAN", pkg })); const payload = await Scanner.from(pkg, { maxDepth: 4 }); @@ -54,7 +54,7 @@ export async function search(ws, pkg) { { // save the payload in cache const pkg = `${name}@${version}`; - logger.info(`[WEBSOCKET | SEARCH](scan <${pkg}> done|cache: updated|pkg: ${pkg})`); + logger.info(`[ws|search](scan ${pkg} done|cache: updated)`); // update the payloads list const { lru, older, lastUsed, root } = await appCache.removeLastLRU(); @@ -75,6 +75,6 @@ export async function search(ws, pkg) { ...updatedList })); - logger.info(`[WEBSOCKET | SEARCH](payloadsList updated|payload sent to client)`); + logger.info(`[ws|search](data sent to client|cache: updated)`); } }