Last active
August 24, 2018 11:39
-
-
Save goromlagche/35070f98073102d2429e588197257a93 to your computer and use it in GitHub Desktop.
writing a generic log formatter for rails and sneakers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module App | |
| class Application < Rails::Application | |
| ... | |
| config.log_tags = [:request_id] | |
| ... | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomFormatter < ActiveSupport::Logger::SimpleFormatter | |
| include ActiveSupport::TaggedLogging::Formatter | |
| def call(severity, timestamp, _progname, msg) | |
| { | |
| timestamp: timestamp, | |
| level: severity, | |
| guid: current_tags.first || SecureRandom.uuid, # use request_id for rails and generate a uuid for sneakers | |
| pid: $PID, | |
| message: msg | |
| }.to_json.to_s + " \n" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.logger.formatter = CustomFormatter.new | |
| Sneakers.logger.formatter = CustomFormatter.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| A very simple generic log formatter for sneakers and rails. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment