pier_logging 0.1.4 → 0.1.9

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: 1d355c6b8850fce6ffea3a958ade583412cc4d315668c2b3795924ec281ceb1e
4
- data.tar.gz: 0c3382adb12ff55ebb201468f6470b722baa6344460a62b8666be0effba6a07c
3
+ metadata.gz: 35cdfc0d8703bece81fe50518a2e834b7216dbf409960126285f7a319cbaabf4
4
+ data.tar.gz: 6d13d3e9e03414af4bbcdeb04b895654cff354760c3f8f2e23f2db8511fdbdd8
5
5
  SHA512:
6
- metadata.gz: 9cb167ecc0b099d029a22b7c60a9d8ad2f8dddf0e586435d936a0344c1b6d3da0e6a8630ddc8e5ad3cb60d0271d56d4e1063ee385064fb7534b31735031b7ce9
7
- data.tar.gz: 68c003813d5e5bdb3aa07ee1008cd16e6faad3361d6add6849f3503e6aaadc8621ca86c6fe192719f71c12a20b21f3d602ff06f5ab8f35aa768fd38103d6319a
6
+ metadata.gz: c9627c7c496e508244c3a9b7c4cbc659d26e77b9a06dceaef2282656bc7ff2705c26cb7065635dad953ac69d571d6f4f623b487b6dbe2c915f22e99627991897
7
+ data.tar.gz: 4b4d2b2f0ba570aeeea62580dac125a7df90f4d85883056f06855f6e4e51596b0f2fe2f93cd5c080f33a1349095e84f17f01232c54f6c754d8e319c47fd50d7d
@@ -0,0 +1,40 @@
1
+ # This is a comment.
2
+ # Each line is a file pattern followed by one or more owners.
3
+
4
+ # These owners will be the default owners for everything in
5
+ # the repo. Unless a later match takes precedence,
6
+ # @global-owner1 and @global-owner2 will be requested for
7
+ # review when someone opens a pull request.
8
+ * @pier-digital/backend
9
+ .github/* @pier-digital/backend @pier-digital/devops
10
+ bin/* @pier-digital/backend @pier-digital/devops
11
+
12
+ # # Order is important; the last matching pattern takes the most
13
+ # # precedence. When someone opens a pull request that only
14
+ # # modifies JS files, only @js-owner and not the global
15
+ # # owner(s) will be requested for a review.
16
+ # *.js @js-owner
17
+
18
+ # # You can also use email addresses if you prefer. They'll be
19
+ # # used to look up users just like we do for commit author
20
+ # # emails.
21
+ # *.go docs@example.com
22
+
23
+ # # In this example, @doctocat owns any files in the build/logs
24
+ # # directory at the root of the repository and any of its
25
+ # # subdirectories.
26
+ # /build/logs/ @doctocat
27
+
28
+ # # The `docs/*` pattern will match files like
29
+ # # `docs/getting-started.md` but not further nested files like
30
+ # # `docs/build-app/troubleshooting.md`.
31
+ # docs/* docs@example.com
32
+
33
+ # # In this example, @octocat owns any file in an apps directory
34
+ # # anywhere in your repository.
35
+ # apps/ @octocat
36
+
37
+ # # In this example, @doctocat owns any file in the `/docs`
38
+ # # directory in the root of your repository.
39
+ # /docs/ @doctocat
40
+
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pier_logging (0.1.2)
4
+ pier_logging (0.1.7)
5
5
  awesome_print
6
+ facets
6
7
  ougai
7
8
  rails
8
9
 
@@ -70,6 +71,7 @@ GEM
70
71
  concurrent-ruby (1.1.6)
71
72
  crass (1.0.6)
72
73
  erubi (1.9.0)
74
+ facets (3.1.0)
73
75
  globalid (0.4.2)
74
76
  activesupport (>= 4.2.0)
75
77
  i18n (1.8.3)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # PierLogging
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pier_logging`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A gem developed by [PIER](https://www.pier.digital/) to standardize our logs (request and general-purpose)
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,17 +20,75 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ - Create an initializer at `config/initializers/pier_logging.rb` to configure the gem.
24
+ - Configure General-purpose logging, Request logging and register the request logger rails middleware.
25
+ - Configure your logger on `config/application.rb` to use `PierLogging::Logger`
26
+
27
+ ### General-purpose logging
28
+
29
+ Use `PierLogging.configure_logger` block to configure general-purpose logs. Accepted configs are:
30
+
31
+ | config | Required | Type | Default |
32
+ | --------- | --------:| -------------------------:| ----------------------------------:|
33
+ | app_name | true | string | nil |
34
+ | env | true | string | nil |
35
+ | formatter | false | `Ougai::Formatters::Base` | `PierLogging::Formatter::Json.new` |
36
+
37
+ ### Request logging
38
+
39
+ Use `PierLogging.configure_request_logger` block to configure request logs. Accepted configs are:
40
+
41
+ | config | Required | Type | Default |
42
+ | ---------------- | --------:| ---------------:| ----------:|
43
+ | enabled | false | boolean | false |
44
+ | user_info_getter | true | block (headers) | nil |
45
+
46
+ The block passed to `user_info_getter` receives the headers of the request so you can use your headers to define the username or role.
26
47
 
27
- ## Development
48
+ You have at your disposal the following helper methods:
28
49
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+ - has_basic_credentials(headers): checks if there are basic auth credentials in the header
51
+ - get_basic_credentials_user(headers): get the user from the basic auth credential
30
52
 
31
- To 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+ ### Example
54
+
55
+ ```ruby
56
+ # config/initializers/pier_logging.rb
57
+
58
+ PierLogging.configure_logger do |config|
59
+ config.app_name = Rails.application.class.module_parent_name.underscore.dasherize
60
+ config.env = Rails.env
61
+ config.formatter = Rails.env.production? ? PierLogging::Formatter::Json.new :
62
+ PierLogging::Formatter::Readable.new
63
+ end
64
+
65
+ PierLogging.configure_request_logger do |config|
66
+ config.user_info_getter do |headers|
67
+ if headers['MY-USER-HEADER'].present?
68
+ { username: headers['MY-USER-HEADER'] }
69
+ elsif has_basic_credentials?(headers)
70
+ { username: get_basic_credentials_user(headers) }
71
+ else
72
+ { username: 'anonymous' }
73
+ end
74
+ end
75
+ config.enabled = ENV.fetch('REQUEST_LOG_ENABLED', 'true') == 'true'
76
+ end
77
+
78
+ Rails.application.config.middleware.use PierLogging::RequestLogger, PierLogging::Logger.new(STDOUT)
79
+ ```
80
+
81
+ ```ruby
82
+ # config/application.rb
83
+
84
+ # ...
85
+ config.logger = PierLogging::Logger.new(STDOUT)
86
+ # ...
87
+ ```
32
88
 
33
89
  ## Contributing
34
90
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pier_logging.
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pier-digital/pier_logging.
36
92
 
37
93
  ## License
38
94
 
@@ -1,11 +1,14 @@
1
1
  require "rails"
2
2
  require "ougai"
3
+ require "awesome_print"
4
+ require "facets"
3
5
  require "pier_logging/version"
4
6
  require "pier_logging/logger"
5
7
  require "pier_logging/request_logger"
6
8
  require "pier_logging/formatter/base"
7
9
  require "pier_logging/formatter/json"
8
10
  require "pier_logging/formatter/readable"
11
+ require "pier_logging/helpers/headers"
9
12
 
10
13
  module PierLogging
11
14
  def self.logger_configuration
@@ -57,12 +60,13 @@ module PierLogging
57
60
  @enabled = false
58
61
  end
59
62
 
60
- def user_info_getter(&block)
61
- raise ArgumentError, "Config 'user_info_getter' must be a 'Proc'" unless block_given?
62
- @user_info_getter = block
63
+ def user_info_getter=(proc)
64
+ raise ArgumentError, "Config 'user_info_getter' must be a 'Proc'" unless proc.is_a? Proc
65
+ @user_info_getter = proc
63
66
  end
64
67
 
65
68
  def enabled=(enabled = false)
69
+ raise ArgumentError, "Config 'enabled' must be a 'boolean'" unless !!enabled == enabled
66
70
  @enabled = enabled
67
71
  end
68
72
  end
@@ -0,0 +1,20 @@
1
+ module PierLogging
2
+ module Helpers
3
+ class Headers
4
+ def self.has_basic_credentials?(headers)
5
+ auth_header = headers['AUTHENTICATION'].to_s
6
+ return false if auth_header.blank?
7
+ # Optimization: https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code
8
+ return false if auth_header.split(' ', 2)[0].casecmp('basic') == 0
9
+ return false if auth_header.split(' ', 2)[1].blank?
10
+ return true
11
+ end
12
+
13
+ def self.get_basic_credentials_user(headers)
14
+ auth_headers = headers['AUTHENTICATION'].to_s
15
+ credentials = auth_headers.split(' ', 2)[1]
16
+ ::Base64.decode64(credentials).split(':', 2)[0]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -67,7 +67,7 @@ module PierLogging
67
67
  # We should never fall in this part as the only errors that could result in this are errors
68
68
  # in our logger (inside this same method)
69
69
  @logger.error(error.message)
70
- end
70
+ end
71
71
 
72
72
  private
73
73
  def get_request_headers_from_env(env)
@@ -87,16 +87,15 @@ module PierLogging
87
87
  end
88
88
 
89
89
  def parse_body(body)
90
- if body.is_a? String # when string
91
- JSON.parse(body, nil, allow_blank: true)
92
- elsif body.is_a? Array # Grape body
93
- JSON.parse(body.last, nil, allow_blank: true)
94
- elsif body.is_a? ActionDispatch::Response::RackBody # Rails body
95
- JSON.parse(body.body, nil, allow_blank: true)
96
- else
97
- body
98
- end
90
+ body_object = get_body_object(body)
91
+ Oj.load(body_object, allow_blank: true)
99
92
  rescue
93
+ body_object || body
94
+ end
95
+
96
+ def get_body_object(body)
97
+ return body.last if body.is_a? Array # Grape body
98
+ return body.body if body.is_a? ActionDispatch::Response::RackBody # Rails body
100
99
  body
101
100
  end
102
101
 
@@ -125,20 +124,5 @@ module PierLogging
125
124
  rescue
126
125
  500
127
126
  end
128
-
129
- def has_basic_credentials?(headers)
130
- auth_header = headers['AUTHENTICATION'].to_s
131
- return false if auth_header.blank?
132
- # Optimization: https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code
133
- return false if auth_header.split(' ', 2)[0].casecmp('basic') == 0
134
- return false if auth_header.split(' ', 2)[1].blank?
135
- return true
136
- end
137
-
138
- def get_basic_credentials_user(headers)
139
- auth_headers = headers['AUTHENTICATION'].to_s
140
- credentials = auth_headers.split(' ', 2)[1]
141
- ::Base64.decode64(credentials).split(':', 2)[0]
142
- end
143
127
  end
144
128
  end
@@ -1,3 +1,3 @@
1
1
  module PierLogging
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "ougai"
27
27
  spec.add_dependency "awesome_print"
28
28
  spec.add_dependency "rails"
29
+ spec.add_dependency "facets"
29
30
 
30
31
  spec.add_development_dependency "bundler", ">= 2.1.4"
31
32
  spec.add_development_dependency "rake", ">= 12.3.3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pier_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauricio Banduk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-03 00:00:00.000000000 Z
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ougai
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: facets
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,7 @@ executables: []
115
129
  extensions: []
116
130
  extra_rdoc_files: []
117
131
  files:
132
+ - ".github/CODEOWNERS"
118
133
  - ".github/workflows/publish.yml"
119
134
  - ".gitignore"
120
135
  - ".ruby-version"
@@ -134,6 +149,7 @@ files:
134
149
  - lib/pier_logging/formatter/base.rb
135
150
  - lib/pier_logging/formatter/json.rb
136
151
  - lib/pier_logging/formatter/readable.rb
152
+ - lib/pier_logging/helpers/headers.rb
137
153
  - lib/pier_logging/logger.rb
138
154
  - lib/pier_logging/request_logger.rb
139
155
  - lib/pier_logging/version.rb