ble-mcp-test
Version:
Complete BLE testing stack: WebSocket bridge server, MCP observability layer, and Web Bluetooth API mock. Test real BLE devices in Playwright/E2E tests without browser support.
45 lines (44 loc) • 1.42 kB
JavaScript
import { normalizeLogLevel } from './utils.js';
export class Logger {
prefix;
level;
includeTimestamp;
constructor(prefix) {
this.prefix = prefix;
this.level = normalizeLogLevel(process.env.BLE_MCP_LOG_LEVEL);
this.includeTimestamp = process.env.BLE_MCP_LOG_TIMESTAMPS !== 'false';
}
formatMessage(...args) {
if (this.includeTimestamp) {
const timestamp = new Date().toISOString().substring(11, 23); // HH:MM:SS.mmm
return [`[${timestamp}] [${this.prefix}]`, ...args];
}
return [`[${this.prefix}]`, ...args];
}
shouldLog(messageLevel) {
const levels = ['debug', 'info', 'warn', 'error'];
const currentLevelIndex = levels.indexOf(this.level);
const messageLevelIndex = levels.indexOf(messageLevel);
return messageLevelIndex >= currentLevelIndex;
}
debug(...args) {
if (this.shouldLog('debug')) {
console.log(...this.formatMessage(...args));
}
}
info(...args) {
if (this.shouldLog('info')) {
console.log(...this.formatMessage(...args));
}
}
warn(...args) {
if (this.shouldLog('warn')) {
console.warn(...this.formatMessage(...args));
}
}
error(...args) {
if (this.shouldLog('error')) {
console.error(...this.formatMessage(...args));
}
}
}