rewrite web request logging function

This commit is contained in:
pa
2026-03-10 23:33:15 +09:00
parent 1c9e4621f5
commit 0234abcca3
7 changed files with 83 additions and 39 deletions

View File

@@ -26,4 +26,40 @@ window.$debug = AppDebug;
window.utils = utils;
window.dayjs = dayjs;
/**
* @param {string} tag
* @param {string|unknown[]} target
* @param {...any} rest
*/
export function logWebRequest(tag, target, ...rest) {
if (!AppDebug.debugWebRequests) return;
console.log(tag, target, ...rest);
}
let _queryLogDepth = 0;
/**
* Wraps an async fn so that any API request made inside
* will NOT emit the default [API …] debug log (the query
* layer prints its own log instead).
* @template T
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
*/
export async function withQueryLog(fn) {
_queryLogDepth++;
try {
return await fn();
} finally {
_queryLogDepth--;
}
}
/**
* @returns {boolean} true when inside a withQueryLog callback
*/
export function isApiLogSuppressed() {
return _queryLogDepth > 0;
}
export { AppDebug };

View File

@@ -8,7 +8,7 @@ import {
useUserStore
} from '../stores';
import { getCurrentUser } from '../coordinators/userCoordinator';
import { AppDebug } from './appConfig.js';
import { AppDebug, isApiLogSuppressed, logWebRequest } from './appConfig.js';
import { escapeTag } from '../shared/utils';
import { i18n } from '../plugins/i18n';
import { statusCodes } from '../shared/constants/api.js';
@@ -139,11 +139,17 @@ export function request(endpoint, options) {
throw `API request blocked while logged out: ${endpoint}`;
}
const parsed = parseResponse(response);
if (AppDebug.debugWebRequests) {
if (!isApiLogSuppressed()) {
const tag = `[API ${init.method}]`;
if (!parsed.data) {
console.log(init, 'no data', parsed);
logWebRequest(tag, endpoint, `(${parsed.status}) no data`);
} else {
console.log(init, 'parsed data', parsed.data);
logWebRequest(
tag,
endpoint,
`(${parsed.status})`,
parsed.data
);
}
}
if (parsed.hasApiError) {