{"id":13878290,"url":"https://github.com/iloveitaly/simple_structured_logger","last_synced_at":"2025-04-24T01:41:50.949Z","repository":{"id":44927750,"uuid":"57132772","full_name":"iloveitaly/simple_structured_logger","owner":"iloveitaly","description":"An incredibly simple, very opinionated, structured logging library for ruby","archived":false,"fork":false,"pushed_at":"2025-02-01T04:51:45.000Z","size":54,"stargazers_count":16,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T18:12:37.774Z","etag":null,"topics":["logging","rails","ruby","ruby-gem","structured-logging"],"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/iloveitaly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":["iloveitaly"]}},"created_at":"2016-04-26T13:58:41.000Z","updated_at":"2025-01-28T13:02:05.000Z","dependencies_parsed_at":"2025-01-07T14:21:41.447Z","dependency_job_id":"8022f4ea-f1f4-4cde-9734-e9bd046085d1","html_url":"https://github.com/iloveitaly/simple_structured_logger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iloveitaly%2Fsimple_structured_logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iloveitaly%2Fsimple_structured_logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iloveitaly%2Fsimple_structured_logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iloveitaly%2Fsimple_structured_logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iloveitaly","download_url":"https://codeload.github.com/iloveitaly/simple_structured_logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250545649,"owners_count":21448246,"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-gem","structured-logging"],"created_at":"2024-08-06T08:01:45.302Z","updated_at":"2025-04-24T01:41:50.923Z","avatar_url":"https://github.com/iloveitaly.png","language":"Ruby","funding_links":["https://github.com/sponsors/iloveitaly"],"categories":["Ruby"],"sub_categories":[],"readme":"[![Ruby](https://github.com/iloveitaly/simple_structured_logger/actions/workflows/ruby.yml/badge.svg)](https://github.com/iloveitaly/simple_structured_logger/actions/workflows/ruby.yml)\n[![gem](https://img.shields.io/gem/v/simple_structured_logger.svg)](https://rubygems.org/gems/simple_structured_logger)\n\n# SimpleStructuredLogger\n\nDead-simple structured logging in ruby with a dead-simple codebase. No dependencies, everything logs to stdout, and simple hooks to customize. That's it.\n\n```ruby\ngem 'simple_structured_logger'\n```\n\n## Usage\n\nYou can use this logger anywhere. Class methods, instance methods, use it as a logger for libraries, etc.\n\nSome examples:\n\n```ruby\n# in a console or simple script\ninclude SimpleStructuredLogger\nlog.info 'core message', key: Time.now.to_i\n\n# in class \u0026 instance methods\nclass LoggingInModule\n  include SimpleStructuredLogger\n\n  def self.log_something\n    log.info 'including the module enables a class and instance method', key: Time.now.to_i\n  end\n\n  def log_something_else\n    log.info 'the class and instance method share the same logging context', key: Time.now.to_i\n  end\nend\n\n# So, how do I set the context? How can I customize how it's set?\nSimpleStructuredLogger.configure do\n  expand_context do |context|\n    # you can pass in a object and use `expand_context` to extract the relevant keys\n    if context[:user]\n      context[:user_id] = context[:user].id\n      context[:user_name] = context[:user].name\n    end\n\n    context\n  end\nend\n\nclass ExampleJob\n  def perform(user_id)\n    user = get_user(user_id, job_argument)\n    log.set_context(user: user, job: self.class, job_argument: job_argument)\n    log.info 'the log will contain the user_id, job_argument, and job class'\n\n    # you can also add additional default pairs without resetting context\n    log.default_tags[:something] = 'else'\n  end\nend\n\n# Can you pass object arguments as values and automatically expand them? Well, yes, you can!\nSimpleStructuredLogger.configure do\n  expand_log do |tags, default_tags|\n    if tags[:stripe_resource] \u0026\u0026 tags[:stripe_resource].respond_to?(:id)\n      stripe_resource = tags.delete(:stripe_resource)\n      tags[:stripe_resource_id] = stripe_resource.id\n      tags[:stripe_resource_type] = stripe_resource.class.to_s\n    end\n\n    # this is a really nice pattern I like to use. The `metric` key can trigger a call out to your observability tooling\n    if tags[:metric]\n      dimensions = default_tags.slice(:stripe_user_id, :other_default_tag)\n      metrics.track_counter(tags[:metric], dimensions: dimensions)\n    end\n\n    tags\n  end\nend\n\n# want simple formatting? You got it!\nSimpleStructuredLogger.logger.formatter = proc do |severity, _datetime, _progname, msg|\n  \"#{severity}: #{msg}\\n\"\nend\n\n# Configure the logger directly if you need to\nSimpleStructuredLogger.logger.level(Logger::INFO)\n```\n\nWant to change the log level quickly? Without modifying source?\n\n```shell\nLOG_LEVEL=DEBUG ruby your_script.rb\n\n# case does not matter\nLOG_LEVEL=info ruby your_script.rb\n```\n\n## Design Goals\n\n* Extremely simple codebase that's easy to read and override\n* Structured logging that reads nicely and is easy to filter using grep or something like Papertrail\n* Ability to easily set context, and expand context with user-configurable hook\n* Ability to easily add structured log pre-processing. I want to be able to pass\n  in an object specific to my application and for the relevant important keys to\n  be expanded automatically.\n* `Rails.logger = SimpleStructuredLogger.new(STDOUT)`\n* Not designed around massive systems or scale: no JSON logging, multiple log destinations, and other fanciness.\n* Don't build in fancy pre-processing for errors or other common ruby objects\n\n### Opinionated Devops Setup\n\n* Errors are tracked using Rollbar, Airbrake, Sentry, etc.\n* Log to STDOUT\n* Pipe STDOUT to PaperTrail, Loggly, etc\n* Great for Heroku or [dokku/docker](http://mikebian.co/sending-dokku-container-logs-to-papertrail/) hosted system\n\n## Alternatives\n\n* https://github.com/jordansissel/ruby-cabin\n* https://github.com/asenchi/scrolls\n* https://github.com/stripe/chalk-log\n* https://github.com/nishidayuya/structured_logger\n* https://github.com/rocketjob/semantic_logger\n\n## Related\n\n* https://github.com/roidrage/lograge\n\n## Why is structured logging important?\n\n* https://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-about-real-time-datas-unifying\n* http://juliusdavies.ca/logging.html\n\n## What about Rail's tagged logging?\n\nTagged logging is not structured logging. I want to be able to search through\nPaperTrail/Splunk/etc and easily grab an audit trail for a specific context, i.e. `the_job=FailingJob the_user=1`.\n\n## Testing\n\n```\nbundle exec rake\n```\n\n## TODO\n\n- [ ] Support logs as blocks?","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filoveitaly%2Fsimple_structured_logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filoveitaly%2Fsimple_structured_logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filoveitaly%2Fsimple_structured_logger/lists"}