https://github.com/xuwupeng2000/json-logger
为了确保在 Rails 服务中的日志格式一致性,通常需要使用 JSON 日志记录器,特别是在与 Kibana 等日志服务一起工作时。这个宝石提供了一个预定义的 JSON 日志格式,专门为 Rails 应用程序定制。通过将此宝石添加到您的 Gemfile 中,您可以轻松实现 Rails 服务的日志一致性。
一个用于 Rails 的 JSON 日志记录器,该日志记录器是基于 lograge 和 ougai 构建的。它作为一个 Rails 引擎运行,无需任何额外的配置即可无缝工作。只需将其添加为一个 gem 即可启用其功能。
我研发这个项目的初衷是因为我有很多的微服务,我想让他们以一致的 format 输出 json 日志。
{"@timestamp":"2024-02-28T09:07:58.098Z","message":" ActiveRecord::SchemaMigration Load (0.1ms) SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
{"@timestamp":"2024-02-28T09:07:58.121Z","message":" Rendering /Users/jawu/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/templates/rails/welcome/index.html.erb","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
{"@timestamp":"2024-02-28T09:07:58.122Z","message":"Rails Request","log":{"level":"INFO","logger":"Rails"},"trace":{"id":null},"error":{},"request":{"method":"GET","body":{"content":"{\"controller\"=>\"rails/welcome\", \"action\"=>\"index\"}"}},"response":{"status_code":200},"process":{"id":45821},"url":{"path":"/"},"event":{"action":"Rails::WelcomeController#index","duration":6.79}}
{"@timestamp":"2024-02-28T09:07:59.588Z","message":" Rendering /Users/jawu/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/templates/rails/welcome/index.html.erb","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
{"@timestamp":"2024-02-28T09:07:59.588Z","message":"Rails Request","log":{"level":"INFO","logger":"Rails"},"trace":{"id":null},"error":{},"request":{"method":"GET","body":{"content":"{\"controller\"=>\"rails/welcome\", \"action\"=>\"index\"}"}},"response":{"status_code":200},"process":{"id":45821},"url":{"path":"/"},"event":{"action":"Rails::WelcomeController#index","duration":1.82}}