sensible_logging 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40ddd14f9217b3c9465bf528d8a1a2befb3ff17177340d119533e58d16e208de
4
- data.tar.gz: 5a20a67b1f01cc7fdd4f7ec7959472086e2d73987ab5c2d6d54f1cbc823d4183
3
+ metadata.gz: b91e1451d99dabecf5e9126f248426ca32d622fd4aa90e73cd80b6a034f1ca98
4
+ data.tar.gz: c7d8e9bd3ef502279b40c133a2aff29c9cfde8ed24985362d848960221c38d31
5
5
  SHA512:
6
- metadata.gz: 0f17b47325de077896ae98001452e19a02df1ba12fce8734139ec47d665a1dc8db77609c15d127ff74c8da57e1aebd0f2464c5fa63d530b1e6443d47e37a8c7a
7
- data.tar.gz: cea09e17ded5de488a701a7aa5e737320ab9c6e444493669d996e57c4979a14199b979eac44cc2f8232a6c5cc6e58619205393a7f43b1436db1f9fa7aadae440
6
+ metadata.gz: 57c777173f5b0f66a26f6b5b3761d148de8e9f11af77bee6a24a0157fe09dd3f00cc65705316d5d6ba9c5f70cb9cfdaf466988486ec9793ac039b8b73697d1ef
7
+ data.tar.gz: 5560735ee50f49f582d4983f537a25829d93f6967fc5c3efe44e4fc20bb26b4e7eab2a1f5a23e70cccbc9e60184fcae867124705b16d671230d01595ea13bbfb
@@ -0,0 +1,20 @@
1
+ version: 2
2
+ jobs:
3
+ build_and_test:
4
+ docker:
5
+ - image: circleci/ruby:2.5
6
+ environment:
7
+ - RACK_ENV: test
8
+
9
+ steps:
10
+ - checkout
11
+
12
+ - run: bundle install --jobs=4 --retry=3 --path vendor/bundle
13
+
14
+ - run: bundle exec rspec --backtrace
15
+
16
+ workflows:
17
+ version: 2
18
+ test_and_deploy:
19
+ jobs:
20
+ - build_and_test
data/README.md CHANGED
@@ -1,41 +1,91 @@
1
1
  # Sensible logging
2
2
 
