33 *
44 * This module provides standardized console logging utilities for internal application logging.
55 * It is separate from the user-facing logging system in logging.ts.
6+ *
7+ * For Node.js runtime, uses TraceRoot logger for enhanced logging with trace correlation.
8+ * For Edge runtime, falls back to console logging to avoid import errors.
69 */
710import chalk from 'chalk'
811import { env } from '@/lib/env'
912
13+ // Runtime detection - check if we're in Edge runtime
14+ const isEdgeRuntime = ( ) => {
15+ return (
16+ typeof window !== 'undefined' ||
17+ ( typeof globalThis !== 'undefined' && 'EdgeRuntime' in globalThis ) ||
18+ typeof process === 'undefined' ||
19+ ( typeof process !== 'undefined' && process . env . NEXT_RUNTIME === 'edge' )
20+ )
21+ }
22+
23+ // Conditional TraceRoot import - only in Node runtime
24+ let traceRootLogger : any = null
25+
26+ if ( ! isEdgeRuntime ( ) ) {
27+ try {
28+ const traceRoot = require ( 'traceroot-sdk-ts' )
29+ traceRootLogger = traceRoot . getLogger
30+ } catch ( importError ) {
31+ console . warn ( 'TraceRoot SDK not available, falling back to console logging' )
32+ }
33+ }
34+
1035/**
1136 * LogLevel enum defines the severity levels for logging
1237 *
@@ -108,16 +133,29 @@ const formatObject = (obj: any): string => {
108133 *
109134 * This class provides methods for logging at different severity levels
110135 * and handles formatting, colorization, and environment-specific behavior.
136+ * Uses TraceRoot logger in Node runtime and falls back to console logging in Edge runtime.
111137 */
112138export class Logger {
113139 private module : string
140+ private traceRootLoggerInstance : any = null
114141
115142 /**
116143 * Create a new logger for a specific module
117144 * @param module The name of the module (e.g., 'OpenAIProvider', 'AgentBlockHandler')
118145 */
119146 constructor ( module : string ) {
120147 this . module = module
148+
149+ // Initialize TraceRoot logger instance if available
150+ if ( traceRootLogger ) {
151+ try {
152+ this . traceRootLoggerInstance = traceRootLogger ( module )
153+ } catch ( error ) {
154+ console . warn (
155+ `Failed to create TraceRoot logger for module ${ module } , falling back to console logging`
156+ )
157+ }
158+ }
121159 }
122160
123161 /**
@@ -224,7 +262,11 @@ export class Logger {
224262 * @param args Additional arguments to log
225263 */
226264 debug ( message : string , ...args : any [ ] ) {
227- this . log ( LogLevel . DEBUG , message , ...args )
265+ if ( this . traceRootLoggerInstance ) {
266+ this . traceRootLoggerInstance . debug ( message , ...args )
267+ } else {
268+ this . log ( LogLevel . DEBUG , message , ...args )
269+ }
228270 }
229271
230272 /**
@@ -242,7 +284,11 @@ export class Logger {
242284 * @param args Additional arguments to log
243285 */
244286 info ( message : string , ...args : any [ ] ) {
245- this . log ( LogLevel . INFO , message , ...args )
287+ if ( this . traceRootLoggerInstance ) {
288+ this . traceRootLoggerInstance . info ( message , ...args )
289+ } else {
290+ this . log ( LogLevel . INFO , message , ...args )
291+ }
246292 }
247293
248294 /**
@@ -259,7 +305,11 @@ export class Logger {
259305 * @param args Additional arguments to log
260306 */
261307 warn ( message : string , ...args : any [ ] ) {
262- this . log ( LogLevel . WARN , message , ...args )
308+ if ( this . traceRootLoggerInstance ) {
309+ this . traceRootLoggerInstance . warn ( message , ...args )
310+ } else {
311+ this . log ( LogLevel . WARN , message , ...args )
312+ }
263313 }
264314
265315 /**
@@ -276,7 +326,11 @@ export class Logger {
276326 * @param args Additional arguments to log
277327 */
278328 error ( message : string , ...args : any [ ] ) {
279- this . log ( LogLevel . ERROR , message , ...args )
329+ if ( this . traceRootLoggerInstance ) {
330+ this . traceRootLoggerInstance . error ( message , ...args )
331+ } else {
332+ this . log ( LogLevel . ERROR , message , ...args )
333+ }
280334 }
281335}
282336
0 commit comments