Skip to content

Commit 6d1b172

Browse files
committed
feat(traceroot): add traceroot logger
1 parent b7876ca commit 6d1b172

File tree

6 files changed

+923
-217
lines changed

6 files changed

+923
-217
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { beforeEach, describe, expect, test, vi } from 'vitest'
2+
3+
vi.unmock('@/lib/logs/console/logger')
4+
5+
import { createLogger, Logger } from '@/lib/logs/console/logger'
6+
7+
describe('Logger', () => {
8+
let logger: Logger
9+
10+
beforeEach(() => {
11+
logger = new Logger('TestModule')
12+
})
13+
14+
describe('class instantiation', () => {
15+
test('should create logger instance', () => {
16+
expect(logger).toBeDefined()
17+
expect(logger).toBeInstanceOf(Logger)
18+
})
19+
})
20+
21+
describe('createLogger factory function', () => {
22+
test('should create logger instance', () => {
23+
const factoryLogger = createLogger('FactoryModule')
24+
expect(factoryLogger).toBeDefined()
25+
expect(factoryLogger).toBeInstanceOf(Logger)
26+
})
27+
})
28+
29+
describe('logging methods', () => {
30+
test('should have debug method', () => {
31+
expect(typeof logger.debug).toBe('function')
32+
})
33+
34+
test('should have info method', () => {
35+
expect(typeof logger.info).toBe('function')
36+
})
37+
38+
test('should have warn method', () => {
39+
expect(typeof logger.warn).toBe('function')
40+
})
41+
42+
test('should have error method', () => {
43+
expect(typeof logger.error).toBe('function')
44+
})
45+
})
46+
})

apps/sim/lib/logs/console/logger.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,35 @@
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
*/
710
import chalk from 'chalk'
811
import { 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
*/
112138
export 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

apps/sim/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"tailwind-merge": "^2.6.0",
126126
"tailwindcss-animate": "^1.0.7",
127127
"three": "0.177.0",
128+
"traceroot-sdk-ts": "0.0.1-alpha.33",
128129
"uuid": "^11.1.0",
129130
"xlsx": "0.18.5",
130131
"zod": "^3.24.2"

apps/sim/traceroot.config.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { TraceRootConfigFile } from 'traceroot-sdk-ts'
2+
3+
const config: TraceRootConfigFile = {
4+
// Basic service configuration
5+
service_name: 'sim',
6+
github_owner: 'simstudioai',
7+
github_repo_name: 'sim',
8+
github_commit_hash: 'staging',
9+
10+
// Your environment configuration such as development, staging, production
11+
environment: process.env.NODE_ENV || 'development',
12+
13+
// Token configuration
14+
// This is the token you can generate from the TraceRoot.AI website
15+
token: 'traceroot-*',
16+
17+
// Whether to enable console export of spans and logs
18+
enable_span_console_export: false,
19+
enable_log_console_export: true,
20+
21+
// Whether to enable cloud export of spans and logs
22+
enable_span_cloud_export: false,
23+
enable_log_cloud_export: false,
24+
25+
// Log level
26+
log_level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
27+
28+
// Local mode that whether to store all TraceRoot data locally
29+
// and allow traceroot platform serving locally
30+
// This requires Jaeger to be installed and running
31+
local_mode: false,
32+
33+
// Whether to auto-initialize the traceroot SDK
34+
autoInit: true,
35+
}
36+
export default config

0 commit comments

Comments
 (0)