A summary of data about the Ruby ecosystem.

https://github.com/roidrage/lograge

An attempt to tame Rails' default policy to log everything.
https://github.com/roidrage/lograge

Keywords from Contributors

activerecord mvc activejob crash-reporting rubygem rack rubocop rspec background-jobs static-code-analysis

Last synced: about 17 hours ago
JSON representation

Repository metadata

An attempt to tame Rails' default policy to log everything.

README.md

CI
Gem Version

Lograge - Taming Rails' Default Request Logging

Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable
and, in the context of running multiple processes and servers, unreadable
default logging output. Rails' default approach to log everything is great
during development, it's terrible when running it in production. It pretty much
renders Rails logs useless to me.

Lograge is a work in progress. I appreciate constructive feedback and criticism.
My main goal is to improve Rails' logging and to show people that they don't
need to stick with its defaults anymore if they don't want to.

Instead of trying solving the problem of having multiple lines per request by
switching Rails' logger for something that outputs syslog lines or adds a
request token, Lograge replaces Rails' request logging entirely, reducing the
output per request to a single line with all the important information, removing
all that clutter Rails likes to include and that gets mingled up so nicely when
multiple processes dump their output into a single file.

Instead of having an unparsable amount of logging output like this:

Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
  Rendered text template within layouts/application (0.0ms)
  Rendered layouts/_assets.html.erb (2.0ms)
  Rendered layouts/_top.html.erb (2.6ms)
  Rendered layouts/_about.html.erb (0.3ms)
  Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)

you get a single line with all the important information, like this:

method=GET path=/ format=json controller=HomeController action=index status=200 duration=79.0 view=78.8 db=0.0

The second line is easy to grasp with a single glance and still includes all the
relevant information as simple key-value pairs. The syntax is heavily inspired
by the log output of the Heroku router. It doesn't include any timestamp by
default, instead it assumes you use a proper log formatter instead.

Supported Ruby and Rails Releases

Lograge is actively tested against current and officially supported Ruby and
Rails releases. That said, Lograge should work with older releases.

  • Rails: Edge, 7.1, 7.0, 6.1, 6.0, 5.2
  • Rubies:
    • MRI: HEAD, 3.3, 3.2 3.1, 3.0, 2.7, 2.6
    • JRuby: HEAD, 9.2, 9.1
    • TruffleRuby: HEAD, 21.3

Installation

In your Gemfile

gem "lograge"

Enable it in an initializer or the relevant environment config:

# config/initializers/lograge.rb
# OR
# config/environments/production.rb
Rails.application.configure do
  config.lograge.enabled = true
end

If you're using Rails 5's API-only mode and inherit from
ActionController::API, you must define it as the controller base class which
lograge will patch:

# config/initializers/lograge.rb
Rails.application.configure do
  config.lograge.base_controller_class = 'ActionController::API'
end

If you use multiple base controller classes in your application, specify an array:

# config/initializers/lograge.rb
Rails.application.configure do
  config.lograge.base_controller_class = ['ActionController::API', 'ActionController::Base']
end

You can also add a hook for own custom data

# config/environments/staging.rb
Rails.application.configure do
  config.lograge.enabled = true

  # custom_options can be a lambda or hash
  # if it's a lambda then it must return a hash
  config.lograge.custom_options = lambda do |event|
    # capture some specific timing values you are interested in
    {:name => "value", :timing => some_float.round(2), :host => event.payload[:host]}
  end
end

Or you can add a timestamp:

Rails.application.configure do
  config.lograge.enabled = true

  # add time to lograge
  config.lograge.custom_options = lambda do |event|
    { time: Time.now }
  end
end

You can also keep the original (and verbose) Rails logger by following this configuration:

Rails.application.configure do
  config.lograge.keep_original_rails_log = true

  config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
end

You can then add custom variables to the event to be used in custom_options (available via the event.payload hash, which has to be processed in custom_options method to be included in log output, see above):

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def append_info_to_payload(payload)
    super
    payload[:host] = request.host
  end
end

Alternatively, you can add a hook for accessing controller methods directly (e.g. request and current_user).
This hash is merged into the log data automatically.

Rails.application.configure do
  config.lograge.enabled = true

  config.lograge.custom_payload do |controller|
    {
      host: controller.request.host,
      user_id: controller.current_user.try(:id)
    }
  end
end

