{"id":20130508,"url":"https://github.com/davidwinter/sensible_logging","last_synced_at":"2025-04-09T16:15:46.445Z","repository":{"id":43483358,"uuid":"162613465","full_name":"davidwinter/sensible_logging","owner":"davidwinter","description":"A logging library with sensible defaults for Sinatra apps.","archived":false,"fork":false,"pushed_at":"2024-07-31T11:04:44.000Z","size":146,"stargazers_count":4,"open_issues_count":4,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T16:15:39.850Z","etag":null,"topics":["logging","sinatra"],"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/davidwinter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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,"publiccode":null,"codemeta":null},"funding":{"github":["davidwinter"]}},"created_at":"2018-12-20T17:55:07.000Z","updated_at":"2023-01-31T17:08:48.000Z","dependencies_parsed_at":"2024-03-25T10:51:28.383Z","dependency_job_id":null,"html_url":"https://github.com/davidwinter/sensible_logging","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidwinter%2Fsensible_logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidwinter%2Fsensible_logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidwinter%2Fsensible_logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidwinter%2Fsensible_logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidwinter","download_url":"https://codeload.github.com/davidwinter/sensible_logging/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065282,"owners_count":21041872,"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","sinatra"],"created_at":"2024-11-13T20:38:54.105Z","updated_at":"2025-04-09T16:15:46.335Z","avatar_url":"https://github.com/davidwinter.png","language":"Ruby","funding_links":["https://github.com/sponsors/davidwinter"],"categories":[],"sub_categories":[],"readme":"# Sensible Logging\n\n[![test](https://github.com/davidwinter/sensible_logging/workflows/test/badge.svg)](https://github.com/davidwinter/sensible_logging/actions?query=workflow%3Atest) [![Codecov](https://img.shields.io/codecov/c/github/davidwinter/sensible_logging)](https://codecov.io/gh/davidwinter/sensible_logging) [![Gem](https://img.shields.io/gem/v/sensible_logging)](https://rubygems.org/gems/sensible_logging) [![Gem](https://img.shields.io/gem/dt/sensible_logging)](https://rubygems.org/gems/sensible_logging)\n\n\u003e A logging extension with sensible defaults for Sinatra apps.\n\n## Features\n\n* Add a request UUID (or use an existing one if present in the `X-Request-Id` HTTP header) for use in logs, your app or other middlewares\n* Trim the request logs to the bare minimal (inspired by lograge):\n  * method\n  * path\n  * requesting IP address\n  * status\n  * duration\n  * params if a `GET` request\n\n  Example log line:\n  ```\n  method=GET path=/contact client=192.168.1.254 status=200 duration=0.124 params={\"category\"=\u003e\"question\"}\n  ```\n* Tagged logging, with some sensible defaults:\n  * severity\n  * subdomain\n  * environment\n  * request UUID\n\n  Example log line:\n  ```\n  [INFO] [www.gb] [staging] [6004bb70-7b6d-43b6-a2cf-72d0336663ba] @todo tidy sql query\n  ```\n\n## Usage\n\n1. Add `sensible_logging` to your `Gemfile` and install with `bundle install`:\n\n  ```ruby\n  gem 'sensible_logging'\n  ```\n2. In your `app.rb` register the module and then define your logging configuration:\n\n  ```ruby\n  require 'sensible_logging'\n\n  class App \u003c Sinatra::Base\n    register Sinatra::SensibleLogging\n\n    # Initialise Sensible Logging to add our middlewares.\n    sensible_logging(\n      logger: Logger.new($stdout)\n      )\n\n    # Configure the log level for different environments\n    configure :production do\n      set :log_level, Logger::INFO\n    end\n\n    # Requests will be logged in a minimal format\n    get '/' do\n      'Hello!'\n    end\n\n    get '/about' do\n      # The standard Sinatra logger helper will use the Sensible Logging gem\n      logger.info('About page')\n    end\n\n    get '/contact' do\n      # In addition to the default tags, you can add additional ones by using the `tagged` block on the `logger` helper\n      # [INFO] [localhost] [development] [a9d0183d-a3c3-4081-b502-38dcf4c3c4d7] [todo] Contact page\n      logger.tagged('todo') do |logger|\n        logger.info('Contact page')\n      end\n    end\n\n  # rest of code omitted\n  ```\n\n### Available options\n\nThere are a number of options that you can pass into `sensible_logging`:\n\n* `logger`: The logging object.\n  **Default**: `Logger.new($stdout)`\n* `use_default_log_tags`: Includes the subdomain, `RACK_ENV` and unique request ID in the log tags.\n  **Default**: `true`\n* `tld_length`: For example, if your domain was `www.google.com` this would result in `www` being tagged as your subdomain. If your domain is `www.google.co.uk`, set this value to `2` to correctly identify the subdomain as `www` rather than `www.google`.\n  **Default**: `1`.\n* `log_tags`: An array of additional log tags to include. This can be strings, or you can include a `lambda` that will be evaluated. The `lambda` is passed a Rack `Request` object, and it must return an array of string values.\n  **Default**: `[]`\n* `exclude_params`: An array of parameter names to be excluded from `GET` requests. By default, `GET` parameters are outputted in logs. If for example with the request `http://google.com/?one=dog\u0026two=cat` you didn't want the `one` logged, you would set `exclude_params` to be `['one']`\n  **Default**: `[]`\n* `include_log_severity`: Includes the log severity in the tagged output, such as `INFO`, `DEBUG` etc\n  **Default**: `true`\n\nSensible Logger will also respect the following Sinatra settings:\n\n* `log_level`: The level at which your logger object should respect logs. See above example.\n  **Default**: `Logger::DEBUG`\n\n## Examples\n\nThere is an example Sinatra app included in this repo. Here's how to use it:\n\n```shell\nbundle install\ncd examples\nrackup\n```\n\nWith the app running, run some curl commands against it:\n\n```shell\ncurl 'localhost:9292/hello?one=two\u0026two=three'\n```\n\nYou should notice in the logs:\n\n* Standard Sinatra `logger` helper works out of the box within apps with tags.\n* Excluded parameters are not included, in this example `two` based on `config.ru`\n* The request log is minimal compared to out of the box Sinatra.\n* `env['request_id']` is now available to group log lines from the same request together, or to use in additional services such as Sentry.\n\n## FAQ\n\n### Why is the timestamp absent?\n\nTo quote [lograge][link_lograge] (which was the inspiration for this library):\n\n\u003e 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.\n\n## Releasing\n\n1. Update the version number `lib/sensible_logging/version.rb`\n2. Create a git commit and tag with the new version number: `git tag -a v0.4.2 -m \"v0.4.2\"`\n3. Create a new PR with this change and once approved, merge to `main`\n4. Once approved and merged in, locally, run: `bundle exec rake release` which will build and push the new gem to Rubygems\n\n## Authors\n\nBy [David Winter](https://github.com/davidwinter) with contributions from [Mark Sta Ana](https://github.com/booyaa) \u0026 [Anthony King](https://github.com/cybojenix)\n\n## License\n\nMIT\n\n[link_lograge]: https://github.com/roidrage/lograge#lograge---taming-rails-default-request-logging\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidwinter%2Fsensible_logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidwinter%2Fsensible_logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidwinter%2Fsensible_logging/lists"}