Lucky Logo


You can log messages by calling debug, info, warn, or error on Lucky.logger. You can pass in a string or a NamedTuple:

# Log formatter will receive { message: "My message" }"My message")
# Log formatter will receive { foo: "bar" } "bar")

By default, Lucky formats the log output to look like GET 200 / TIMESTAMP (27.0ยตs). This is broken down in to several parts:

#Logger Options

You can change the logger configuration in config/

# Show/Hide the current timestamp of the request. By default this is shown only
# in production
show_timestamps : Bool

# Customize how your log is formatted, and what information it shows. By
# default this is the `DefaultLogFormatter`
log_formatter : Lucky::LogFormatters::Base

# An option to turn on/off logging. By default logging is enabled except for
# when running tests.
enabled : Bool

#Log Formatters

To customize your logging output, you can create a custom Log Formatter. Open your config/ file to add this option:

Lucky::LogHandler.configure do |settings|
  settings.show_timestamps = Lucky::Env.production?
  settings.log_formatter =

Note: Be sure to require this new formatter in your stack. Typically this is done src/

The structure for the formatters is a class that inherits from Lucky::LogFormatters::Base and defines the format(context, time, elapsed) : String method. Take a look at this example:

class MyCustomFormatter < Lucky::LogFormatters::Base
  def format(context, time, elapsed)
    case context.response.status_code
    when 200..399
    when 400..499
    when 500..599

This method gives you access to the current context : HTTP::Server::Context which contains the request and response objects, time : Time which is the current timestamp, and elapsed : Time::Span which is the amount of time the entire request took to complete.

Lucky has 3 helper methods you can use for some helpful formatting as well.

# Returns the status_code colorized with green, yellow, or red text based on the range of the code

# Formats the time to ISO_8601_DATE_TIME if the `show_timestamps` option is set to true.

# Does some fancy math to display the elapsed span in a human readable format