{"id":17020557,"url":"https://github.com/krisrang/heavylog","last_synced_at":"2025-10-08T17:23:19.543Z","repository":{"id":31319948,"uuid":"127542724","full_name":"krisrang/heavylog","owner":"krisrang","description":"JSON request logging for Rails","archived":false,"fork":false,"pushed_at":"2024-03-04T09:25:41.000Z","size":172,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-26T12:19:38.451Z","etag":null,"topics":["logging","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krisrang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-03-31T15:05:30.000Z","updated_at":"2024-01-12T18:06:16.000Z","dependencies_parsed_at":"2024-03-04T10:49:05.972Z","dependency_job_id":null,"html_url":"https://github.com/krisrang/heavylog","commit_stats":{"total_commits":95,"total_committers":2,"mean_commits":47.5,"dds":"0.10526315789473684","last_synced_commit":"4a7f2cad290ffe5c4a9c1f9ef3d066fbe97d276a"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisrang%2Fheavylog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisrang%2Fheavylog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisrang%2Fheavylog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisrang%2Fheavylog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krisrang","download_url":"https://codeload.github.com/krisrang/heavylog/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243447639,"owners_count":20292453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["logging","rails","ruby"],"created_at":"2024-10-14T07:05:25.334Z","updated_at":"2025-10-08T17:23:14.508Z","avatar_url":"https://github.com/krisrang.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Heavylog [![Test](https://github.com/krisrang/heavylog/actions/workflows/test.yml/badge.svg)](https://github.com/krisrang/heavylog/actions/workflows/test.yml) [![Gem Version](https://badge.fury.io/rb/heavylog.svg)](https://badge.fury.io/rb/heavylog)\n\nHeavylog enables you to log all Rails requests to file as JSON or any other format you want.\n\nAside from metadata about requests full output is also included like SQL/Rails logging and `puts` statements.  \nExample request using the JSON formatter:\n```\n{\"request_id\":\"e2cdef0a-9851-4aab-b58f-60e607b4d1a9\",\"request_start\":\"2021-04-25T15:37:20+00:00\",\"ip\":\"52.52.52.52\",\"messages\":\"Started GET \\\"/admin/info/sidekiq\\\" for 52.52.52.52 at 2021-04-25 15:37:20 +0000\\nProcessing by Admin::InfoController#sidekiq_stats as */*\\nRequested via apphost.com as */*\\n  Snippet Load (1.8ms)  SELECT \\\"snippets\\\".* FROM \\\"snippets\\\" WHERE (locale = 'sv-SE' AND tag = 'information_notice_contact') ORDER BY \\\"snippets\\\".\\\"id\\\" ASC LIMIT $1  [[\\\"LIMIT\\\", 1]]\\n  Snippet Load (1.5ms)  SELECT \\\"snippets\\\".* FROM \\\"snippets\\\" WHERE (locale = 'sv-SE' AND tag = 'contact_us_frame') ORDER BY \\\"snippets\\\".\\\"id\\\" ASC LIMIT $1  [[\\\"LIMIT\\\", 1]]\\n  Rendering text template\\n  Rendered text template (Duration: 0.1ms | Allocations: 16)\\nCompleted 200 OK in 41ms (Views: 0.6ms | ActiveRecord: 3.3ms | Allocations: 10734)\\n\",\"method\":\"GET\",\"path\":\"/admin/info/sidekiq\",\"format\":\"*/*\",\"controller\":\"Admin::InfoController\",\"action\":\"sidekiq_stats\",\"status\":200,\"duration\":40.74,\"view_runtime\":0.56,\"db_runtime\":3.28,\"user_id\":null,\"admin_id\":null,\"request_host\":\"apphost.com\",\"ua\":\"curl/7.58.0\",\"operation\":null}\n```\n\nExample use case is collecting the JSON files and shipping them to an Elastic/Kibana cluster for centralized logging.\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"heavylog\"\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\n## Usage\n\nEnable and configure in a Rails initializer `config/initializers/logger.rb`:\n\n```rb\nRails.application.configure do\n  config.heavylog.enabled = true\n  config.heavylog.path = Rails.root.join(\"log/heavylog.log\")\n  # Default formatter is Heavylog::Formatters::Raw which simply outputs the ruby hash as a string\n  config.heavylog.formatter = Heavylog::Formatters::Json.new\n  config.heavylog.log_sidekiq = true # Default is `false`, set to `true` to automatically log sidekiq job runs too.\nend\n```\n\n## Configuration\n\n| Option | Type     | Description                |\n| :-------- | :------- | :------------------------- |\n| `enabled` | `boolean` | Set to `true` to enable logging. Default: `false`.  |\n| `path` | `string` | Path to output file. Default: `log/heavylog.log`.  |\n| `message_limit` | `integer` | Request output will be truncated if longer than this limit. Default: `52428800` (50MB).  |\n| `log_sidekiq` | `boolean` | Set to `true` to automatically log sidekiq jobs too. Default: `false`.  |\n| `error_handler` | `lambda/proc` | Code to execute when formatter throws exception. Default: `-\u003e(e) { p \"HeavyLog: Error writing to log: #{e.class}: #{e.message}\\n  #{e.backtrace.join(\"\\n  \")}\" }`\n| `custom_payload` | `block` | Block executed for every request that should return hash with extra fields you want to log. Default: `nil`. |\n\n### Custom payload\n\n`custom_payload` accepts a block with a single argument, `controller` you can use to access any methods you normally would in a controller action.  \nIt should return a hash with the extra fields you want to log.\n\n```rb\nRails.application.configure do\n  config.heavylog.custom_payload do |controller|\n    user_id = controller.respond_to?(:current_user) ? controller.current_user\u0026.id : nil\n\n    {\n      user_id:      user_id,\n      request_host: controller.request.host,\n      ua:           controller.request.user_agent,\n      operation:    controller.request.params[:operationName],\n    }\n  end\nend\n```\n\n### Sidekiq logging\n\nSet `log_sidekiq` to `true` if you want to automatically log Sidekiq job runs to the same file. Example with JSON formatter:\n```\n{\"request_id\":\"fb2c3798e2634011d670f753\",\"request_start\":\"2021-04-25T16:00:53+00:00\",\"ip\":\"127.0.0.1\",\"messages\":\"  Order Load (1.8ms)  SELECT \\\"orders\\\".* FROM \\\"orders\\\" WHERE \\\"orders\\\".\\\"id\\\" = $1 LIMIT $2  [[\\\"id\\\", 109987473], [\\\"LIMIT\\\", 1]]\\n  Customer Load (1.7ms)  SELECT \\\"customers\\\".* FROM \\\"customers\\\" WHERE \\\"customers\\\".\\\"id\\\" = $1 LIMIT $2  [[\\\"id\\\", 1027337], [\\\"LIMIT\\\", 1]]\\n\",\"controller\":\"SidekiqLogger\",\"action\":\"MailPrepaidCheckupsJob\",\"args\":\"[109987473]\"}\n```\n\nSidekiq job runs go into the same file as regular request logs but have controller set to `SidekiqLogger` and action to the name of the Job. \n\n### JSON formatter\n\nEvery request results in a hash containing all info about the request. The default `Heavylog::Formatters::Raw` formatter simply outputs the hash as a string to the output file.  \nUse the `Heavylog::Formatters::Json` formatter to convert the hash to JSON. The resulting file will contain one JSON object per line for every request.\n\n### Custom formatter\n\nThe formatter interface is simply a class with a `call` method that accepts a single argument which is the hash containing info about the request.  \nThe method should return the final result you want to write to file. Heavylog writes one line per request.\n\nJSON formatter for example:\n```rb\nclass Json\n  def call(data)\n    ::JSON.dump(data)\n  end\nend\n```\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisrang%2Fheavylog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrisrang%2Fheavylog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisrang%2Fheavylog/lists"}