3
- Proof of concept for some sensible defaults for logging in a Sinatra app.
3
+ [![CircleCI](https://circleci.com/gh/madetech/sensible_logging.svg?style=svg)](https://circleci.com/gh/madetech/sensible_logging) [![Gem Version](https://badge.fury.io/rb/sensible_logging.png)](http://badge.fury.io/rb/sensible_logging)
4
4
 
5
- Plan will be to wrap this up in a single self contained gem that can easily be dropped into existing Sinatra applications.
5
+
6
+ A logging library with sensible defaults for Sinatra apps.
6
7
 
7
8
  ## Features
8
9
 
9
- * Add (or use an existing) request ID for use in logs
10
- * Trim the request logs to the bare minimal (method, path, status, duration and params if a GET request) - similar to lograge with Rails
11
- * Tagged logging out of the box, with some sensible defaults; host, environment and request ID
10
+ * Add (or use an existing if present `X-Request-Id`) request ID for use in logs
11
+ * Trim the request logs to the bare minimal (inspired by lograge):
12
+ * method
13
+ * path
14
+ * requesting IP address
15
+ * status
16
+ * duration
17
+ * params if a `GET` request
18
+ * Tagged logging, with some sensible defaults:
19
+ * subdomain
20
+ * environment
21
+ * unique request ID
22
+
23
+ ## Usage
24
+
25
+ 1. Add `sensible_logging` to your `Gemfile` and use `bundle install`.
26
+ 2. In `app.rb` register the module and then define your logging defaults.
27
+
28
+ ```ruby
29
+ require 'sensible_logging'
12
30
 
13
- ## Demo
31
+ class App < Sinatra::Base
32
+ register Sinatra::SensibleLogging
14
33
 
15
- There is an example Sinatra app included in this repo, so:
34
+ sensible_logging(
35
+ logger: Logger.new(STDOUT)
36
+ )
16
37
 
38
+ # rest of code omitted
17
39
  ```
40
+
41
+ ### Available options
42
+
43
+ There are a number of options that you can pass into `sensible_logging`:
44
+
45
+ * `logger`: The logging object.
46
+ **Default**: `Logger.new(STDOUT)`
47
+ * `use_default_log_tags`: Includes the subdomain, `RACK_ENV` and unique request ID in the log tags.
48
+ **Default**: `true`
49
+ * `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`.
50
+ **Default**: `1`.
51
+ * `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.
52
+ **Default**: `[]`
53
+ * `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&two=cat` you didn't want the `one` logged, you would set `exclude_params` to be `['one']`
54
+ **Default**: `[]`
55
+
56
+ ## Examples
57
+
58
+ There is an example Sinatra app included in this repo. Here's how to use it:
59
+
60
+ ```shell
18
61
  bundle install
62
+ cd examples
19
63
  rackup
20
64
  ```
21
65
 
22
66
  With the app running, run some curl commands against it:
23
67
 
24
- ```
68
+ ```shell
25
69
  curl 'localhost:9292/hello?one=two&two=three'
26
70
  ```
27
71
 
28
72
  You should notice in the logs:
29
73
 
30
74
  * Standard Sinatra `logger` helper works out of the box within apps with tags.
31
- * Excluded paramaters are not included, in this example `two` based on `config.ru`
75
+ * Excluded parameters are not included, in this example `two` based on `config.ru`
32
76
  * The request log is minimal compared to out of the box Sinatra.
33
77
  * `env['request_id']` is now available to hook into Sentry reports.
34
78
 
35
- ## Using
79
+ ## FAQ
80
+
81
+ ### Why is the timestamp absent?
82
+
83
+ To quote [lograge][link_lograge] (which was the inspiration for this library):
84
+
85
+ > 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.
36
86
 
37
- Key thing is to wrap the Sinatra app in `sensible_logging` (as in `config.ru`) with your required options for logger, log tags and excluded parameters.
87
+ ## License
38
88
 
39
- ## Todo
89
+ MIT
40
90
 
41
- Need to ensure `sensible_logging` confirms to all of the relevant `productionisation` checklist items. Pretty much there though.
91
+ [link_lograge]: https://github.com/roidrage/lograge#lograge---taming-rails-default-request-logging
@@ -12,7 +12,9 @@ class RequestLogger
12
12
  status, headers, body = @app.call(env)
13
13
  end_time = Time.now - start_time
14
14
 
15
- message = "method=#{req.request_method} path=#{req.path} status=#{status} duration=#{end_time}"
15
+ client_ip = req.ip || 'n/a'
16
+
17
+ message = "method=#{req.request_method} path=#{req.path} client=#{client_ip} status=#{status} duration=#{end_time}"
16
18
  filtered_params = filter_params(req)
17
19
  message += " params=#{filtered_params}" if req.get? && ! filtered_params.empty?
18
20
  env['logger'].info(message)
@@ -1,3 +1,4 @@
1
+ require 'ipaddr'
1
2
  require 'active_support/tagged_logging'
2
3
  require_relative '../helpers/subdomain_parser'
3
4
 
@@ -22,13 +23,17 @@ class TaggedLogger
22
23
 
23
24
  def default_tags(tld_length: 1)
24
25
  [lambda { |req|
25
- subdomain_parser = SubdomainParser.new(tld_length: tld_length)
26
- subdomain = subdomain_parser.parse(req.host)
27
-
28
- [subdomain || 'n/a', ENV['RACK_ENV'], req.env['request_id']]
26
+ [subdomain(req.host, tld_length) || 'n/a', ENV['RACK_ENV'], req.env['request_id']]
29
27
  }]
30
28
  end
31
29
 
30
+ def subdomain(host, tld_length)
31
+ IPAddr.new(host)
32
+ rescue IPAddr::InvalidAddressError
33
+ subdomain_parser = SubdomainParser.new(tld_length: tld_length)
34
+ subdomain_parser.parse(host)
35
+ end
36
+
32
37
  def generate_tags(env)
33
38
  req = Rack::Request.new(env)
34
39
  tags = @tags.map do |tag|
@@ -1,3 +1,3 @@
1
1
  module SensibleLogging
2
- VERSION = "0.3.0"
2
+ VERSION = '0.4.0'
3
3
  end
@@ -7,7 +7,7 @@ require_relative './sensible_logging/middlewares/request_logger'
7
7
  module Sinatra
8
8
  module SensibleLogging
9
9
  def sensible_logging(
10
- logger:,
10
+ logger: Logger.new(STDOUT),
11
11
  log_tags: [],
12
12
  use_default_log_tags: true,
13
13
  exclude_params: [],
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensible_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Winter
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-01-08 00:00:00.000000000 Z
13
+ date: 2019-01-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -131,6 +131,7 @@ executables: []
131
131
  extensions: []
132
132
  extra_rdoc_files: []
133
133
  files:
134
+ - ".circleci/config.yml"
134
135
  - ".gitignore"
135
136
  - ".rspec"
136
137
  - ".ruby-version"