Logging API
LoggerConfig, LogLevel and StabilizeLogger
LoggerConfig
export interface LoggerConfig { level?: LogLevel; filePath?: string; maxFileSize?: number; maxFiles?: number;}Passed as the third positional argument to the Stabilize constructor.
constructor( config: DBConfig, cacheConfig: CacheConfig = { enabled: false, ttl: 60 }, loggerConfig: LoggerConfig = {}, existingClient?: DBClient,)Options:
- level Maximum level to write. Defaults to
LogLevel.Info - filePath Log file path. Defaults to
null— console only - maxFileSize Rotate at this size. Defaults to
1 * 1024 * 1024 - maxFiles Number of rotated files kept. Defaults to
3
import { Stabilize, DBType, LogLevel } from "stabilize-orm";
const orm = new Stabilize( { type: DBType.SQLite, connectionString: "./data/app.db" }, { enabled: false, ttl: 60 }, { level: LogLevel.Warn, filePath: "./logs/stabilize.log", maxFileSize: 5 * 1024 * 1024, maxFiles: 5, });LogLevel
export enum LogLevel { Debug, Info, Warn, Error } // 0, 1, 2, 3level is a maximum, not a minimum: filtering is shouldLog(messageLevel) { return messageLevel <= this.level; }. A lower number is therefore more verbose.
What each setting writes:
- Debug (0) Debug, Info, Warn and Error
- Info (1) Info, Warn and Error — the default
- Warn (2) Warn and Error only
- Error (3) Error only
Lower number = more verbose.
Setting LogLevel.Warn writes Warn and Error only — everything below is dropped. Setting LogLevel.Debug writes everything.
StabilizeLogger methods
logQuery(query: string, params: any[], executionTime?: number): voidlogError(error: Error): voidlogMetrics(metrics: PoolMetrics): voidlogInfo(message: string): voidlogWarn(message: string): voidlogDebug(message: string): voidPoolMetrics is { activeConnections; idleConnections; totalConnections }.
- logQuery SQL text, bound parameters, and optional execution time
- logError An
Errorinstance - logMetrics A
PoolMetricssnapshot - logInfo A plain Info message
- logWarn A plain Warn message
- logDebug A plain Debug message
Writes are fire-and-forget.
These public methods call the private async log() / rotateLogFile() without awaiting them. A write still in flight when the process exits is lost, so flush by keeping the process alive or by logging early rather than relying on the final line before exit.
File rotation
Rotation only applies when filePath is set. Once the current file reaches maxFileSize, it is renamed to .1, existing rotations shift up one number, and the oldest file (.{maxFiles}) is deleted.