Core functions and utilities for Langfuse packages including API client, logging, media handling, and OpenTelemetry tracing attributes
Configurable logging infrastructure for the Langfuse SDK with multiple severity levels, custom prefixes, timestamps, and both global singleton and instance-based usage patterns.
Enumeration of log levels in order of severity (lowest to highest).
enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3
}Values:
DEBUG = 0 - Detailed debug information for troubleshooting development issuesINFO = 1 - General informational messages about application flowWARN = 2 - Warning messages for potentially harmful situationsERROR = 3 - Critical errors that may cause application failureImport:
import { LogLevel } from '@langfuse/core';A configurable logger class that supports different log levels, custom prefixes, and optional timestamps.
class Logger {
constructor(config?: LoggerConfig);
error(message: string, ...args: unknown[]): void;
warn(message: string, ...args: unknown[]): void;
info(message: string, ...args: unknown[]): void;
debug(message: string, ...args: unknown[]): void;
setLevel(level: LogLevel): void;
getLevel(): LogLevel;
}
interface LoggerConfig {
level: LogLevel;
prefix?: string;
enableTimestamp?: boolean;
}Import:
import { Logger, LoggerConfig, LogLevel } from '@langfuse/core';Creates a new Logger instance with optional configuration.
constructor(config?: LoggerConfig)Parameters:
config (optional) - Configuration object for the logger
level: LogLevel - The minimum log level to output (required if config provided)prefix?: string - Optional prefix to prepend to all log messagesenableTimestamp?: boolean - Whether to include timestamps (default: true)Usage Example:
import { Logger, LogLevel } from '@langfuse/core';
// Create logger with debug level
const logger = new Logger({
level: LogLevel.DEBUG,
prefix: 'Langfuse SDK',
enableTimestamp: true
});
logger.debug('Initialization started');
logger.info('Connected to API');
logger.warn('Rate limit approaching');
logger.error('Failed to send batch', { batchSize: 10 });Logs an error message if the logger's level is ERROR or lower.
/**
* Logs an error message with optional additional arguments
* @param message - The error message to log
* @param args - Additional arguments to log (objects, errors, etc.)
*/
error(message: string, ...args: unknown[]): void;Usage Example:
logger.error('Failed to fetch trace', { traceId: 'abc123' });
try {
// some operation
} catch (error) {
logger.error('Operation failed', error);
}Logs a warning message if the logger's level is WARN or lower.
/**
* Logs a warning message with optional additional arguments
* @param message - The warning message to log
* @param args - Additional arguments to log
*/
warn(message: string, ...args: unknown[]): void;Usage Example:
logger.warn('Retry attempt 3 of 5', { endpoint: '/api/traces' });Logs an informational message if the logger's level is INFO or lower.
/**
* Logs an informational message with optional additional arguments
* @param message - The info message to log
* @param args - Additional arguments to log
*/
info(message: string, ...args: unknown[]): void;Usage Example:
logger.info('Batch sent successfully', { batchSize: 50 });Logs a debug message if the logger's level is DEBUG.
/**
* Logs a debug message with optional additional arguments
* @param message - The debug message to log
* @param args - Additional arguments to log
*/
debug(message: string, ...args: unknown[]): void;Usage Example:
logger.debug('Request details', { url: '/api/traces', method: 'POST' });Updates the minimum log level for the logger.
/**
* Sets the minimum log level
* @param level - The new minimum log level
*/
setLevel(level: LogLevel): void;Usage Example:
// Start with info level
const logger = new Logger({ level: LogLevel.INFO });
// Enable debug logging for troubleshooting
logger.setLevel(LogLevel.DEBUG);
logger.debug('Now debug messages will appear');
// Back to info for production
logger.setLevel(LogLevel.INFO);Retrieves the current minimum log level.
/**
* Gets the current minimum log level
* @returns The current log level
*/
getLevel(): LogLevel;Usage Example:
const currentLevel = logger.getLevel();
console.log(`Current log level: ${LogLevel[currentLevel]}`);Convenience functions for working with the global singleton logger instance.
function configureGlobalLogger(config: LoggerConfig): void;
function getGlobalLogger(): Logger;
function resetGlobalLogger(): void;Import:
import {
configureGlobalLogger,
getGlobalLogger,
resetGlobalLogger,
LogLevel
} from '@langfuse/core';Configures the global logger with new settings. Should be called early in application initialization before any logging occurs.
/**
* Configures the global singleton logger
* @param config - Logger configuration
*/
function configureGlobalLogger(config: LoggerConfig): void;Usage Example:
import { configureGlobalLogger, LogLevel } from '@langfuse/core';
// Configure at app startup
configureGlobalLogger({
level: LogLevel.INFO,
prefix: 'MyApp',
enableTimestamp: true
});Gets the global singleton logger instance.
/**
* Gets the global singleton logger instance
* @returns The global logger instance
*/
function getGlobalLogger(): Logger;Usage Example:
import { getGlobalLogger } from '@langfuse/core';
const logger = getGlobalLogger();
logger.info('Using global logger');Resets the global logger instance and configuration. Primarily used for testing.
/**
* Resets the global logger instance and configuration
* Used primarily for testing purposes
*/
function resetGlobalLogger(): void;Usage Example:
import { resetGlobalLogger } from '@langfuse/core';
// In test cleanup
afterEach(() => {
resetGlobalLogger();
});Creates a new independent Logger instance with the specified configuration. Unlike the global logger, this creates an isolated instance.
/**
* Creates a new Logger instance independent of the global singleton
* @param config - Optional logger configuration
* @returns A new Logger instance
*/
function createLogger(config?: LoggerConfig): Logger;Import:
import { createLogger, LogLevel } from '@langfuse/core';Usage Example:
import { createLogger, LogLevel } from '@langfuse/core';
// Create custom logger for a specific module
const moduleLogger = createLogger({
level: LogLevel.DEBUG,
prefix: 'TraceProcessor',
enableTimestamp: true
});
moduleLogger.debug('Processing trace batch');Direct access to the LoggerSingleton class for advanced usage patterns. This provides an alternative way to access the global logger singleton.
/**
* The singleton logger instance for convenient access
*/
export const logger: typeof LoggerSingleton;
class LoggerSingleton {
static getInstance(): Logger;
static configure(config: LoggerConfig): void;
static reset(): void;
}Import:
import { logger } from '@langfuse/core';Usage Example:
import { logger, LogLevel } from '@langfuse/core';
// Access global logger via singleton
const globalLogger = logger.getInstance();
globalLogger.info('Quick logging access');
// Configure via singleton
logger.configure({
level: LogLevel.DEBUG,
prefix: 'Langfuse SDK',
enableTimestamp: true
});
// Reset via singleton (useful for testing)
logger.reset();Use the global logger singleton for application-wide logging with consistent configuration.
import { configureGlobalLogger, getGlobalLogger, LogLevel } from '@langfuse/core';
// Configure once at application startup
configureGlobalLogger({
level: process.env.NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.DEBUG,
prefix: 'Langfuse',
enableTimestamp: true
});
// Use anywhere in your application
function processTraces() {
const logger = getGlobalLogger();
logger.info('Starting trace processing');
// ... processing logic
}Create dedicated logger instances for different modules or components.
import { createLogger, LogLevel } from '@langfuse/core';
class TraceService {
private logger: Logger;
constructor() {
this.logger = createLogger({
level: LogLevel.INFO,
prefix: 'TraceService'
});
}
async processTrace(traceId: string) {
this.logger.debug('Processing trace', { traceId });
try {
// ... processing
this.logger.info('Trace processed successfully', { traceId });
} catch (error) {
this.logger.error('Failed to process trace', { traceId, error });
throw error;
}
}
}Configure logging based on environment variables.
import { configureGlobalLogger, LogLevel, getEnv } from '@langfuse/core';
// Map environment variable to LogLevel
const logLevelMap: Record<string, LogLevel> = {
'DEBUG': LogLevel.DEBUG,
'INFO': LogLevel.INFO,
'WARN': LogLevel.WARN,
'ERROR': LogLevel.ERROR
};
const logLevelStr = getEnv('LANGFUSE_LOG_LEVEL') || 'INFO';
const logLevel = logLevelMap[logLevelStr] ?? LogLevel.INFO;
configureGlobalLogger({
level: logLevel,
prefix: 'Langfuse SDK',
enableTimestamp: true
});Enable debug logging only when needed without affecting production.
import { Logger, LogLevel } from '@langfuse/core';
const logger = new Logger({
level: process.env.DEBUG ? LogLevel.DEBUG : LogLevel.INFO
});
// Debug messages only appear when DEBUG env var is set
logger.debug('Detailed request information', {
headers: { /* ... */ },
body: { /* ... */ }
});enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3
}
interface LoggerConfig {
level: LogLevel;
prefix?: string;
enableTimestamp?: boolean;
}
class Logger {
constructor(config?: LoggerConfig);
error(message: string, ...args: unknown[]): void;
warn(message: string, ...args: unknown[]): void;
info(message: string, ...args: unknown[]): void;
debug(message: string, ...args: unknown[]): void;
setLevel(level: LogLevel): void;
getLevel(): LogLevel;
}Install with Tessl CLI
npx tessl i tessl/npm-langfuse--core