To further clean up your logging, you can also tell Lograge to skip log messages
meeting given criteria. You can skip log messages generated from certain controller
actions, or you can write a custom handler to skip messages based on data in the log event:

# config/environments/production.rb
Rails.application.configure do
  config.lograge.enabled = true

  config.lograge.ignore_actions = ['HomeController#index', 'AController#an_action']
  config.lograge.ignore_custom = lambda do |event|
    # return true here if you want to ignore based on the event
  end
end

Lograge supports multiple output formats. The most common is the default
lograge key-value format described above. Alternatively, you can also generate
JSON logs in the json_event format used by Logstash.

# config/environments/production.rb
Rails.application.configure do
  config.lograge.formatter = Lograge::Formatters::Logstash.new
end

Note: When using the logstash output, you need to add the additional gem
logstash-event. You can simply add it to your Gemfile like this

gem "logstash-event"

Done.

The available formatters are:

  Lograge::Formatters::Lines.new
  Lograge::Formatters::Cee.new
  Lograge::Formatters::Graylog2.new
  Lograge::Formatters::KeyValue.new  # default lograge format
  Lograge::Formatters::KeyValueDeep.new
  Lograge::Formatters::Json.new
  Lograge::Formatters::Logstash.new
  Lograge::Formatters::LTSV.new
  Lograge::Formatters::Raw.new       # Returns a ruby hash object

In addition to the formatters, you can manipulate the data yourself by passing
an object which responds to #call:

# config/environments/production.rb
Rails.application.configure do
  config.lograge.formatter = ->(data) { "Called #{data[:controller]}" } # data is a ruby hash
end

Internals

Thanks to the notification system that was introduced in Rails 3, replacing the
logging is easy. Lograge unhooks all subscriptions from
ActionController::LogSubscriber and ActionView::LogSubscriber, and hooks in
its own log subscription, but only listening for two events: process_action
and redirect_to (in case of standard controller logs).
It makes sure that only subscriptions from those two classes
are removed. If you happened to hook in your own, they'll be safe.

Unfortunately, when a redirect is triggered by your application's code,
ActionController fires two events. One for the redirect itself, and another one
when the request is finished. Unfortunately, the final event doesn't include the
redirect, so Lograge stores the redirect URL as a thread-local attribute and
refers to it in process_action.

The event itself contains most of the relevant information to build up the log
line, including view processing and database access times.

While the LogSubscribers encapsulate most logging pretty nicely, there are still
two lines that show up no matter what. The first line that's output for every
Rails request, you know, this one:

Started GET "/" for 127.0.0.1 at 2012-03-12 17:10:10 +0100

And the verbose output coming from rack-cache:

cache: [GET /] miss

Both are independent of the LogSubscribers, and both need to be shut up using
different means.

For the first one, the starting line of every Rails request log, Lograge
replaces code in Rails::Rack::Logger to remove that particular log line. It's
not great, but it's just another unnecessary output and would still clutter the
log files. Maybe a future version of Rails will make this log line an event as
well.

To remove rack-cache's output (which is only enabled if caching in Rails is
enabled), Lograge disables verbosity for rack-cache, which is unfortunately
enabled by default.

There, a single line per request. Beautiful.

Action Cable

Starting with version 0.11.0, Lograge introduced support for Action Cable logs.
This proved to be a particular challenge since the framework code is littered
with multiple (and seemingly random) logger calls in a number of internal classes.
In order to deal with it, the default Action Cable logger was silenced.
As a consequence, calling logger e.g. in user-defined Connection or Channel
classes has no effect - Rails.logger (or any other logger instance)
has to be used instead.

Additionally, while standard controller logs rely on process_action and redirect_to
instrumentations only, Action Cable messages are generated from multiple events:
perform_action, subscribe, unsubscribe, connect, and disconnect.
perform_action is the only one included in the actual Action Cable code and
others have been added by monkey patching ActionCable::Channel::Base and
ActionCable::Connection::Base classes.

What it doesn't do

Lograge is opinionated, very opinionated. If the stuff below doesn't suit your
needs, it may not be for you.

Lograge removes ActionView logging, which also includes rendering times for
partials. If you're into those, Lograge is probably not for you. In my honest
opinion, those rendering times don't belong in the log file, they should be
collected in a system like New Relic, Librato Metrics or some other metrics
service that allows graphing rendering percentiles. I assume this for everything
that represents a moving target. That kind of data is better off being
visualized in graphs than dumped (and ignored) in a log file.

