Lograge

repository·master·Indexed 25 days ago

https://github.com/roidrage/lograge

A tool for customizing and simplifying Rails request logging by replacing verbose default logs with structured, single-line logs in formats such as JSON, Key-Value, LTSV, and Graylog2. It provides configurations for custom options, log transformation via before_format, action filtering, and integration with log aggregators.

Tokens
788
Snippets
0
Records
9
Agent score
86%

What's inside lograge

  1. Initialize Lograge in a Rails application

    master
    Call Lograge.setup(app) within your application configuration to initialize the library. This handles attaching to ActionController (and ActionCable if defined), setting up custom payloads, and configuring formatters based on your application's config.lograge settings.
  2. Configure custom options for log lines

    master

    You can append custom data to every log line using Lograge.custom_options.

    Supported formats:

    • A Hash of options to be merged into the log.
    • Any object that responds to .call(event) and returns a Hash (where event is the log event).

    Note: If you provide a callable, it will be executed for every event.

  3. Set the log level and formatter

    master

    Configure the global logging behavior using Lograge.log_level and Lograge.formatter.

    • log_level: Sets the log level for emitted lines (defaults to :info).
    • formatter: Sets the output format. Supported formats include :lograge and :logstash. You can also pass an instance of a formatter class (e.g., Lograge::Formatters::Json.new).
  4. Transform log output with `before_format`

    master

    The before_format configuration allows you to modify the structure of the log data before it is formatted into the final string.

    You must pass a callable (e.g., a proc or lambda) that accepts two arguments: data (the current hash of log data) and payload (the original event payload). The callable should return the modified data hash. If the callable returns nil, the original data is used.

  5. Use the Graylog2 log formatter

    master
    The Lograge::Formatters::Graylog2 formatter prepares log data for Graylog2 by ensuring all keys follow the GELF (Graylog Extended Log Format) additional field syntax. It prefixes every key with an underscore (_) and generates a short_message field containing the HTTP status, method, path, and controller/action information.
  6. Ignore events using a custom predicate

    master

    For more complex filtering, use Lograge.ignore(test) where test is an object that responds to .call(event) and returns true if the event should be ignored.

    You can also use Lograge.ignore_nothing to clear all current ignore conditions.

  7. Ignore specific controller actions

    master

    To prevent certain controller actions from being logged, use Lograge.ignore_actions.

    Supported formats:

    • A single string representing the controller action in the format 'ControllerName#action_name' (e.g., 'UsersController#sign_in').
    • An array of such strings.

    Calling this method multiple times appends new ignore conditions to the existing list.

  8. Use the LTSV log formatter

    master

    The Lograge::Formatters::LTSV class provides a formatter that outputs logs in Labeled Tab-Separated Values (LTSV) format. Each log entry consists of key:value pairs separated by tabs (\t).

    Special handling for specific data types:

    • Errors: If a key is named :error, the value is escaped and wrapped in single quotes ('value') to prevent parsing ambiguity.
    • Floats: Float values are formatted to two decimal places (%.2f).
    • Escaping: The formatter automatically escapes backslashes (\\), newlines (\n), carriage returns (\r), and tabs (\t) within string values.