Skip to content

Loggical API Documentation v1.0.1


Loggical API Documentation / Logger

Class: Logger

Defined in: core/logger.ts:20

Logger class for structured logging with colors and levels

Constructors

Constructor

ts
new Logger(options: LoggerOptions): Logger;

Defined in: core/logger.ts:82

Create a new logger with options

Parameters

ParameterTypeDescription
optionsLoggerOptionsLogger configuration options

Returns

Logger

Examples

typescript
// Default logger
const logger = new Logger()

// Use preset with light customization
const apiLogger = new Logger({
  preset: 'compact',
  prefix: 'API',
  minLevel: LogLevel.WARN
})
typescript
// Full control over all options
const customLogger = new Logger({
  colorLevel: ColorLevel.ENHANCED,
  timestamped: true,
  compactObjects: false,
  maxValueLength: 200,
  transports: [new ConsoleTransport(), new FileTransport({ filename: 'app.log' })]
})

Methods

create()

ts
static create(options?: LoggerOptions): Logger;

Defined in: core/logger.ts:111

Static factory method for creating a logger with default configuration

Parameters

ParameterType
options?LoggerOptions

Returns

Logger


compact()

ts
static compact(options?: Partial<LoggerOptions>): Logger;

Defined in: core/logger.ts:118

Static factory method for creating a compact logger

Parameters

ParameterType
options?Partial<LoggerOptions>

Returns

Logger


readable()

ts
static readable(options?: Partial<LoggerOptions>): Logger;

Defined in: core/logger.ts:125

Static factory method for creating a readable logger

Parameters

ParameterType
options?Partial<LoggerOptions>

Returns

Logger


server()

ts
static server(options?: Partial<LoggerOptions>): Logger;

Defined in: core/logger.ts:132

Static factory method for creating a server logger

Parameters

ParameterType
options?Partial<LoggerOptions>

Returns

Logger


development()

ts
static development(options?: Partial<LoggerOptions>): Logger;

Defined in: core/logger.ts:139

Static factory method for creating a development logger

Parameters

ParameterType
options?Partial<LoggerOptions>

Returns

Logger


debug()

ts
debug(...messages: unknown[]): void;

Defined in: core/logger.ts:205

Log debug messages - lowest priority, typically disabled in production

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.debug('Processing user data', { userId: '123', step: 'validation' })
logger.debug('Cache hit', { key: 'user:123', ttl: 300 })

info()

ts
info(...messages: unknown[]): void;

Defined in: core/logger.ts:221

Log informational messages - general application flow and events

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.info('User login successful', { userId: '123', timestamp: new Date() })
logger.info('Request processed', { method: 'POST', url: '/api/users', duration: '150ms' })

warn()

ts
warn(...messages: unknown[]): void;

Defined in: core/logger.ts:237

Log warning messages - potentially harmful situations that don't stop execution

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.warn('API rate limit approaching', { current: 95, limit: 100 })
logger.warn('Deprecated feature used', { feature: 'oldApi', alternative: 'newApi' })

error()

ts
error(...messages: unknown[]): void;

Defined in: core/logger.ts:253

Log error messages - error conditions that don't require immediate termination

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.error('Database connection failed', { error: err.message, retryCount: 3 })
logger.error('Payment processing error', { orderId: '12345', gateway: 'stripe' })

highlight()

ts
highlight(...messages: unknown[]): void;

Defined in: core/logger.ts:269

Log highlighted messages - important information that needs attention

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.highlight('System maintenance starting', { duration: '30min', affectedServices: ['api', 'db'] })
logger.highlight('New feature deployed', { version: '2.1.0', features: ['auth', 'payments'] })

fatal()

ts
fatal(...messages: unknown[]): void;

Defined in: core/logger.ts:284

Log fatal error messages - unrecoverable errors

Parameters

ParameterTypeDescription
...messagesunknown[]The message string and optional data objects to log

Returns

void

Example

typescript
logger.fatal('Database connection lost', { host: 'db.example.com', lastPing: '30s ago' })

getOptions()

ts
getOptions(): LoggerOptions;

Defined in: core/logger.ts:290

Returns

LoggerOptions


withPrefix()

ts
withPrefix(prefix: string): Logger;

Defined in: core/logger.ts:317

Create a new logger with an additional prefix

Prefixes are displayed before each log message to identify the source or component. They can be automatically abbreviated if abbreviatePrefixes is enabled.

Parameters

ParameterTypeDescription
prefixstringThe prefix to add (e.g., 'API', 'DATABASE', 'AUTH-SERVICE')

