feat(traceroot): add traceroot logger

This commit is contained in:
Vikhyath Mondreti
2025-09-23 10:39:52 -07:00
parent b7876ca466
commit 6d1b172a3e
6 changed files with 923 additions and 217 deletions

View File

@@ -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')
})
})
})

View File

@@ -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
*
@@ -108,9 +133,11 @@ 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
@@ -118,6 +145,17 @@ export class Logger {
*/
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`
)
}
}
}
/**
@@ -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)
}
}
/**
@@ -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)
}
}
/**
@@ -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)
}
}
/**
@@ -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)
}
}
}

View File

@@ -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"

View File

@@ -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

994
bun.lock

File diff suppressed because it is too large Load Diff

View File

@@ -46,6 +46,7 @@
"react-colorful": "5.6.1",
"remark-gfm": "4.0.1",
"socket.io-client": "4.8.1",
"traceroot-sdk-ts": "0.0.1-alpha.35",
"twilio": "5.9.0"
},
"devDependencies": {