@posthog/agent
Version:
TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog
84 lines (82 loc) • 2.46 kB
JavaScript
/**
* Simple logger utility with configurable debug mode and external log forwarding
*/
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
LogLevel[LogLevel["WARN"] = 1] = "WARN";
LogLevel[LogLevel["INFO"] = 2] = "INFO";
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
})(LogLevel || (LogLevel = {}));
class Logger {
debugEnabled;
prefix;
scope;
onLog;
constructor(config = {}) {
this.debugEnabled = config.debug ?? false;
this.prefix = config.prefix ?? "[PostHog Agent]";
this.scope = config.scope ?? "agent";
this.onLog = config.onLog;
}
setDebug(enabled) {
this.debugEnabled = enabled;
}
setOnLog(onLog) {
this.onLog = onLog;
}
formatMessage(level, message, data) {
const timestamp = new Date().toISOString();
const base = `${timestamp} ${this.prefix} [${level}] ${message}`;
if (data !== undefined) {
return `${base} ${JSON.stringify(data, null, 2)}`;
}
return base;
}
emitLog(level, message, data) {
if (this.onLog) {
this.onLog(level, this.scope, message, data);
return;
}
const shouldLog = this.debugEnabled || level === "error";
if (shouldLog) {
console[level](this.formatMessage(level.toLowerCase(), message, data));
}
}
error(message, error) {
const data = error instanceof Error
? { message: error.message, stack: error.stack }
: error;
this.emitLog("error", message, data);
}
warn(message, data) {
this.emitLog("warn", message, data);
}
info(message, data) {
this.emitLog("info", message, data);
}
debug(message, data) {
this.emitLog("debug", message, data);
}
log(level, message, data, scope) {
const originalScope = this.scope;
if (scope) {
this.scope = scope;
}
this.emitLog(level, message, data);
this.scope = originalScope;
}
/**
* Create a child logger with additional prefix and scope
*/
child(childPrefix) {
return new Logger({
debug: this.debugEnabled,
prefix: `${this.prefix} [${childPrefix}]`,
scope: `${this.scope}:${childPrefix}`,
onLog: this.onLog,
});
}
}
export { LogLevel, Logger };
//# sourceMappingURL=logger.js.map