pier_logging 0.1.2 → 0.1.7

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: 0a2b1c76716abf96ce6ed1d392d3ac945a3565b05abb16f5400fd1f41a27e509
4
- data.tar.gz: 22ab7775f0c76d51075d566b7d8561c8ceed631693661ef6995669f03c51a809
3
+ metadata.gz: 1edbf6a4c1e0e1bcce41d72a274024cc03c3cbe1f1b7d024895ff9e22233c3de
4
+ data.tar.gz: f8e35c6786a7f91ad63390ba2e967c585515347e2f5a572e0207d85b701ab3f3
5
5
  SHA512:
6
- metadata.gz: 54864d9541a7aecb5e470d00b8938d3f1198ffb031ecb7057e2b4bcbe00bf5e3f5f7f953fa39f927071f4d26cf0f8a7c1c1d07f68c0bcee66edaf6700b7597e1
7
- data.tar.gz: 5a2230fcf76ac2e0563b9100f90f6841b0c97a1cf907b8e6e017fc65934f92201a1ef8cc64becbf309caf5efcf03a83fd3d5444263c74fe10c244c3e73b5d629
6
+ metadata.gz: 59cd614cbff2b29fa81ee854550774e9257eef41606683be53b80f436fbcf111cb6487d81fb4190e1b06a054d83b77225da1700120c456ca90808270ba7cbcb4
7
+ data.tar.gz: 7119018bce9343ac5a9542a0ab11b28656b8853e22d33b5cf14eb00f50e827606a4c0731209255c6e698d0d0a5dff1d4b4ebf5066bf279fff24c2d99d5c20663
@@ -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
+
@@ -18,21 +18,22 @@ jobs:
18
18
  version:
19
19
  - 'lib/pier_logging/version.rb'
20
20
  - name: Setup Ruby
21
- # if: steps.filter.outputs.version == 'true'
21
+ if: steps.filter.outputs.version == 'true'
22
22
  uses: ruby/setup-ruby@v1
23
23
  with:
24
24
  ruby-version: 2.7.1
25
25
  - name: Get version
26
- # if: steps.filter.outputs.version == 'true'
26
+ if: steps.filter.outputs.version == 'true'
27
27
  id: new_version
28
28
  run: echo "##[set-output name=version;]$(ruby ./bin/get_current_version.rb)"
29
29
  - name: Create tag
30
+ if: steps.filter.outputs.version == 'true'
30
31
  uses: tvdias/github-tagger@v0.0.1
31
32
  with:
32
33
  repo-token: "${{ secrets.GITHUB_TOKEN }}"
33
34
  tag: "${{ steps.new_version.outputs.version }}"
34
35
  - name: Create Release
35
- # if: steps.filter.outputs.version == 'true'
36
+ if: steps.filter.outputs.version == 'true'
36
37
  id: create_release
37
38
  uses: actions/create-release@v1
38
39
  env:
@@ -44,8 +45,22 @@ jobs:
44
45
  draft: false
45
46
  prerelease: false
46
47
  - name: Publish Gem
47
- # if: steps.filter.outputs.version == 'true'
48
+ if: steps.filter.outputs.version == 'true'
48
49
  env:
49
50
  VERSION: ${{ steps.new_version.outputs.version }}
50
51
  RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
51
52
  run: ruby ./bin/publish_gem.rb
53
+ - name: Notify deploy status to Slack
54
+ if: steps.filter.outputs.version == 'true'
55
+ env:
56
+ SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
57
+ STATUS: ${{ job.status }}
58
+ GH_MESSAGE: ${{ github.event.commits[0].message }}
59
+ GH_ACTOR: ${{ github.actor }}
60
+ GH_SHA: ${{ github.sha }}
61
+ GH_REPO: ${{ github.repository }}
62
+ APP_ENVIRONMENT: rubygems
63
+ SLACK_CHANNEL: CEBAD7R0F
64
+ LANGUAGE: "ruby" # "python", "nodejs"
65
+ DEPLOY_TARGET: ${{ github.event.repository.name }}
66
+ run: /bin/bash ./bin/notify_slack_github.sh
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
 
@@ -6,6 +6,7 @@ require "pier_logging/request_logger"
6
6
  require "pier_logging/formatter/base"
7
7
  require "pier_logging/formatter/json"
8
8
  require "pier_logging/formatter/readable"
9
+ require "pier_logging/helpers/headers"
9
10
 
10
11
  module PierLogging
11
12
  def self.logger_configuration
@@ -57,12 +58,13 @@ module PierLogging
57
58
  @enabled = false
58
59
  end
59
60
 
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
61
+ def user_info_getter=(proc)
62
+ raise ArgumentError, "Config 'user_info_getter' must be a 'Proc'" unless proc.is_a? Proc
63
+ @user_info_getter = proc
63
64
  end
64
65
 
65
66
  def enabled=(enabled = false)
67
+ raise ArgumentError, "Config 'enabled' must be a 'boolean'" unless !!enabled == enabled
66
68
  @enabled = enabled
67
69
  end
68
70
  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.2"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pier_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauricio Banduk
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".github/CODEOWNERS"
118
119
  - ".github/workflows/publish.yml"
119
120
  - ".gitignore"
120
121
  - ".ruby-version"
@@ -134,6 +135,7 @@ files:
134
135
  - lib/pier_logging/formatter/base.rb
135
136
  - lib/pier_logging/formatter/json.rb
136
137
  - lib/pier_logging/formatter/readable.rb
138
+ - lib/pier_logging/helpers/headers.rb
137
139
  - lib/pier_logging/logger.rb
138
140
  - lib/pier_logging/request_logger.rb
139
141
  - lib/pier_logging/version.rb