Semantic Logger

Comprehensive logging interface for Ruby and Rails. Simultaneously outputs human-readable colorful text logs and machine-readable JSON logs. Achieves high-performance processing (thousands of lines per second) through simultaneous writing to multiple destinations and in-memory queues.

loggingRubyRailsstructured-logginghigh-performance

Logging Library

Semantic Logger

Overview

Semantic Logger is a comprehensive logging interface for Ruby and Rails. It simultaneously outputs human-readable colorful text logs and machine-readable JSON logs, achieving high-performance processing (thousands of lines per second) through simultaneous writing to multiple destinations and in-memory queues.

Details

Semantic Logger is a next-generation logging framework designed to overcome the limitations of traditional Ruby logging libraries. A key feature is log processing in a dedicated thread, achieving high throughput without affecting the main application processing.

Key Features

  • Multiple Output Support: Dedicated appenders for Sentry, syslog, DataDog, Splunk, Elasticsearch, MongoDB, etc.
  • Structured Logging: Flexible output formats with JSON, color, and custom formatters
  • Asynchronous Processing: High performance through log processing in separate threads
  • Rails Integration: Seamless Rails integration through rails_semantic_logger
  • Diverse Appenders: Rich output destinations including HTTP, TCP, UDP, Kafka, RabbitMQ

Pros and Cons

Pros

  • High Performance: Capable of processing thousands of lines per second with in-memory queues and asynchronous processing
  • Rich Output Destinations: Support for various services with 15+ built-in appenders
  • Structured Logging: Machine-readable log format through JSON output
  • Configuration Flexibility: Detailed configuration and filtering capabilities per appender
  • Rails Integration: Easy adoption into existing Rails applications
  • Metrics Features: Built-in performance measurement and metrics collection

Cons

  • Learning Curve: Initial configuration can be complex due to rich feature set
  • Memory Usage: Queues for asynchronous processing consume memory
  • Dependencies: Some appenders require additional gems

Reference Pages

Usage Examples

Basic Configuration

require 'semantic_logger'

# Set global default log level
SemanticLogger.default_level = :trace

# Add file appender with colorized formatter
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)

# Create logger instance
logger = SemanticLogger['MyClass']
logger.info "Hello World"

Multiple Appender Configuration

# Simultaneous output to file and Syslog server
SemanticLogger.add_appender(file_name: "development.log", formatter: :color)
SemanticLogger.add_appender(appender: :syslog, url: "tcp://myloghost:514")

# JSON format file output
SemanticLogger.add_appender(file_name: "app.json", formatter: :json)

Structured Logging Usage

logger = SemanticLogger['UserService']

# Logging with structured data
logger.info "User login", 
  user_id: 12345, 
  ip_address: "192.168.1.100",
  user_agent: "Mozilla/5.0..."

# Performance measurement with metrics
logger.measure_info "Database query" do
  User.find(12345)
end

Custom Appender Implementation

class SimpleAppender < SemanticLogger::Subscriber
  def initialize(host: nil, **args, &block)
    @host = host
    super(**args, &block)
  end

  def log(log)
    # Display log structure
    p log
    
    # Display formatted output
    puts formatter.call(log)
  end

  def flush
    puts "Flush executed"
  end

  def close
    puts "Appender closing"
  end
end

# Add custom appender
SemanticLogger.add_appender(appender: SimpleAppender.new)

Per-Appender Log Level Configuration

# Output warning level and above to warnings.log
SemanticLogger.add_appender(file_name: "log/warnings.log", level: :warn)

# Output trace level and above to trace.log
SemanticLogger.add_appender(file_name: "log/trace.log", level: :trace)

logger = SemanticLogger["MyClass"]
logger.level = :trace
logger.trace "Detailed trace information"
logger.warn "Warning message"

External Service Integration

# Elasticsearch integration
SemanticLogger.add_appender(
  appender:    :elasticsearch,
  url:         "http://localhost:9200",
  index:       "my-app-logs",
  data_stream: true
)

# Sentry integration
SemanticLogger.add_appender(appender: :sentry_ruby)

# HTTP endpoint integration
SemanticLogger.add_appender(
  appender: :http,
  url:      "https://api.example.com/logs",
  formatter: :json
)