fluent_logger_rails 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a88bc428967e9ee7c2f81f9e747b0cc868f52fe9158587c4c847f1516c43d59
4
- data.tar.gz: 175d618a1cc6cd67e7c137b02328040e256c8f8e101e6d8786501227792453fe
3
+ metadata.gz: 0ce631df4339e61d2d64b000abb2411ae9c392455cafd934ed8f9b02a4bce363
4
+ data.tar.gz: 8180c87b862c568b132b41f62fd8d007c1d25323254395f4814165767ca5c765
5
5
  SHA512:
6
- metadata.gz: a1557f1bb328c7525b9c7341898c8cfeebe4646a1b2d0741b09cc7e65ae3e2e1de6654b59d8abd1b50818b9e94c2ddc8c95fd3bb40c9d08daf3d632cdf74f11b
7
- data.tar.gz: ad952d449c9c9e53e3ac59da6da928cb1a34fd01fa4d34d9a2fce3d4160a48b3cca22fe75517b8b62144ae21a878e962abc915a21a97446bb9bc9919a79d6687
6
+ metadata.gz: 40aefe081952b14f73fc8aa695a5ffab2c24659d199c40b688513a44d3b61894bc872aa24d01f3da1fe45b292b668f2b77c464073df9991d5b9c9fd67ff5fe1a
7
+ data.tar.gz: 45144abd502ce1d3a22c61fce70393628aff562da6ec75bcb4cc55e22cadf05893d2b7c82120bc216b7d964265d2da79c3ee4e1eec58aa6eb29b6a478e1b3b19
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.6
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rspec spec
@@ -8,9 +8,20 @@ Bug Fixes:
8
8
 
9
9
  Enhancements:
10
10
 
