Skip to main content
Sonamu uses LogTape to provide structured logging. You can configure logging behavior with the logging option in sonamu.config.ts.

What is LogTape?

LogTape is a structured logging library for TypeScript. Instead of simple string logs, it records logs as structured data.

Regular Logging vs Structured Logging

Regular logging (string):
Structured logging (LogTape):

Benefits of Structured Logging

  1. Easy search/filtering - Easily find logs matching specific conditions
  2. Analyzable - Aggregate log data and generate statistics
  3. Type safe - TypeScript validates log data structure
  4. Flexible output - Output same log in multiple formats (console, file, external services)

Core Concepts

1. Sink (Output Destination)

Where logs are ultimately recorded.
A single log can be recorded to multiple Sinks simultaneously:
  • Console: Real-time monitoring
  • File: Permanent storage
  • Sentry: Error notifications

2. Filter (Filtering Conditions)

Determines which logs to record.
Without Filters, all logs are output, degrading performance and readability.

3. Logger (Logger Configuration)

Connects categories, Sinks, and Filters.

Sink-Filter-Logger Relationship

Example:

Log Level Selection Guide

Log levels indicate the importance of logs.

Meaning and Usage of Each Level

1. debug
  • Meaning: Detailed debugging information
  • When to use: Problem tracking during development
  • Examples: Function call order, variable values
2. info (default)
  • Meaning: General operational information
  • When to use: Confirming normal operation
  • Examples: HTTP requests/responses, task completion
3. warning
  • Meaning: Potential problem warning
  • When to use: Not an error but needs attention
  • Examples: Slow responses, retries occurring
4. error
  • Meaning: Recoverable error
  • When to use: Exception handling, recoverable failures
  • Examples: Validation failure, API call failure
5. fatal
  • Meaning: Critical failure (server shutdown level)
  • When to use: Unrecoverable serious problems
  • Examples: DB connection failure, out of memory
Level Hierarchy:
Setting a lower level outputs all higher levels. Example: Setting lowestLevel: "warning" outputs warning, error, and fatal.

Why is it Designed This Way?

1. Default Values are Safe

Sonamu’s default configuration:
Reasons:
  • ✅ Only /api paths logged → Excludes unnecessary static file requests
  • info level → Not too much, not too little
  • ✅ Excludes healthcheck → Excludes repetitive requests from monitoring systems

2. Extensible

You can add custom settings on top of default settings:

3. Minimal Performance Impact

Block unnecessary logs early through Filters:

Default Configuration

If you omit the logging option, default settings are automatically applied.
Default behavior:
  • Logs Fastify requests/responses to console
  • Only logs /api/* paths (excluding healthcheck)
  • Pretty format output (timestamp, category, level)

logging Options

Disable Logging

fastifyCategory

Specifies the category to use for Fastify HTTP logging. Type: readonly string[] Default: ["fastify"]
Category format: ["a", "b", "c"] array represents hierarchy Log output example:

sinks

Add destinations (sinks) for log output. Type: Record<string, Sink>
The name "fastify-console" is the default sink automatically generated by Sonamu. Adding with this name will overwrite the default sink.

filters

Add conditions for selective log filtering. Type: Record<string, FilterLike>
The name "fastify-console" is the default filter automatically generated by Sonamu. Adding with this name will overwrite the default filter.

loggers

Configure which sinks and filters to use per category. Type: LoggerConfig[]
If there’s a logger configuration for the category set in fastifyCategory, Sonamu won’t add the default logger.

Basic Examples

Minimal Configuration (Using Defaults)

Disable Logging

Custom Category

Default Behavior Details

Sonamu automatically configures logging as follows:

1. Fastify Sink Auto-Generation

Features:
  • Shows HTTP method and response code: [GET:200]
  • Shows request URL: /api/user/list
  • Colors distinguish levels

2. Fastify Filter Auto-Generation

3. Logger Auto-Generation

4. Meta Logger Disabled

Log Levels

LogTape log levels (in ascending order):
  1. debug - Detailed debugging information
  2. info - General information (default)
  3. warning - Warning
  4. error - Error
  5. fatal - Fatal error

Practical Examples

Features:
  • debug level to check all details
  • Console output for real-time viewing
  • Fast feedback loop

Adding File Logging

Production Configuration

Fastify Logging Auto-Integration

Sonamu automatically integrates Fastify logging with the @logtape/fastify package.
Automatically logged information:
  • HTTP requests (method, URL)
  • Response codes
  • Response times
  • Error stack traces

Important Notes

1. Overwriting Default sink/filter

2. logger Option When Logging is Disabled

3. Category Consistency

Next Steps

Sinks & Filters

Control log output precisely with custom Sinks and Filters

Category Logging

Manage logs systematically with the category system

Fastify Logging

Customize HTTP request/response logging

LogTape Documentation

Learn advanced features in the official LogTape documentation