UNPKG

bear-tracker

Version:

Lightweight bot detection middleware for tracking AI crawler visits (OpenAI, ChatGPT, etc.) with API support and analytics

51 lines 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BotLogger = void 0; class BotLogger { constructor(logLevel = 'info') { this.logLevel = logLevel; } log(botInfo) { const logData = { timestamp: botInfo.timestamp.toISOString(), bot_detected: botInfo.isBot, bot_name: botInfo.name, bot_type: botInfo.type, user_agent: botInfo.userAgent, ...(botInfo.ip && { ip_address: botInfo.ip }), ...(botInfo.description && { bot_description: botInfo.description }) }; // Format for structured logging (Vercel/Serverless friendly) const logMessage = `[BOT_TRACKER] ${JSON.stringify(logData)}`; switch (this.logLevel) { case 'error': if (botInfo.isBot && (botInfo.type === 'ai_training' || botInfo.type === 'unknown')) { console.error(logMessage); } break; case 'warn': if (botInfo.isBot) { console.warn(logMessage); } break; case 'info': default: console.log(logMessage); break; } } static createStructuredLog(botInfo) { return { event: 'ai_bot_detection', timestamp: botInfo.timestamp.toISOString(), bot_detected: botInfo.isBot, bot_name: botInfo.name, bot_type: botInfo.type, user_agent: botInfo.userAgent, ...(botInfo.ip && { ip_address: botInfo.ip }), ...(botInfo.description && { bot_description: botInfo.description }) }; } } exports.BotLogger = BotLogger; //# sourceMappingURL=logger.js.map