{"id":13878006,"url":"https://github.com/hschne/mr-loga-loga","last_synced_at":"2025-04-24T04:09:58.095Z","repository":{"id":39599962,"uuid":"427470431","full_name":"hschne/mr-loga-loga","owner":"hschne","description":"The bombastic, fantastic logger for Ruby 💎","archived":false,"fork":false,"pushed_at":"2024-11-10T07:44:33.000Z","size":293,"stargazers_count":22,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T07:04:03.311Z","etag":null,"topics":["logging","rails","ruby","ruby-on-rails"],"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/hschne.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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}},"created_at":"2021-11-12T19:12:37.000Z","updated_at":"2024-11-10T07:44:36.000Z","dependencies_parsed_at":"2023-12-02T20:24:14.861Z","dependency_job_id":"24d5f4e7-850b-4984-90e2-8eb1a3f7f3c9","html_url":"https://github.com/hschne/mr-loga-loga","commit_stats":{"total_commits":86,"total_committers":3,"mean_commits":"28.666666666666668","dds":"0.41860465116279066","last_synced_commit":"50a78c3f51590032b313ef526103af940bcea692"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hschne%2Fmr-loga-loga","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hschne%2Fmr-loga-loga/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hschne%2Fmr-loga-loga/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hschne%2Fmr-loga-loga/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hschne","download_url":"https://codeload.github.com/hschne/mr-loga-loga/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240571029,"owners_count":19822413,"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","ruby-on-rails"],"created_at":"2024-08-06T08:01:37.337Z","updated_at":"2025-02-24T22:45:44.644Z","avatar_url":"https://github.com/hschne.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# Mr. Loga Loga\n\n\u003cimg alt=\"logo\" src=\"logo.png\" width=\"300px\" height=\"auto\"\u003e\n\n### The simply bombastic, fantastic logger for Ruby 💎\n\n[![Gem Version](https://badge.fury.io/rb/mr_loga_loga.svg)](https://badge.fury.io/rb/mr_loga_loga)\n[![Main](https://github.com/hschne/mr-loga-loga/actions/workflows/main.yml/badge.svg)](https://github.com/hschne/mr-loga-loga/actions/workflows/main.yml)\n![License](https://img.shields.io/github/license/hschne/mr-loga-loga)\n\n[![Maintainability](https://api.codeclimate.com/v1/badges/7cdd4779e6c62bce6ba0/maintainability)](https://codeclimate.com/github/hschne/mr-loga-loga/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/7cdd4779e6c62bce6ba0/test_coverage)](https://codeclimate.com/github/hschne/mr-loga-loga/test_coverage)\n\u003c/div\u003e\n\n## What's this?\n\nMrLogaLoga is a logger for Ruby that allows you to easily attach contextual information to your log messages. \nWhen writing logs, messages only tell half the story. MrLogaLoga allows you to make the most of your logs.\n\n```ruby\nlogger.info('message', user: 'name', data: 1)\n# I, [2022-01-01T12:00:00.000000 #19074]  INFO -- Main: message user=user data=1\n```\n\nYou can find out more about the motivation behind the project [here](#why-mrlogaloga). For usage read [Usage](#usage) or [Advanced Usage](#advanced-usage).\n\n**Note**: This gem is in early development. Try it out and leave some feedback, it really goes a long way in helping me out with development. Any [feature request](https://github.com/hschne/mr-loga-loga/issues/new?assignees=\u0026labels=type%3ABug\u0026template=FEATURE_REQUEST.md\u0026title=) or [bug report](https://github.com/hschne/mr-loga-loga/issues/new?assignees=\u0026labels=type%3AEnhancement\u0026template=BUG_REPORT.md\u0026title=) is welcome. If you like this project, leave a star to show your support! ⭐\n\n## Getting Started\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'mr_loga_loga'\n```\n\nAnd then execute:\n\n```\nbundle install\n```\n\n## Usage\n\nMrLogaLoga provides the same interface as the Ruby default logger you are used to. In addition, however, you can attach contextual information to your log messages.\n\n```ruby\nrequire 'mr_loga_loga'\n\nlogger = MrLogaLoga::Logger.new\nlogger.info('message', user: 'name', data: 1) \n# I, [2022-01-01T12:00:00.000000 #19074]  INFO -- Main: message user=user data=1\nlogger.context(class: 'classname').warn('message') \n# W, [2022-01-01T12:00:00.000000 #19074]  WARN -- Main: message class=classname\n```\n\nTo customize how log messages are formatted see [Formatters][#formatters].\n\n## Advanced Usage\n\nMrLogaLoga provides a fluent interface to build log messages. For example, to attach an additional `user` field to a log message you can use any of the following:\n\n```ruby\nlogger.info('message', user: 'name')\nlogger.context(user: 'name').info('message') # Explicit context\nlogger.context { { user: 'name' } }.info('message') # Block context\nlogger.user('name').info('message') # Dynamic context method\nlogger.user { 'name' }.info('message') # Dynamic context block\n```\n\nThe block syntax [ is recommended when logging calculated properties ](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html#class-Logger-label-How+to+log+a+message). You can find more in-depth information about specific method in the [Code Documentation](https://www.hschne.at/mr-loga-loga/).\n\n#### Shared Context\n\nIf multiple log messages within the same class should share a context include the `MrLogaLoga` module. Using `logger` will result in the defined context being included per default:\n\n```ruby\nclass MyClass \n  include MrLogaLoga\n\n  # This is the default. You may overwrite this in your own classes\n  def loga_context \n     { class_name: self.class.name } \n  end\n\n  def log\n   # This includes the class name in the log message now\n   logger.debug('debug') # debug class_name=MyClass\n   # Additional context will be merged\n   logger.debug('debug', user: 'user') # debug class_name=MyClass user=user\n  end\nend\n```\n\nWhen used with [Rails](#rails) logger will default to `Rails.logger`. If you use MrLogaLoga outside of Rails, you can either configure the logger instance to be used globally, or by overwriting the `loga_loga` method:\n\n```ruby\n# In some configuration class\nMrLogaLoga.configure do |configuration|\n  logger = MrLogaLoga::Logger.new($stdout)\nend\n\n# In the class where you do the logging itself\nclass MyClass \n  include MrLogaLoga\n\n  def loga_loga\n    MrLogaLoga::Logger.new($stdout)\n  end\n\n  def log\n    # ...\n  end\nend\n```\n\n### Formatters\n\nMrLogaLoga uses the [KeyValue](https://github.com/hschne/mr-loga-loga/blob/main/lib/mr_loga_loga/formatters/key_value.rb) formatter per default. The [Json](https://github.com/hschne/mr-loga-loga/blob/main/lib/mr_loga_loga/formatters/json.rb) formatter is also included. To use a specific formatter pass it to the logger constructor:\n\n```Ruby\n\nMrLogaLoga::Logger.new(STDOUT, formatter: MrLogaLoga::Formatters::KeyValue.new)\n```\n\nYou can implement and add your own formatters like so:\n\n```ruby\nclass MyFormatter\n  def call(severity, datetime, progname, message, **context)\n    context = context.map { |key, value| \"#{key}=#{value}\" }.compact.join(' ')\n    \"#{severity} #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N')} #{progname} #{message} #{context}\"\n  end\nend\n\nMrLogaLoga::Logger.new(STDOUT, formatter: MyFormatter.new)\n```\n\n## Usage with Other Gems\n\nThis section describes how you can use MrLogaLoga together with other well-known gems.\n\n### Rails\n\nUsing MrLogaLoga in Ruby on Rails is straightforward. Set up MrLogaLoga as logger in your `application.rb` or environment files and you are off to the races:\n\n```ruby\n# application.rb\nconfig.log_formatter = MrLogaLoga::Formatters::KeyValue.new\nconfig.logger = MrLogaLoga::Logger.new($stdout, formatter: config.log_formatter)\n```\n\nIn order to use MrLogaLoga together with tagged logging you may have to patch `ActiveSupport::TaggedLogging`: \n```ruby\nmodule ActiveSupport\n  module TaggedLogging\n    module Formatter\n      def call(severity, time, progname, message, context)\n        tags = current_tags\n        context[:tags] = tags if tags.present?\n        super(severity, time, progname, message, context)\n      end\n    end\n  end\nend\n```\n\n### Lograge\n\n[LogRage](https://github.com/roidrage/lograge) and MrLogaLoga work well together. You must set lograge to use the raw formatter, however: \n\n```ruby\nRails.application.configure do\n  config.lograge.enabled = true\n  config.lograge.formatter = Lograge::Formatters::Raw.new\nend\n```\n\n\n\n### Sidekiq\n\nYou can use MrLogaLoga with [Sidekiq](https://github.com/mperham/sidekiq) by configuring it like so: \n\n```ruby\nSidekiq.configure_server do |config|\n  logger = MrLogaLoga::Logger.new($stdout, formatter: MrLogaLoga::Formatters::Json.new)\n  config.logger = logger.context { Thread.current[:sidekiq_context] || {} }\n  config.logger.level = Logger::INFO\n  \n  # Remove existing error handlers to avoid double logging\n  config.error_handlers.clear\n  config.error_handlers \u003c\u003c proc { |ex, context| Sidekiq.logger.warn(ex, context[:job]) }\nend\n```\n\nIf you want to use MrLogaLoga's helper methods in your workers you must include `MrLogaLoga` after including `Sidekiq`: \n```ruby\nclass MyWorker\n  include Sidekiq::Worker\n  include MrLogaLoga\nend\n```\n\n## Why MrLogaLoga?\n\nThe more context your logs provide, the more use you will get out of them. The standard Ruby logger only takes a string as an argument, so you have to resort to something like this:\n\n```ruby\nlogger.debug(\"my message user=#{user} more_data=#{data}\")\n```\n\nThis is fine, as long as you do not need to change your log format. Changing your log formatter will not change the format of your message, nor the formatting of the contextual information you provided.\n\nMrLogaLoga addresses this by allowing you to attach contextual information to your logs and giving you full control over how both message and context are formatted. There are other gems doing similar things, most notably [Semantic Logger](https://logger.rocketjob.io/). Where Semantic Logger provides lots of functionality related to logging, MrLogaLoga focuses on making it nice to write log messages - and nothing more. \n\n## Credit\n\nThis little library was inspired by [Lograge](https://github.com/roidrage/lograge) first and foremost. Some inspiration was taken from [ougai](https://github.com/tilfin/ougai). I would like to thank the amazing [@LenaSchnedlitz](https://twitter.com/LenaSchnedlitz) for the incredible logo! 🤩\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests and linter. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nThank you for contributing! :heart:\n\nWe welcome all support, whether on bug reports, code, design, reviews, tests, documentation, translations, or just feature requests.\n\nPlease use [GitHub issues](https://github.com/hschne/mr-loga-loga/issues) to submit bugs or feature requests.\n\n## License\n\nThe gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the MrLogaLoga project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/hschne/mr_loga_loga/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhschne%2Fmr-loga-loga","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhschne%2Fmr-loga-loga","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhschne%2Fmr-loga-loga/lists"}