convokit
Version:
A flexible TypeScript framework for ingesting, processing, and exporting chat/conversation data for LLM training and analysis.
78 lines • 2.97 kB
JavaScript
import { loadConfig } from './ConvoKitConfig.js';
// Load config once at the start
let configLoaded = false;
let enableDebugging = false;
let enablePerformanceStats = true;
let enableWarnings = true;
async function ensureConfigLoaded() {
if (!configLoaded) {
try {
const config = await loadConfig();
enableDebugging = config.enableDebugging ?? false;
enablePerformanceStats = config.enablePerformanceStats ?? true;
enableWarnings = config.enableWarnings ?? true;
configLoaded = true;
}
catch (err) {
// Log error during initial load attempt, but allow logging functions to work
console.error(`[ERROR] ${new Date().toISOString()} - ConvoKitConfig: Failed to load config initially: ${err.message}`);
// Use default logging settings if config fails
}
}
}
const timers = new Map();
async function log(level, where, ...args) {
await ensureConfigLoaded(); // Ensure config is loaded before logging
switch (level) {
case 'INFO':
case 'SUCCESS': // Treat SUCCESS like INFO for now, maybe add color later
console.log(`[${level}] ${new Date().toISOString()} - ${where}:`, ...args);
break;
case 'ERROR':
console.error(`[ERROR] ${new Date().toISOString()} - ${where}:`, ...args);
break;
case 'DEBUG':
if (enableDebugging) {
console.debug(`[DEBUG] ${new Date().toISOString()} - ${where}:`, ...args);
}
break;
case 'WARN':
if (enableWarnings) {
console.warn(`[WARN] ${new Date().toISOString()} - ${where}:`, ...args);
}
break;
}
}
async function time(where, timerName) {
await ensureConfigLoaded();
if (!enablePerformanceStats)
return;
const key = `${where}:${timerName}`;
timers.set(key, Date.now());
await log('INFO', where, `--------- Started timer: ${timerName} ---------`);
}
async function timeEnd(where, timerName) {
await ensureConfigLoaded();
if (!enablePerformanceStats)
return;
const key = `${where}:${timerName}`;
const startTime = timers.get(key);
if (startTime === undefined) {
await log('ERROR', where, `No such timer: ${timerName}`);
return;
}
const elapsed = Date.now() - startTime;
timers.delete(key);
await log('INFO', where, `--------- ${timerName} took ${elapsed}ms --------- `);
}
// Exported logging functions
export const ConvoKitLogging = {
info: (where, ...args) => log('INFO', where, ...args),
error: (where, ...args) => log('ERROR', where, ...args),
debug: (where, ...args) => log('DEBUG', where, ...args),
warn: (where, ...args) => log('WARN', where, ...args),
success: (where, ...args) => log('SUCCESS', where, ...args),
time: time,
timeEnd: timeEnd,
};
//# sourceMappingURL=ConvoKitLogging.js.map