Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import pc from 'picocolors'; |
| 2 | import { COMPOSIO_LOG_LEVEL } from './constants'; |
| 3 | import { redactSensitiveText } from '../telemetry/redact'; |
| 4 | |
| 5 | // Define log levels with corresponding priorities |
| 6 | export const LOG_LEVELS = { |
| 7 | silent: -1, // No logs |
| 8 | error: 0, // Highest priority - critical errors |
| 9 | warn: 1, // Warning messages |
| 10 | info: 2, // General information |
| 11 | debug: 3, // Debug information |
| 12 | } as const; |
| 13 | |
| 14 | export type LogLevel = keyof typeof LOG_LEVELS; |
| 15 | |
| 16 | /** |
| 17 | * Get the current log level from environment variables. |
| 18 | * Defaults to 'info' if not set or invalid. |
| 19 | * @returns {LogLevel} The current log level |
| 20 | */ |
| 21 | export const getLogLevel = (): LogLevel => { |
| 22 | const envLevel = (COMPOSIO_LOG_LEVEL ?? 'info')?.toLowerCase(); |
| 23 | return envLevel && envLevel in LOG_LEVELS ? (envLevel as LogLevel) : 'info'; |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * Destination for SDK log output. Each method receives the already |
| 28 | * formatted and redacted message as its single argument. `console` satisfies |
| 29 | * this interface, and so do most structured loggers (pino, winston, ...). |
| 30 | */ |
| 31 | export interface ComposioLogger { |
| 32 | error: (...args: unknown[]) => void; |
| 33 | warn: (...args: unknown[]) => void; |
| 34 | info: (...args: unknown[]) => void; |
| 35 | debug: (...args: unknown[]) => void; |
| 36 | } |
| 37 | |
| 38 | interface LoggerOptions { |
| 39 | level?: LogLevel; |
| 40 | includeTimestamp?: boolean; |
| 41 | /** Where formatted output is written. Defaults to `console`. */ |
| 42 | sink?: ComposioLogger; |
| 43 | } |
| 44 | |
| 45 | export interface LoggerConfigureOptions { |
| 46 | level?: LogLevel; |
| 47 | sink?: ComposioLogger; |
| 48 | } |
| 49 | |
| 50 | export class Logger { |
| 51 | private level: LogLevel; |
| 52 | private readonly includeTimestamp: boolean; |
| 53 | private sink: ComposioLogger; |
| 54 | |
| 55 | constructor(options: LoggerOptions = {}) { |
| 56 | this.level = options.level ?? getLogLevel(); |
| 57 | this.includeTimestamp = options.includeTimestamp ?? true; |
| 58 | this.sink = options.sink ?? console; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Reconfigure the level and/or sink in place. Omitted fields are left |
| 63 | * unchanged, so `configure({ level: 'debug' })` keeps the current sink. |
| 64 | */ |
| 65 | configure(options: LoggerConfigureOptions): void { |
| 66 | if (options.level !== undefined) this.level = options.level; |
| 67 | if (options.sink !== undefined) this.sink = options.sink; |
| 68 | } |
| 69 | |
| 70 | getLevel(): LogLevel { |
| 71 | return this.level; |
| 72 | } |
| 73 | |
| 74 | private formatMessage(args: unknown[]): string { |
| 75 | const formattedArgs = args |
| 76 | .map((arg, index) => { |
| 77 | if (typeof arg === 'object') { |
| 78 | return JSON.stringify(arg); |
| 79 | } else { |
| 80 | if (index === 0) { |
| 81 | if (args.length > 1) { |
| 82 | return pc.yellow(`${arg}`); |
| 83 | } else { |
| 84 | return String(arg); |
| 85 | } |
| 86 | } |
| 87 | return String(arg); |
| 88 | } |
| 89 | }) |
| 90 | .join('\n'); |
| 91 | |
| 92 | const redactedArgs = redactSensitiveText(formattedArgs) ?? formattedArgs; |
| 93 | |
| 94 | if (!this.includeTimestamp) return redactedArgs; |
| 95 | |
| 96 | const timestamp = new Date().toISOString(); |
| 97 | return `${pc.gray(timestamp)} - ${redactedArgs}`; |
| 98 | } |
| 99 | |
| 100 | private shouldLog(level: LogLevel): boolean { |
| 101 | return LOG_LEVELS[level] <= LOG_LEVELS[this.level]; |
| 102 | } |
| 103 | |
| 104 | error(...args: unknown[]): void { |
| 105 | if (this.shouldLog('error')) { |
| 106 | this.sink.error(this.formatMessage(args)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | warn(...args: unknown[]): void { |
| 111 | if (this.shouldLog('warn')) { |
| 112 | this.sink.warn(this.formatMessage(args)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | info(...args: unknown[]): void { |
| 117 | if (this.shouldLog('info')) { |
| 118 | this.sink.info(this.formatMessage(args)); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | debug(...args: unknown[]): void { |
| 123 | if (this.shouldLog('debug')) { |
| 124 | this.sink.debug(this.formatMessage(args)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Create and export a default logger instance |
| 130 | const logger = new Logger(); |
| 131 | export default logger; |