pier_logging 0.1.4 → 0.1.5

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: eff84f20b75432ad8b9677f443507f89618bb16cb10fb540cea1f15e9ef38f44
4
+ data.tar.gz: 3ccee8b70edaf743e2a4696eb1b405c505b0d6d6d2aaffd565d761e318feca21
5
5
  SHA512:
6
- metadata.gz: 9cb167ecc0b099d029a22b7c60a9d8ad2f8dddf0e586435d936a0344c1b6d3da0e6a8630ddc8e5ad3cb60d0271d56d4e1063ee385064fb7534b31735031b7ce9
7
- data.tar.gz: 68c003813d5e5bdb3aa07ee1008cd16e6faad3361d6add6849f3503e6aaadc8621ca86c6fe192719f71c12a20b21f3d602ff06f5ab8f35aa768fd38103d6319a
6
+ metadata.gz: 16da7e85dbb12163a9bc5c9d660258ef51b0d968e55be28c04159cad2481ed62f3349e902a3dd9cc74417ec15c4d8bb0d3decab17cd717eecdb39a404d821b48
7
+ data.tar.gz: 88f4e611ab39c17fdb669bae02cffd16a6466e8c11d1403a9a60c84aceb834f707d212e72f908ca532fd1802c1522e1c497229082cf0a7b55235662287258e3a
@@ -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
+
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
 
@@ -57,12 +57,13 @@ module PierLogging
57
57
  @enabled = false
58
58
  end
59
59
 
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
60
+ def user_info_getter=(proc)
61
+ raise ArgumentError, "Config 'user_info_getter' must be a 'Proc'" unless proc.is_a? Proc
62
+ @user_info_getter = proc
63
63
  end
64
64
 
65
65
  def enabled=(enabled = false)
66
+ raise ArgumentError, "Config 'enabled' must be a 'boolean'" unless !!enabled == enabled
66
67
  @enabled = enabled
67
68
  end
68
69
  end
@@ -88,11 +88,11 @@ module PierLogging
88
88
 
89
89
  def parse_body(body)
90
90
  if body.is_a? String # when string
91
- JSON.parse(body, nil, allow_blank: true)
91
+ Oj.load(body, allow_blank: true)
92
92
  elsif body.is_a? Array # Grape body
93
- JSON.parse(body.last, nil, allow_blank: true)
93
+ Oj.load(body.last, allow_blank: true)
94
94
  elsif body.is_a? ActionDispatch::Response::RackBody # Rails body
95
- JSON.parse(body.body, nil, allow_blank: true)
95
+ Oj.load(body.body, allow_blank: true)
96
96
  else
97
97
  body
98
98
  end
@@ -1,3 +1,3 @@
1
1
  module PierLogging
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
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.4
4
+ version: 0.1.5
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"