Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
228: Structured Logging with Semantic Logger
Imagine you're running a massive warehouse. In the old days, your staff kept a "daily log" in a leather-bound notebook. They'd write things like, "Tuesday, 2 PM: A palette of blue widgets fell over in aisle 4, looks like the forklift driver was rushing." It's descriptive, sure, but if you want to find out how many palettes fell in aisle 4 over the last six months, you have to read every single page and manually tally them up. It's a nightmare.
Now, imagine replacing that notebook with a digital spreadsheet. Instead of a sentence, the worker fills out columns: [Date] [Time] [Event: Palette Drop] [Location: Aisle 4] [Item: Blue Widgets] [Cause: Speeding]. Suddenly, you can filter, sort, and generate a report on aisle 4's safety record in three seconds. That is exactly the shift we're making when we move from standard Ruby logging to structured logging with the semantic_logger gem.
The Nightmare of Grepping Text Files
Most of us start with the standard Ruby Logger. It's fine for small scripts, but in a production environment, it produces "unstructured" data—just long strings of text. I've spent way too many late nights using grep and awk to try and parse through 500MB of text files just to find a specific correlation ID for a failing request. It's tedious and error-prone.
Semantic Logger changes the game by treating a log entry as an event rather than a string. Instead of interpolating variables into a sentence, you pass them as a payload. This allows the logger to output the data in a format (like JSON) that tools like Elasticsearch, Datadog, or Splunk can index instantly.
Moving from Strings to Payloads
Let's look at how this actually looks in code. If you were logging a failed payment in a standard logger, you'd probably do something like this:
logger.error "Payment failed for user #{user_id} on order #{order_id}: #{error_message}"
That's a nightmare to parse. With Semantic Logger, we separate the message (which stays constant) from the payload (which changes). Here is how I'd write that same log:
# First, we set up the logger to output JSON to the console
SemanticLogger.add_appender(io: STDOUT, formatter: :json)
logger = SemanticLogger['PaymentProcessor']
# Now we log the event with a hash of data
logger.error "Payment failed",
user_id: user_id,
order_id: order_id,
error: error_message,
amount: 49.99
Notice the difference? The message is just "Payment failed". The metadata is a hash. When this hits your log aggregator, you don't search for the string "Payment failed for user 123"; you query for json.user_id == 123 AND level == 'error'. It's a massive jump in efficiency.
Routing Your Logs to Multiple Destinations
One thing I love about Semantic Logger is the concept of "Appenders." In the standard library, you're usually stuck sending logs to one place (a file or STDOUT). In a real-world app, you often want your logs in multiple places: a local file for quick debugging and a remote service for long-term analysis.
You can add as many appenders as you need without changing your business logic. You just configure them at startup:
# Log to a file in a human-readable format for local dev
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)
# Simultaneously stream JSON to a TCP socket for a log aggregator
SemanticLogger.add_appender(tcp_client: 'log-collector.internal:5000', formatter: :json)
Now, every time you call logger.info, the gem handles the heavy lifting of formatting that data differently for each destination. Your code stays clean, and your operations team stays happy.
📋 Practical Task
Building a Structured Order Audit Log
You are tasked with replacing a legacy logging system for an e-commerce checkout process. The current system uses string interpolation, making it impossible to track how many orders are failing due to "Out of Stock" issues versus "Payment Declined" issues.
Your Goal: Create a script that uses semantic_logger to log a series of order attempts.
- Configure the logger to output JSON to STDOUT.
- Create a loop that simulates processing three different orders.
- For each order, log an
infomessage "Processing Order" and anerrormessage "Order Failed" if the order fails. - The
errorlog must include a payload with:order_id,customer_email, and areason(e.g., :insufficient_funds, :out_of_stock).
Verify that your output is valid JSON, meaning you should see curly braces {} and key-value pairs in your console, not just plain sentences.
There are no comments for now.