Returns

Logger

A new Logger instance with the additional prefix

Example

typescript
const logger = new Logger()
const apiLogger = logger.withPrefix('API-SERVER')
const authLogger = apiLogger.withPrefix('AUTH')

authLogger.info('User authenticated')
// Output: [timestamp] ℹ️ [API-SERVER] [AUTH] User authenticated

withContext()

ts
withContext(context: string | Record<string, unknown>, value?: unknown): Logger;

Defined in: core/logger.ts:376

Add context that will appear in all subsequent log messages

Context is persistent data that automatically appears in every log message from this logger instance. This is particularly useful for request-scoped logging where you want to include request ID, user ID, etc.

Parameters

ParameterTypeDescription
contextstring | Record<string, unknown>Either a key string or an object with key-value pairs
value?unknownThe value when using key-value syntax

Returns

Logger

A new Logger instance with the added context

Examples

typescript
// Key-value syntax
const userLogger = logger.withContext('userId', 'user-123')

// Object syntax
const requestLogger = logger.withContext({
  requestId: 'req-456',
  method: 'POST',
  ip: '192.168.1.1'
})

fullLogger.info('Processing request')
// Output: [...] Processing request { requestId: "req-456", method: "POST", ip: "192.168.1.1" }

View full context example

javascript
// User Session Tracking
const sessionLogger = logger.withContext({
  userId: 'user-12345',
  sessionId: 'sess-abc-123',
  role: 'admin'
})

sessionLogger.info('User session started')

// Request/Response Cycle
const requestLogger = sessionLogger.withContext({
  requestId: 'req-def-456',
  method: 'POST',
  endpoint: '/api/users'
})

requestLogger.info('Incoming request')
requestLogger.info('Request processed successfully')

withoutContext()

ts
withoutContext(): Logger;

Defined in: core/logger.ts:396

Remove all context from the logger

Returns

Logger

A new Logger instance with no context


withoutContextKey()

ts
withoutContextKey(key: string): Logger;

Defined in: core/logger.ts:410

Remove a specific context key

Parameters

ParameterTypeDescription
keystringThe context key to remove

Returns

Logger

A new Logger instance without the specified context key


getContext()

ts
getContext(): Record<string, unknown>;

Defined in: core/logger.ts:426

Get current context as a plain object

Returns

Record<string, unknown>

Object with current context key-value pairs


addTransport()

ts
addTransport(transport: Transport): this;

Defined in: core/logger.ts:436

Add a transport to this logger

Parameters

ParameterTypeDescription
transportTransportThe transport to add

Returns

this


removeTransport()

ts
removeTransport(transportName: string): this;

Defined in: core/logger.ts:445

Remove a transport by name

Parameters

ParameterTypeDescription
transportNamestringName of the transport to remove

Returns

this


getTransport()

ts
getTransport(transportName: string): undefined | Transport;

Defined in: core/logger.ts:454

Get transport by name

Parameters

ParameterTypeDescription
transportNamestringName of the transport

Returns

undefined | Transport


getTransports()

ts
getTransports(): Transport[];

Defined in: core/logger.ts:461

Get all transports

Returns

Transport[]


clearTransports()

ts
clearTransports(): this;

Defined in: core/logger.ts:468

Clear all transports

Returns

this


getTransportStatus()

ts
getTransportStatus(): Record<string, unknown>[];

Defined in: core/logger.ts:476

Get status of all transports

Returns

Record<string, unknown>[]


installPlugin()

ts
installPlugin(plugin: Plugin): Promise<void>;

Defined in: core/logger.ts:486

Install a plugin

Parameters

ParameterTypeDescription
pluginPluginThe plugin to install

Returns

Promise<void>


uninstallPlugin()

ts
uninstallPlugin(name: string): Promise<void>;

Defined in: core/logger.ts:494

Uninstall a plugin by name

Parameters

ParameterTypeDescription
namestringThe name of the plugin to uninstall

Returns

Promise<void>


getPlugins()

ts
getPlugins(): Plugin[];

Defined in: core/logger.ts:502

Get all installed plugins

Returns

Plugin[]

Array of installed plugins


hasPlugin()

ts
hasPlugin(name: string): boolean;

Defined in: core/logger.ts:511

Check if a plugin is installed

Parameters

ParameterTypeDescription
namestringThe name of the plugin to check

Returns

boolean

True if the plugin is installed


close()

ts
close(): Promise<void>;

Defined in: core/logger.ts:532

Close all transports and cleanup

Returns

Promise<void>

Released under the MIT License.