Multi-integration Webex chat/HTTP bot that unifies phone, AV, and network status for retail store support. Consolidates data from Webex Calling, Meraki, Workspace ONE (MDM), Atlas AMP, RED digital signage, and OptiSigns into rich per-store status commands. Key surfaces: - /phonestatus, /avstatus — per-store phone & AV device reports with clickable Meraki deep-links and per-port detail. - /webexhost — check/assign Webex Meetings host licenses via the Service App; adaptive-card confirmation flow, HTTP-API-gated. - /offboarduser — full Webex Admin offboarding (auth revoke, device wipe, license removal); adaptive-card confirmation. - /jirapoll — on-demand trigger for the hourly Jira poller. - /bulkavstatuscsv — bulk store CSV export with concurrency limits. Automation: - Hourly Jira poller (node-cron) with an X.AI (Grok) ticket classifier that categorizes unassigned tickets as phone/av/skip, extracts store numbers from free-text, and enriches Jira with the same detailed markdown the chat commands emit (converted to Jira ADF, preserves bold + Meraki links). Idempotent via a `bot-enriched` Jira label. Architecture: - Node.js 20+, ESM, Express 5, webex-node-bot-framework. - Layered integrations (integrations/*), services (services/*), commands (commands/*), utils (utils/*). - Shared markdown renderers (services/renderers/*) feed both chat handlers and the Jira poller so the two surfaces stay in sync. - Hand-rolled markdown-to-ADF converter (utils/markdownToAdf.js) — no new npm dependency. - Node built-in test runner (`node --test tests/*.test.js`), 30 tests covering the converter, renderers, and poller ADF assembly. Docker + docker-compose deployment. Config via .env (see .env.example for the full option surface).
127 lines
No EOL
3.6 KiB
JavaScript
127 lines
No EOL
3.6 KiB
JavaScript
// utils/logger.js
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const projectRoot = path.resolve(__dirname, '..');
|
|
const LOG_DIR = path.join(projectRoot, 'logs');
|
|
|
|
// Ensure logs directory exists
|
|
if (!fs.existsSync(LOG_DIR)) {
|
|
fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
}
|
|
|
|
const getLogFileName = () => {
|
|
const date = new Date().toISOString().split('T')[0];
|
|
return path.join(LOG_DIR, `${date}.log`);
|
|
};
|
|
|
|
// Cleanup logs older than 14 days
|
|
const cleanOldLogs = () => {
|
|
try {
|
|
const files = fs.readdirSync(LOG_DIR);
|
|
const now = Date.now();
|
|
const fourteenDaysAgo = now - (14 * 24 * 60 * 60 * 1000);
|
|
|
|
files.forEach(file => {
|
|
if (!file.endsWith('.log')) return;
|
|
const filePath = path.join(LOG_DIR, file);
|
|
const stats = fs.statSync(filePath);
|
|
if (stats.mtimeMs < fourteenDaysAgo) {
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
});
|
|
} catch (err) {
|
|
console.error(`[LOGGER] Failed to clean old logs: ${err.message}`);
|
|
}
|
|
};
|
|
|
|
// Run cleanup on startup
|
|
cleanOldLogs();
|
|
|
|
const levels = {
|
|
info: 'INFO ',
|
|
warn: 'WARN ',
|
|
error: 'ERROR',
|
|
debug: 'DEBUG'
|
|
};
|
|
|
|
/**
|
|
* Safe string conversion for any value
|
|
*/
|
|
function safeString(value) {
|
|
if (value === null) return 'null';
|
|
if (value === undefined) return 'undefined';
|
|
if (typeof value === 'object') {
|
|
try {
|
|
return JSON.stringify(value, null, 2);
|
|
} catch (e) {
|
|
return `[Object ${Object.prototype.toString.call(value)}]`;
|
|
}
|
|
}
|
|
return String(value);
|
|
}
|
|
|
|
export function logger(module, message, level = 'info') {
|
|
const effectiveLevel = (process.env.LOG_LEVEL || 'info').toLowerCase();
|
|
const levelOrder = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
if ((levelOrder[level] || 20) < (levelOrder[effectiveLevel] || 20)) {
|
|
return; // filtered
|
|
}
|
|
|
|
const now = new Date();
|
|
const year = now.getFullYear();
|
|
const month = String(now.getMonth() + 1).padStart(2, '0');
|
|
const day = String(now.getDate()).padStart(2, '0');
|
|
const hours = String(now.getHours()).padStart(2, '0');
|
|
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|
const millis = String(now.getMilliseconds()).padStart(3, '0');
|
|
|
|
const timestamp = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${millis}`;
|
|
const levelStr = levels[level] || 'INFO ';
|
|
|
|
// Ultra-safe message conversion
|
|
let messageStr = '';
|
|
if (message == null) {
|
|
messageStr = message === null ? 'null' : 'undefined';
|
|
} else if (typeof message === 'object') {
|
|
try {
|
|
messageStr = JSON.stringify(message, null, 2);
|
|
} catch (e) {
|
|
messageStr = `[Object]`;
|
|
}
|
|
} else {
|
|
messageStr = String(message);
|
|
}
|
|
|
|
const logLine = `[${timestamp}] [${levelStr}] [${module}] ${messageStr}\n`;
|
|
|
|
// Console output
|
|
if (level === 'error') {
|
|
console.error(logLine.trim());
|
|
} else if (level === 'warn') {
|
|
console.warn(logLine.trim());
|
|
} else {
|
|
console.log(logLine.trim());
|
|
}
|
|
|
|
// Write to daily log file
|
|
try {
|
|
const logFile = getLogFileName();
|
|
fs.appendFileSync(logFile, logLine, 'utf8');
|
|
} catch (err) {
|
|
console.error(`[LOGGER] Failed to write to log file: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
// Convenience methods
|
|
export const logInfo = (module, msg) => logger(module, msg, 'info');
|
|
export const logWarn = (module, msg) => logger(module, msg, 'warn');
|
|
export const logError = (module, msg) => logger(module, msg, 'error');
|
|
export const logDebug = (module, msg) => logger(module, msg, 'debug');
|
|
|
|
export default logger; |