11
- * Add a rake task to walk through your table/columns and easily classify and create a migration from it
12
- * Add a migration generator
13
- * Module helper to include in migrations to add data classification smart comment
11
+ * None
12
+
13
+ Deprecations:
14
+ * None
15
+
16
+ ## v0.2.0
17
+
18
+ Bug Fixes:
19
+
20
+ * None
21
+
22
+ Enhancements:
23
+
24
+ * Refactoring interface, major breaking changes on format (#1)
14
25
 
15
26
  Deprecations:
16
27
  * None
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fluent_logger_rails (0.1.0)
4
+ fluent_logger_rails (0.2.0)
5
5
  activesupport (~> 5.0)
6
6
 
7
7
  GEM
@@ -13,9 +13,9 @@ GEM
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
15
  coderay (1.1.3)
16
- concurrent-ruby (1.1.6)
16
+ concurrent-ruby (1.1.7)
17
17
  diff-lcs (1.4.4)
18
- i18n (1.8.3)
18
+ i18n (1.8.5)
19
19
  concurrent-ruby (~> 1.0)
20
20
  method_source (1.0.0)
21
21
  minitest (5.14.1)
data/README.md CHANGED
@@ -0,0 +1,109 @@
1
+ # Fluent Logger Rails
2
+
3
+ This is a library that wraps the [fluent-logger gem](https://github.com/fluent/fluent-logger-ruby) and provides easy integration with your Rails application. This includes a log formatter that supports [Rails tagged logging](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html) so your output JSON format that can be sent to Fluentd (or really any other logging backend).
4
+
5
+ # Installation
6
+
7
+ ```
8
+ gem install fluent_logger_rails
9
+ ```
10
+
11
+ # How to configure
12
+
13
+ ```ruby
14
+ Rails.application.configure do
15
+ config.logger = FluentLoggerRails::Logger.new(
16
+ ::Fluent::Logger::FluentLogger.new(
17
+ nil,
18
+ host: 'localhost',
19
+ port: 24224,
20
+ ),
21
+ level: config.log_level
22
+ )
23
+ # if you are using tagged logging, you need a formatter that supports it
24
+ config.logger.formatter = FluentLoggerRails::TaggedHashFormatter.new
25
+ end
26
+ ```
27
+
28
+ ## Formatter Configuration
29
+
30
+ This gem includes a formatter that supports [Rails tagged logging](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html).
31
+
32
+ ### Hash aka JSON logger formatter
33
+
34
+ This is a JSON formatter that supports tagged logging.
35
+ ```ruby
36
+ config.logger.formatter = FluentLoggerRails::TaggedHashFormatter.new
37
+ config.logger.formatter.datetime_format = '%Y-%m-%d %H:%M:%S.%3N%z'
38
+ config.logger.formatter.parent_key = 'payload'
39
+ ```
40
+
41
+ ### Standard Rails Tagged Logger aka default logger format
42
+
43
+ The standard Rails tagged logger works as well for standard output.
44
+ ```ruby
45
+ ActiveSupport::TaggedLogging.new(config.logger)
46
+ ```
47
+
48
+ # Examples
49
+
50
+ ## Hash formatter with tagged Logging
51
+ ```ruby
52
+ Rails.logger.tagged(user.id) do
53
+ Rails.logger.warn('UserUpdateJob failed')
54
+ end
55
+
56
+ #
57
+ # Outputs:
58
+ #
59
+ # {
60
+ # "tags": [1234],
61
+ # "message": "UserUpdateJob failed",
62
+ # "severity": "WARN",
63
+ # "timestamp": "2019-01-08 14:51:39.701-0800",
64
+ # }
65
+ ```
66
+
67
+ ## Hash formatter with hash tagged Logging
68
+
69
+ ```ruby
70
+ Rails.logger.tagged(user_id: user.id, session_id: user_session.id) do
71
+ Rails.logger.info(message: 'UserUpdateJob failed', args: args)
72
+ end
73
+
74
+ #
75
+ # Outputs:
76
+ #
77
+ # {
78
+ # "user_id": 1234,
79
+ # "session_id": 883839,
80
+ # "message": {
81
+ # "message": "UserUpdateJob failed",
82
+ # "args": [1,2,3]
83
+ # },
84
+ # "severity": "INFO",
85
+ # "timestamp": "2019-01-08 14:51:39.701-0800",
86
+ # }
87
+ ```
88
+
89
+ ## Standard Rails formatter with tagged Logger
90
+
91
+ ```ruby
92
+ Rails.logger.tagged(user.id, user_session.id) do
93
+ Rails.logger.info('UserUpdateJob failed')
94
+ end
95
+
96
+ #
97
+ # Outputs:
98
+ #
99
+ # [1234] [883839] UserUpdateJob failed
100
+ ```
101
+
102
+ # How to test this locally
103
+
104
+ You can setup Fluentd with ruby gems as described on [Fluentd docs](https://docs.fluentd.org/installation/install-by-gem). Once that is running, simply configure your environment with the example above and the logs should appear.
105
+
106
+ # Related Projects
107
+
108
+ - https://github.com/fluent/fluent-logger-ruby
109
+ - https://github.com/actindi/act-fluent-logger-rails
@@ -5,8 +5,11 @@ Gem::Specification.new do |spec|
5
5
  spec.name = 'fluent_logger_rails'
6
6
  spec.version = FluentLoggerRails::VERSION
7
7
  spec.date = '2020-07-14'
8
- spec.summary = "A wrapper for fluent-logger gem to support tagged logging"
9
- spec.description = "A simple hello world gem"
8
+ spec.summary = "A wrapper for fluent-logger gem with support for tagged logging"
9
+ spec.description = "This is a library that wraps the [fluent-logger gem](https://github.com/fluent/fluent-logger-ruby) "\
10
+ "and provides easy integration with your Rails application. This includes log formatters that support "\
11
+ "[Rails tagged logging](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html) for JSON format "\
12
+ "that can be sent to Fluentd (or really any other logging backend)."
10
13
  spec.authors = ["HackerOne Open Source", "Ben Willis"]
11
14
  spec.email = ["opensource+fluent_logger_rails@hackerone.com", "ben@hackeroen.com"]
12
15
  spec.homepage = "https://github.com/Hacker0x01/fluent_logger_rails"
@@ -1,5 +1,2 @@
1
1
  require 'fluent_logger_rails/logger'
2
- require 'fluent_logger_rails/tagged_logging'
3
- require 'fluent_logger_rails/hash_formatter'
4
- require 'fluent_logger_rails/json_formatter'
5
- require 'fluent_logger_rails/pretty_json_formatter'
2
+ require 'fluent_logger_rails/tagged_hash_formatter'
@@ -16,8 +16,7 @@ module FluentLoggerRails
16
16
  message = (block_given? ? yield : progname) if message.blank?
17
17
  return true if message.blank?
18
18
 
19
- message = format_message(severity, Time.now, progname, message)
20
- message = { message: message } unless message.is_a? Hash
19
+ message = format_message(severity, Time.zone.now, progname, message)
21
20
 
22
21
  @logger.post(@path, message)
23
22
  true
@@ -26,5 +25,16 @@ module FluentLoggerRails
26
25
  def close
27
26
  @logger.close
28
27
  end
28
+
29
+ delegate :add_tags, :remove_tags, :clear_tags!, to: :formatter
30
+
31
+ def tagged(*tags)
32
+ formatter.tagged(*tags) { yield self }
33
+ end
34
+
35
+ def flush
36
+ clear_tags!
37
+ super if defined?(super)
38
+ end
29
39
  end
30
40
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FluentLoggerRails
4
+ class TaggedHashFormatter < ::Logger::Formatter
5
+ # Parent key - ensures that if the log is being merged into a higher level payload this key can
6
+ # be set in order to not conflict or be overridden.
7
+ attr_accessor :parent_key
8
+
9
+ def initialize
10
+ super
11
+ # Override the Logger::Formatter default to a format that does not have a trailing space
12
+ @datetime_format = "%Y-%m-%dT%H:%M:%S.%6N"
13
+ end
14
+
15
+ def call(severity, timestamp, _progname, msg)
16
+ payload = {
17
+ severity: format_severity(severity),
18
+ timestamp: format_datetime(timestamp),
19
+ message: msg.is_a?(String) ? msg.strip : msg,
20
+ }.merge(compact_tags.deep_dup)
21
+
22
+ @parent_key ? { @parent_key => payload } : payload
23
+ end
24
+
25
+ def format_severity(severity)
26
+ if severity.blank?
27
+ 'ANY'
28
+ elsif severity.is_a? Integer
29
+ ActiveSupport::Logger::SEV_LABEL[severity]
30
+ else
31
+ severity
32
+ end
33
+ end
34
+
35
+ def tagged(*tags)
36
+ add_tags(*tags)
37
+ yield self
38
+ ensure
39
+ remove_tags(*tags)
40
+ end
41
+
42
+ def add_tags(*tags)
43
+ tags = tags.first if tags.length == 1
44
+
45
+ if tags.is_a? Array
46
+ current_tags[:tags] ||= []
47
+ current_tags[:tags] += tags
48
+ elsif tags.is_a? String
49
+ current_tags[:tags] ||= []
50
+ current_tags[:tags] << tags
51
+ else
52
+ current_tags.merge! tags
53
+ end
54
+ end
55
+
56
+ def remove_tags(*tags)
57
+ tags = tags.first if tags.length == 1
58
+
59
+ if tags.is_a? Array
60
+ current_tags[:tags] ||= []
61
+ tags.each { |tag| current_tags[:tags].delete(tag) }
62
+ elsif tags.is_a? String
63
+ current_tags[:tags] ||= []
64
+ current_tags[:tags].delete(tags)
65
+ else
66
+ tags.each_key { |key| current_tags.delete(key) }
67
+ end
68
+ end
69
+
70
+ def clear_tags!
71
+ current_tags.clear
72
+ end
73
+
74
+ def current_tags
75
+ # We use our object ID here to avoid conflicting with other instances
76
+ thread_key = @thread_key ||= "fluent_logger_rails:#{object_id}"
77
+ Thread.current[thread_key] ||= {}
78
+ end
79
+
80
+ def compact_tags
81
+ current_tags.delete_if { |_k, v| v.blank? }
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module FluentLoggerRails
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent_logger_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HackerOne Open Source
@@ -67,7 +67,10 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- description: A simple hello world gem
70
+ description: This is a library that wraps the [fluent-logger gem](https://github.com/fluent/fluent-logger-ruby)
71
+ and provides easy integration with your Rails application. This includes log formatters
72
+ that support [Rails tagged logging](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html)
73
+ for JSON format that can be sent to Fluentd (or really any other logging backend).
71
74
  email:
72
75
  - opensource+fluent_logger_rails@hackerone.com
73
76
  - ben@hackeroen.com
@@ -75,6 +78,7 @@ executables: []
75
78
  extensions: []
76
79
  extra_rdoc_files: []
77
80
  files:
81
+ - ".github/workflows/ruby.yml"
78
82
  - ".gitignore"
79
83
  - ".rspec"
80
84
  - CHANGELOG.md
@@ -85,11 +89,8 @@ files:
85
89
  - README.md
86
90
  - fluent_logger_rails.gemspec
87
91
  - lib/fluent_logger_rails.rb
88
- - lib/fluent_logger_rails/hash_formatter.rb
89
- - lib/fluent_logger_rails/json_formatter.rb
90
92
  - lib/fluent_logger_rails/logger.rb
91
- - lib/fluent_logger_rails/pretty_json_formatter.rb
92
- - lib/fluent_logger_rails/tagged_logging.rb
93
+ - lib/fluent_logger_rails/tagged_hash_formatter.rb
93
94
  - lib/fluent_logger_rails/version.rb
94
95
  homepage: https://github.com/Hacker0x01/fluent_logger_rails
95
96
  licenses:
@@ -116,5 +117,5 @@ requirements: []
116
117
  rubygems_version: 3.0.3
117
118
  signing_key:
118
119
  specification_version: 4
119
- summary: A wrapper for fluent-logger gem to support tagged logging
120
+ summary: A wrapper for fluent-logger gem with support for tagged logging
120
121
  test_files: []
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class HashFormatter
4
- def call(severity, timestamp, _progname, msg)
5
- severity_display = if severity.blank?
6
- 'ANY'
7
- elsif severity.is_a? Integer
8
- ActiveSupport::Logger::SEV_LABEL[severity]
9
- else
10
- severity
11
- end
12
-
13
- {
14
- severity: severity_display,
15
- timestamp: timestamp.in_time_zone.strftime('%Y-%m-%d %H:%M:%S.%3N%z'),
16
- message: msg.is_a?(String) ? msg.strip : msg,
17
- }.merge(compact_tags)
18
- end
19
-
20
- def tagged(*tags)
21
- add_tags(*tags)
22
- yield self
23
- ensure
24
- remove_tags(*tags)
25
- end
26
-
27
- def add_tags(*tags)
28
- tags = tags.first if tags.length == 1
29
-
30
- if tags.is_a? Array
31
- current_tags[:tags] ||= []
32
- current_tags[:tags] += tags
33
- elsif tags.is_a? String
34
- current_tags[:tags] ||= []
35
- current_tags[:tags] << tags
36
- else
37
- current_tags.merge! tags
38
- end
39
- end
40
-
41
- def remove_tags(*tags)
42
- tags = tags.first if tags.length == 1
43
-
44
- if tags.is_a? Array
45
- current_tags[:tags] ||= []
46
- tags.each { |tag| current_tags[:tags].delete(tag) }
47
- elsif tags.is_a? String
48
- current_tags[:tags] ||= []
49
- current_tags[:tags].delete(tags)
50
- else
51
- tags.each_key { |key| current_tags.delete(key) }
52
- end
53
- end
54
-
55
- def clear_tags!
56
- current_tags.clear
57
- end
58
-
59
- def current_tags
60
- # We use our object ID here to avoid conflicting with other instances
61
- thread_key = @thread_key ||= "fluent_logger_rails:#{object_id}"
62
- Thread.current[thread_key] ||= {}
63
- end
64
-
65
- def compact_tags
66
- current_tags.delete_if { |_k, v| v.blank? }
67
- end
68
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class JsonFormatter < HashFormatter
4
- def call(severity, timestamp, progname, msg)
5
- super(severity, timestamp, progname, msg).to_json
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class PrettyJsonFormatter < HashFormatter
4
- def call(severity, timestamp, progname, msg)
5
- JSON.pretty_generate(super(severity, timestamp, progname, msg))
6
- end
7
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module FluentLoggerRails
4
- module TaggedLogging
5
- def self.new(logger, format: :hash)
6
- logger.formatter = if format == :hash
7
- HashFormatter.new
8
- elsif format == :pretty_json
9
- PrettyJsonFormatter.new
10
- elsif format == :json
11
- JsonFormatter.new
12
- else
13
- fail "Unrecognized log format: '#{format}'"
14
- end
15
-
16
- logger.extend(self)
17
- end
18
-
19
- delegate :add_tags, :remove_tags, :clear_tags!, to: :formatter
20
-
21
- def tagged(*tags)
22
- formatter.tagged(*tags) { yield self }
23
- end
24
-
25
- def flush
26
- clear_tags!
27
- super if defined?(super)
28
- end
29
- end
30
- end