Overview
The LogService class provides a structured logging solution built on top of LogTape. It supports multiple log levels, categories, sinks, and contextual logging.
Class: LogService
Constructor
new LogService(config?: LogConfig)
Configuration options for the logging service
The default target sink name
The default log category. Defaults to "app".
Named sinks for log output (console, file, etc.)
Filter configurations for controlling log output
Array of logger configurations
Example:
import { LogService } from '@resolid/app-log';
const logService = new LogService({
defaultCategory: 'my-app',
sinks: {
console: getConsoleSink()
}
});
await logService.configure();
Methods
Initializes the logging system with the provided configuration.
async configure(): Promise<void>
This method must be called before using the logger. In a Resolid application, this is typically called automatically during the bootstrap phase.
Example:
await logService.configure();
debug()
Logs a debug-level message.
debug(message: string, properties?: Record<string, unknown>): void
Additional properties to include in the log entry
Example:
logService.debug('Processing request', { userId: 123, endpoint: '/api/users' });
info()
Logs an info-level message.
info(message: string, properties?: Record<string, unknown>): void
Additional properties to include in the log entry
Example:
logService.info('User logged in', { userId: 123, timestamp: Date.now() });
warn()
Logs a warning-level message.
warn(message: string, properties?: Record<string, unknown>): void
Additional properties to include in the log entry
Example:
logService.warn('Rate limit approaching', { requests: 95, limit: 100 });
error()
Logs an error-level message.
error(message: string, properties?: Record<string, unknown>): void
Additional properties to include in the log entry
Example:
try {
// Some operation
} catch (err) {
logService.error('Failed to process request', { error: err, userId: 123 });
}
fatal()
Logs a fatal-level message for critical errors.
fatal(message: string, properties?: Record<string, unknown>): void
Additional properties to include in the log entry
Example:
logService.fatal('Database connection failed', { error: err });
category()
Returns a logger for a specific category.
category(name: string): LogCategory
A logger instance for the specified category with debug, info, warn, error, and fatal methods
Example:
const dbLogger = logService.category('database');
dbLogger.info('Query executed', { query: 'SELECT * FROM users', duration: 42 });
getLogger()
Returns a logger instance for a specific category.
getLogger(category?: string): Logger
The category name. Defaults to the default category specified in the config.
A full logger instance from LogTape
Example:
const logger = logService.getLogger('auth');
logger.info('User authenticated');
withContext()
Creates a logger with additional context properties.
withContext(context: Record<string, unknown>): Logger
context
Record<string, unknown>
required
Context properties to include in all log entries
A logger instance with the specified context
Example:
const requestLogger = logService.withContext({
requestId: 'abc-123',
userId: 456
});
requestLogger.info('Processing request'); // Includes requestId and userId
withFilter()
Creates a logger with a custom filter function.
withFilter(filter: (log: LogRecord) => boolean): Logger
filter
(log: LogRecord) => boolean
required
A function that returns true to allow the log or false to suppress it
A filtered logger instance
dispose()
Cleans up logging resources.
async dispose(): Promise<void>
Example:
await logService.dispose();
Type Definitions
LogConfig
type LogConfig = {
defaultTarget?: string;
defaultCategory?: string;
sinks?: Record<string, Sink>;
filters?: Config['filters'];
loggers?: LoggerEntity[];
}
LoggerEntity
type LoggerEntity = {
category: string;
sinks?: string[];
level?: string;
}
LogCategory
type LogCategory = {
debug(message: string, properties?: Record<string, unknown>): void;
info(message: string, properties?: Record<string, unknown>): void;
warn(message: string, properties?: Record<string, unknown>): void;
error(message: string, properties?: Record<string, unknown>): void;
fatal(message: string, properties?: Record<string, unknown>): void;
}
Usage Example
import { LogService, getConsoleSink } from '@resolid/app-log';
const logService = new LogService({
defaultCategory: 'my-app',
sinks: {
console: getConsoleSink()
},
loggers: [
{ category: 'database', sinks: ['console'], level: 'debug' },
{ category: 'api', sinks: ['console'], level: 'info' }
]
});
await logService.configure();
// Use default category
logService.info('Application started');
logService.debug('Configuration loaded', { env: 'production' });
// Use specific categories
const dbLogger = logService.category('database');
dbLogger.info('Connected to database');
const apiLogger = logService.category('api');
apiLogger.warn('Rate limit exceeded', { ip: '192.168.1.1' });
// Context logging
const requestLogger = logService.withContext({ requestId: 'req-123' });
requestLogger.info('Processing request');
Source Code
Location: packages/app-log/src/index.ts