Logging API

LoggerConfig, LogLevel and StabilizeLogger

LoggerConfig

typescript
export interface LoggerConfig {
level?: LogLevel;
filePath?: string;
maxFileSize?: number;
maxFiles?: number;
}

Passed as the third positional argument to the Stabilize constructor.

typescript
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
example/logger.ts
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

typescript
export enum LogLevel { Debug, Info, Warn, Error } // 0, 1, 2, 3

level 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

typescript
logQuery(query: string, params: any[], executionTime?: number): void
logError(error: Error): void
logMetrics(metrics: PoolMetrics): void
logInfo(message: string): void
logWarn(message: string): void
logDebug(message: string): void

PoolMetrics is { activeConnections; idleConnections; totalConnections }.

  • logQuery SQL text, bound parameters, and optional execution time
  • logError An Error instance
  • logMetrics A PoolMetrics snapshot
  • 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.

logs
# With maxFiles: 3 and maxFileSize: 1MB
stabilize.log # active file
stabilize.log.1 # most recent rotation
stabilize.log.2
stabilize.log.3 # deleted on the next rotation