netanalyzer/utils/logger.js
Joseph McQueen 0a46d25bd6 chore: project cleanup — dead code, logging, tests, WS hardening
- Delete unused dataMerger, formatter, Device model, dead service exports,
  and the empty agents/ + .gitkeep placeholders.
- Extract STORE_MODES and MDM device-type filters into a shared constants.js.
- Anchor bot regexes (^help|^store|^analyze) so "analyze store 305" no
  longer fires both handlers; replace catch-all noise.
- Hoist inline require() calls in integrations to top-of-file imports.
- Harden WebSocket server: Authorization header support, single-agent
  enforcement, bounded pending requests, server-level error handler,
  coalesced cache refresh in Meraki client.
- Wrap Meraki/MDM network calls with withRetry; add request timeouts.
- Migrate all console.* calls onto utils/logger.js (LOG_LEVEL aware);
  drive Webex framework logLevel from env.
- Refactor storeDetail.js: shared renderClientLine + buildMdmSection
  helpers cut duplication roughly in half.
- Refresh README structure, document LOG_LEVEL, add npm run agent script,
  add jest testMatch + new tests (handlers, HealthReport, Store, ws).

Verified: npm run lint clean, 7 suites / 31 tests passing.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 17:21:22 -04:00

45 lines
1.1 KiB
JavaScript

/**
* Lightweight structured logger.
*
* Honors LOG_LEVEL (debug | info | warn | error). Defaults to 'info'.
* Output is single-line JSON for easy ingestion by log shippers.
*
* Note: requires config-free defaults so it can be imported anywhere without
* pulling in config/index.js (which validates env on load).
*/
const LEVELS = { debug: 10, info: 20, warn: 30, error: 40 };
function configuredLevel() {
const raw = (process.env.LOG_LEVEL || 'info').toLowerCase();
return LEVELS[raw] ?? LEVELS.info;
}
function shouldEmit(level) {
return LEVELS[level] >= configuredLevel();
}
function log(level, message, meta = {}) {
if (!shouldEmit(level)) return;
const entry = {
ts: new Date().toISOString(),
level,
msg: message,
...meta,
};
const line = JSON.stringify(entry);
if (level === 'error') console.error(line);
else if (level === 'warn') console.warn(line);
else console.log(line);
}
const logger = {
debug: (msg, meta) => log('debug', msg, meta),
info: (msg, meta) => log('info', msg, meta),
warn: (msg, meta) => log('warn', msg, meta),
error: (msg, meta) => log('error', msg, meta),
};
module.exports = logger;