bellycard / napa

A simple framework for building APIs with Grape
Other
329 stars 72 forks source link

Better Logging #213

Closed darbyfrey closed 9 years ago

darbyfrey commented 9 years ago

This is a pretty big refactor of the Napa logger, but it adds some nice features that have been requested for some time. Namely, Heroku support and more readable output in development. Details below:

Heroku support

The problem in the past with running Napa on Heroku is that Napa expected to be able to write it's log files to the log/ directory on the file system. Heroku doesn't let you do that, so the fix has been to monkey patch Napa to disable the log file appender. With this change, Napa will detect if it is running on Heroku and disable the log file appender automatically. Magic!

Better dev output

In the previous logger, all output was in JSON format. This is good for production so that it can be easily parsed by things like Kibana, but it's messy in development because it's hard for humans to read. This change will use a different output format in development so that logs are easier to read. It changes the output from this:

{"timestamp":"2015-05-24T14:37:24.812013-05:00","level":"INFO","logger":"[campaign-service-134c7af93dd2c6143184]","message":{"request":{"method":"GET","path":"/customer_campaigns","query":"","host":"Darbys-MacBook-Pro-2.local","pid":62509,"revision":"f180be33d0b821bc5f9119c143532f1caa601e4d","params":{},"remote_ip":"127.0.0.1"}}}

To this:

 INFO  [campaign-service-3e9277ef4d4a315ceef6] : method=GET path=/customer_campaigns query= host=Darbys-MacBook-Pro-2.local pid=62140 revision=f180be33d0b821bc5f9119c143532f1caa601e4d params={} remote_ip=127.0.0.1

This is also a YAML formatter that can be used and will look like this:


---
timestamp: '2015-05-24T14:40:25.779677-05:00'
level: INFO
logger: '[campaign-service-b7b98f751151834b0d73]'
message:
  :request:
    :method: GET
    :path: /customer_campaigns
    :query: ''
    :host: Darbys-MacBook-Pro-2.local
    :pid: 62583
    :revision: f180be33d0b821bc5f9119c143532f1caa601e4d
    :params: {}
    :remote_ip: 127.0.0.1

---

To enable YAML output, you can add an initializer in your app with the following:

config/initializers/logger.rb

Napa::Logger.config = Napa::Logger::Configuration.new(format: :yaml)

Defaults

By default, the logger will behave exactly the same as it does now in production. It will still log everything as JSON with the same structure, and it will still write the file to log/.

Resolves: https://github.com/bellycard/napa/issues/196

Feedback appreciated!

@bellycard/platform @craigulliott

hstrowd commented 9 years ago

Looks like a great set of logging features to have in the Napa platform. Thanks.

umtrey commented 9 years ago

:fire: :fire: :fire:

Awesome additions here. Looking forward to the discussion of @hstrowd's points.

darbyfrey commented 9 years ago

@hstrowd @umtrey just pushed up some changes related to your comments. Let me know if you have any other thoughts.

umtrey commented 9 years ago

sad travis, but my thoughts? you're pretty swell.

darbyfrey commented 9 years ago

That's what happens when I get a little to crazy with the Rubocop... fixed!

shaqq commented 9 years ago

+:boom:

jonhoman commented 9 years ago

+1 from me.

shaqq commented 9 years ago

@darbyfrey: could you update the README and CHANGELOG?