Skip to content

Logging ​

Runcobo logs through Crystal's standard Log module under the source name runcobo.

Access logs ​

Runcobo::LogHandler (part of the default handlers) logs one line per request:

text
INFO - runcobo: request_completed request_id=7c1f... method=GET path=/notes/1 status=200 elapsed_ms=0.412
  • The query string is not logged, so tokens and personal data in URLs stay out of your logs.
  • Failures are logged at error level with the exception attached, as request_failed or unhandled_exception.

Request IDs ​

When LogHandler is installed (it is by default), every request gets an ID:

  • If the client (or your load balancer) sends a non-empty X-Request-Id header, that value is used.
  • Otherwise a random UUID is generated.

The ID is returned in the X-Request-Id response header, available as request.request_id, and attached to the log context — so logs you write while handling the request carry it automatically:

crystal
module Posts
  Log = ::Log.for("posts")
end

class Posts::CreatePost < Blog::BaseAction
  post "/posts"

  call do
    input = validate(CreatePostContract)
    post = store.create_post(author_id: input.author_id, title: input.title)
    Posts::Log.info { "post_created id=#{post.id}" }   # carries request_id in its context
    render_json({id: post.id}, status_code: 201)
  end
end

Configuring output ​

Use the standard Log.setup in your entry point, before starting the server:

src/main.cr
crystal
require "./app"

Log.setup(:info)                              # default: info to STDOUT
Log.setup(:debug, Log::IOBackend.new(STDERR)) # more detail, to STDERR
Log.setup do |config|                         # per-source levels
  config.bind "*", :warn, Log::IOBackend.new
  config.bind "runcobo", :info, Log::IOBackend.new
end

Runcobo.start

Runcobo.logger(io, level: :info) is a shortcut for Log.setup(level, Log::IOBackend.new(io)).

To control the level with the LOG_LEVEL environment variable instead (DEBUG, INFO, WARN, ERROR, NONE), call Log.setup_from_env before starting the server. Without it, LOG_LEVEL has no effect.

crystal
Log.setup_from_env   # LOG_LEVEL=WARN hides the access log
Runcobo.start

Turning access logs off ​

Set SKIP_LOG=true to leave LogHandler out of the default handlers — handy in benchmarks and some test setups. Exceptions are still logged by ErrorHandler.

No LogHandler, no request IDs

LogHandler is also what assigns request IDs. Without it, request.request_id is nil, no X-Request-Id header is sent, and exception logs can't be correlated with requests. If you build your own handler list, keep Runcobo::LogHandler first.

Released under the MIT License.