Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions apps/sim/lib/logs/console/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { beforeEach, describe, expect, test, vi } from 'vitest'

vi.unmock('@/lib/logs/console/logger')

import { createLogger, Logger } from '@/lib/logs/console/logger'

describe('Logger', () => {
let logger: Logger

beforeEach(() => {
logger = new Logger('TestModule')
})

describe('class instantiation', () => {
test('should create logger instance', () => {
expect(logger).toBeDefined()
expect(logger).toBeInstanceOf(Logger)
})
})

describe('createLogger factory function', () => {
test('should create logger instance', () => {
const factoryLogger = createLogger('FactoryModule')
expect(factoryLogger).toBeDefined()
expect(factoryLogger).toBeInstanceOf(Logger)
})
})

describe('logging methods', () => {
test('should have debug method', () => {
expect(typeof logger.debug).toBe('function')
})

test('should have info method', () => {
expect(typeof logger.info).toBe('function')
})

test('should have warn method', () => {
expect(typeof logger.warn).toBe('function')
})

test('should have error method', () => {
expect(typeof logger.error).toBe('function')
})
})
})
62 changes: 58 additions & 4 deletions apps/sim/lib/logs/console/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,35 @@
*
* This module provides standardized console logging utilities for internal application logging.
* It is separate from the user-facing logging system in logging.ts.
*
* For Node.js runtime, uses TraceRoot logger for enhanced logging with trace correlation.
* For Edge runtime, falls back to console logging to avoid import errors.
*/
import chalk from 'chalk'
import { env } from '@/lib/env'

// Runtime detection - check if we're in Edge runtime
const isEdgeRuntime = () => {
return (
typeof window !== 'undefined' ||
(typeof globalThis !== 'undefined' && 'EdgeRuntime' in globalThis) ||
typeof process === 'undefined' ||
(typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge')
)
}

// Conditional TraceRoot import - only in Node runtime
let traceRootLogger: any = null

if (!isEdgeRuntime()) {
try {
const traceRoot = require('traceroot-sdk-ts')
traceRootLogger = traceRoot.getLogger
} catch (importError) {
console.warn('TraceRoot SDK not available, falling back to console logging')
}
}

/**
* LogLevel enum defines the severity levels for logging
*
Expand Down Expand Up @@ -108,16 +133,29 @@ const formatObject = (obj: any): string => {
*
* This class provides methods for logging at different severity levels
* and handles formatting, colorization, and environment-specific behavior.
* Uses TraceRoot logger in Node runtime and falls back to console logging in Edge runtime.
*/
export class Logger {
private module: string
private traceRootLoggerInstance: any = null

/**
* Create a new logger for a specific module
* @param module The name of the module (e.g., 'OpenAIProvider', 'AgentBlockHandler')
*/
constructor(module: string) {
this.module = module

// Initialize TraceRoot logger instance if available
if (traceRootLogger) {
try {
this.traceRootLoggerInstance = traceRootLogger(module)
} catch (error) {
console.warn(
`Failed to create TraceRoot logger for module ${module}, falling back to console logging`
)
}
}
}

/**
Expand Down Expand Up @@ -224,7 +262,11 @@ export class Logger {
* @param args Additional arguments to log
*/
debug(message: string, ...args: any[]) {
this.log(LogLevel.DEBUG, message, ...args)
if (this.traceRootLoggerInstance) {
this.traceRootLoggerInstance.debug(message, ...args)
} else {
this.log(LogLevel.DEBUG, message, ...args)
}
}

/**
Expand All @@ -242,7 +284,11 @@ export class Logger {
* @param args Additional arguments to log
*/
info(message: string, ...args: any[]) {
this.log(LogLevel.INFO, message, ...args)
if (this.traceRootLoggerInstance) {
this.traceRootLoggerInstance.info(message, ...args)
} else {
this.log(LogLevel.INFO, message, ...args)
}
}

/**
Expand All @@ -259,7 +305,11 @@ export class Logger {
* @param args Additional arguments to log
*/
warn(message: string, ...args: any[]) {
this.log(LogLevel.WARN, message, ...args)
if (this.traceRootLoggerInstance) {
this.traceRootLoggerInstance.warn(message, ...args)
} else {
this.log(LogLevel.WARN, message, ...args)
}
}

/**
Expand All @@ -276,7 +326,11 @@ export class Logger {
* @param args Additional arguments to log
*/
error(message: string, ...args: any[]) {
this.log(LogLevel.ERROR, message, ...args)
if (this.traceRootLoggerInstance) {
this.traceRootLoggerInstance.error(message, ...args)
} else {
this.log(LogLevel.ERROR, message, ...args)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions apps/sim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"three": "0.177.0",
"traceroot-sdk-ts": "0.0.1-alpha.33",
"uuid": "^11.1.0",
"xlsx": "0.18.5",
"zod": "^3.24.2"
Expand Down
36 changes: 36 additions & 0 deletions apps/sim/traceroot.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { TraceRootConfigFile } from 'traceroot-sdk-ts'

const config: TraceRootConfigFile = {
// Basic service configuration
service_name: 'sim',
github_owner: 'simstudioai',
github_repo_name: 'sim',
github_commit_hash: 'staging',

// Your environment configuration such as development, staging, production
environment: process.env.NODE_ENV || 'development',

// Token configuration
// This is the token you can generate from the TraceRoot.AI website
token: 'traceroot-*',

// Whether to enable console export of spans and logs
enable_span_console_export: false,
enable_log_console_export: true,

// Whether to enable cloud export of spans and logs
enable_span_cloud_export: false,
enable_log_cloud_export: false,

// Log level
log_level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',

// Local mode that whether to store all TraceRoot data locally
// and allow traceroot platform serving locally
// This requires Jaeger to be installed and running
local_mode: false,

// Whether to auto-initialize the traceroot SDK
autoInit: true,
}
export default config
Loading