Lograge doesn't yet log the request parameters. This is something I'm actively
contemplating, mainly because I want to find a good way to include them, a way
that fits in with the general spirit of the log output generated by Lograge.
If you decide to include them be sure that sensitive data like passwords
and credit cards are not stored via filtered_parameters
or another means. The payload does already contain the params hash, so you can easily add
it in manually using custom_options:

# production.rb
YourApp::Application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    exceptions = %w(controller action format id)
    {
      params: event.payload[:params].except(*exceptions)
    }
  end
end

FAQ

Logging errors / exceptions

Our first recommendation is that you use exception tracking services built for
purpose ;)

If you absolutely must log exceptions in the single-line format, you can
do something similar to this example:

# config/environments/production.rb

YourApp::Application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    {
      exception: event.payload[:exception], # ["ExceptionClass", "the message"]
      exception_object: event.payload[:exception_object] # the exception instance
    }
  end
end

The :exception is just the basic class and message whereas the
:exception_object is the actual exception instance. You can use both /
either. Be mindful when including this, you will probably want to cherry-pick
particular attributes and almost definitely want to join the backtrace into
something without newline characters.

Handle ActionController::RoutingError

Add a get '*unmatched_route', to: 'application#route_not_found' rule to the end of your routes.rb
Then add a new controller action in your application_controller.rb.

def route_not_found
  render 'error_pages/404', status: :not_found
end

#146

Alternative & Related Projects

Contributing

See the CONTRIBUTING.md file for further information.

License

MIT. Code extracted from Travis CI.

(c) Mathias Meyer

See LICENSE.txt for details.


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 8 days ago

Total Commits: 363
Total Committers: 89
Avg Commits per committer: 4.079
Development Distribution Score (DDS): 0.76

Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0

Name Email Commits
Ben Lovell b****l@g****m 87
Mathias Meyer m****r@p****e 47
Andreas Tiefenthaler at@a****u 28
Ivy Evans i****y@i****t 19
Olle Jonsson o****n@g****m 17
Peter Suschlik ps@n****e 12
Michael Bianco i****y@g****m 10
Maximilian Schulz m****z@k****e 9
Holger Just h****t@m****e 8
Matt Button m****w@s****m 7
Smudge n****n@n****m 6
y-yagi y****a@g****m 5
Mariusz Hausenplas m****s@g****m 4
Jason Rohwedder j****o@b****m 4
Adam Cooper a****r@g****m 4
dependabot[bot] 4****] 3
YOSHIKI y****g@g****m 3
Max Schwenk m****k@g****m 3
Laust Rud Jacobsen l****t@o****o 3
Joe Marty j****e@o****m 3
Ari Pollak a****i@t****m 3
Jim Blomo j****b@g****m 3
JP Slavinsky j****v@g****m 2
Hans Hasselberg me@h****o 2
Alex Ghiculescu a****x@t****o 2
Justin Bull j****n@w****m 2
Takashi Nakagawa t****0@g****m 2
Tilmann Singer t****s@t****t 2
Vladimir Dementyev d****m@g****m 2
jbbarth j****h@g****m 2
and 59 more...

Committer domains:


Issue and Pull Request metadata

Last synced: 29 days ago

Total issues: 59
Total pull requests: 86
Average time to close issues: about 1 year
Average time to close pull requests: 10 months
Total issue authors: 58
Total pull request authors: 53
Average comments per issue: 4.08
Average comments per pull request: 1.52
Merged pull request: 39
Bot issues: 0
Bot pull requests: 5

Past year issues: 0
Past year pull requests: 3
Past year average time to close issues: N/A
Past year average time to close pull requests: less than a minute
Past year issue authors: 0
Past year pull request authors: 2
Past year average comments per issue: 0
Past year average comments per pull request: 0.33
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/roidrage/lograge

Top Issue Authors

  • waghanza (2)
  • ivy (1)
  • mhenrixon (1)
  • erik-brueggemann (1)
  • mkarganov (1)
  • davetron5000 (1)
  • earnold (1)
  • dmolesUC (1)
  • md5 (1)
  • wongy91 (1)
  • fieldse (1)
  • thelinuxlich (1)
  • wjessop (1)
  • sirwolfgang (1)
  • owst (1)

