@posthog/agent
Version:
TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog
111 lines (91 loc) • 2.57 kB
text/typescript
import type { LogLevel as LogLevelType, OnLogCallback } from "../types.js";
/**
* Simple logger utility with configurable debug mode and external log forwarding
*/
export enum LogLevel {
ERROR = 0,
WARN = 1,
INFO = 2,
DEBUG = 3,
}
export interface LoggerConfig {
debug?: boolean;
prefix?: string;
scope?: string;
onLog?: OnLogCallback;
}
export class Logger {
private debugEnabled: boolean;
private prefix: string;
private scope: string;
private onLog?: OnLogCallback;
constructor(config: LoggerConfig = {}) {
this.debugEnabled = config.debug ?? false;
this.prefix = config.prefix ?? "[PostHog Agent]";
this.scope = config.scope ?? "agent";
this.onLog = config.onLog;
}
setDebug(enabled: boolean) {
this.debugEnabled = enabled;
}
setOnLog(onLog: OnLogCallback | undefined) {
this.onLog = onLog;
}
private formatMessage(
level: string,
message: string,
data?: unknown,
): string {
const timestamp = new Date().toISOString();
const base = `${timestamp} ${this.prefix} [${level}] ${message}`;
if (data !== undefined) {
return `${base} ${JSON.stringify(data, null, 2)}`;
}
return base;
}
private emitLog(level: LogLevelType, message: string, data?: unknown) {
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: string, error?: Error | unknown) {
const data =
error instanceof Error
? { message: error.message, stack: error.stack }
: error;
this.emitLog("error", message, data);
}
warn(message: string, data?: unknown) {
this.emitLog("warn", message, data);
}
info(message: string, data?: unknown) {
this.emitLog("info", message, data);
}
debug(message: string, data?: unknown) {
this.emitLog("debug", message, data);
}
log(level: LogLevelType, message: string, data?: unknown, scope?: string) {
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: string): Logger {
return new Logger({
debug: this.debugEnabled,
prefix: `${this.prefix} [${childPrefix}]`,
scope: `${this.scope}:${childPrefix}`,
onLog: this.onLog,
});
}
}