@nullplatform/llm-gateway
Version:
LLM Gateway Core - Main proxy server
132 lines • 4.67 kB
JavaScript
// packages/core/basic-apikey-auth/utils/clickhouse-tracer.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const winston = __importStar(require("winston"));
class Logger {
winston;
level = 'info';
constructor(level = 'info') {
this.level = level;
this.winston = winston.createLogger({
level: this.level,
format: winston.format.combine(winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.json()),
defaultMeta: { service: 'llm-gateway' },
transports: [
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple())
})
]
});
// Add file transport if in production
if (process.env.NODE_ENV === 'production') {
this.winston.add(new winston.transports.File({
filename: 'logs/error.log',
level: 'error'
}));
this.winston.add(new winston.transports.File({
filename: 'logs/combined.log'
}));
}
}
setLevel(level) {
this.level = level;
this.winston.level = level;
}
debug(message, meta) {
this.winston.debug(message, meta);
}
info(message, meta) {
this.winston.info(message, meta);
}
warn(message, meta) {
this.winston.warn(message, meta);
}
error(message, meta) {
const err = meta?.error ? meta.error : meta;
this.winston.error(message, err);
}
// Create child clickhouse-tracer with additional context
child(context) {
const childLogger = new Logger(this.level);
childLogger.winston = this.winston.child(context);
return childLogger;
}
// Performance timing helper
time(label) {
const start = Date.now();
return () => {
const duration = Date.now() - start;
this.debug(`Timer: ${label}`, { duration_ms: duration });
return duration;
};
}
// HTTP request clickhouse-tracer
logRequest(req, res, duration) {
this.info('HTTP Request', {
method: req.method,
url: req.url,
status: res.statusCode,
duration_ms: duration,
user_agent: req.headers['user-agent'],
ip: req.ip || req.connection.remoteAddress,
request_id: req.id
});
}
// Plugin execution clickhouse-tracer
logPluginExecution(pluginName, phase, duration, success) {
const level = success ? 'debug' : 'warn';
this[level]('Plugin execution', {
plugin: pluginName,
phase,
duration_ms: duration,
success
});
}
// LLM provider request clickhouse-tracer
logProviderRequest(provider, model, tokens, duration) {
this.info('Provider request', {
provider,
model,
input_tokens: tokens.prompt_tokens,
output_tokens: tokens.completion_tokens,
total_tokens: tokens.total_tokens,
duration_ms: duration
});
}
}
exports.Logger = Logger;
//# sourceMappingURL=logger.js.map
;