Top Pull Request Authors

  • y-yagi (10)
  • olleolleolle (6)
  • dependabot[bot] (5)
  • ytkg (3)
  • yahonda (2)
  • ohbarye (2)
  • ghiculescu (2)
  • Supy (2)
  • tombruijn (2)
  • johnkennedy-code (2)
  • palkan (2)
  • waghanza (2)
  • benoittgt (2)
  • benlovell (2)
  • kmeyerhofer (2)

Top Issue Labels

  • enhancement (3)
  • question (2)
  • bug (2)

Top Pull Request Labels

  • dependencies (5)
  • github_actions (5)
  • enhancement (1)

Package metadata

gem.coop: lograge

Tame Rails' multi-line logging into a single line per request

  • Homepage: https://github.com/roidrage/lograge
  • Documentation: http://www.rubydoc.info/gems/lograge/
  • Licenses: MIT
  • Latest release: 0.14.0 (published about 2 years ago)
  • Last Synced: 2025-12-12T07:45:17.606Z (2 days ago)
  • Versions: 42
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 173,390,481 Total
  • Docker Downloads: 531,350,939
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 0.083%
    • Downloads: 0.145%
    • Docker downloads count: 0.187%
  • Maintainers (3)
rubygems.org: lograge

Tame Rails' multi-line logging into a single line per request

  • Homepage: https://github.com/roidrage/lograge
  • Documentation: http://www.rubydoc.info/gems/lograge/
  • Licenses: MIT
  • Latest release: 0.14.0 (published about 2 years ago)
  • Last Synced: 2025-12-12T01:02:34.626Z (3 days ago)
  • Versions: 42
  • Dependent Packages: 59
  • Dependent Repositories: 4,136
  • Downloads: 173,373,119 Total
  • Docker Downloads: 531,350,939
  • Rankings:
    • Downloads: 0.147%
    • Docker downloads count: 0.251%
    • Dependent packages count: 0.472%
    • Dependent repos count: 0.487%
    • Average: 0.589%
    • Stargazers count: 0.616%
    • Forks count: 1.561%
  • Maintainers (3)
proxy.golang.org: github.com/roidrage/lograge

  • Homepage:
  • Documentation: https://pkg.go.dev/github.com/roidrage/lograge#section-documentation
  • Licenses: mit
  • Latest release: v0.14.0 (published about 2 years ago)
  • Last Synced: 2025-12-10T22:02:34.398Z (4 days ago)
  • Versions: 32
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent packages count: 6.515%
    • Average: 6.733%
    • Dependent repos count: 6.952%
gem.coop: lograge-with-time

Tame Rails' multi-line logging into a single line per request

  • Homepage: https://github.com/roidrage/lograge
  • Documentation: http://www.rubydoc.info/gems/lograge-with-time/
  • Licenses: MIT
  • Latest release: 0.4.0 (published about 11 years ago)
  • Last Synced: 2025-12-10T22:02:32.429Z (4 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 14,058 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 12.424%
    • Downloads: 37.273%
  • Maintainers (1)
rubygems.org: lograge-with-time

Tame Rails' multi-line logging into a single line per request

  • Homepage: https://github.com/roidrage/lograge
  • Documentation: http://www.rubydoc.info/gems/lograge-with-time/
  • Licenses: MIT
  • Latest release: 0.4.0 (published about 11 years ago)
  • Last Synced: 2025-12-10T22:02:33.146Z (4 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 14,058 Total
  • Rankings:
    • Stargazers count: 0.61%
    • Forks count: 1.526%
    • Average: 15.302%
    • Dependent packages count: 15.551%
    • Dependent repos count: 21.776%
    • Downloads: 37.05%
  • Maintainers (1)

Dependencies

.github/workflows/ci.yml actions
  • actions/checkout v3 composite
  • ruby/setup-ruby v1 composite
.github/workflows/codeql-analysis.yml actions
  • actions/checkout v3 composite
  • github/codeql-action/analyze v2 composite
  • github/codeql-action/autobuild v2 composite
  • github/codeql-action/init v2 composite
Gemfile rubygems
  • actionpack ~> 6 development
  • activerecord ~> 6 development
  • jrjackson ~> 0.2.9 development
  • lines >= 0 development
  • logstash-event >= 0 development
  • pry >= 0 development
  • thread_safe >= 0 development
lograge.gemspec rubygems
  • rspec ~> 3.1 development
  • rubocop ~> 1.23 development
  • simplecov ~> 0.21 development
  • actionpack >= 4
  • activesupport >= 4
  • railties >= 4
  • request_store ~> 1.0

Score: 33.74798328167577