act-fluent-logger-rails 0.6.4 → 0.7.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: bb748e74cfb1f32ac69cadea7c313c718a61d2bc43f145058bfd422360ecac7d
4
- data.tar.gz: b907cd807d165589c574c43c0327a563337642c9b68d589189437b5b1718bdf9
3
+ metadata.gz: acc73cf5a95c1d462332f38826653ce607550c8128195466089495427a7fbf8a
4
+ data.tar.gz: 5eccf02cf3437e591fe56c83a3cce224cd7734edcd9d5af9872cfda2c4ef6966
5
5
  SHA512:
6
- metadata.gz: '04853d8847ba5915d11dcbf0b5833a0e50802905f8b56c94a4cfb02974c3dfb9747db07dd1bdd5347f14db8542e1c38ff9a68584b28685a2f1bf49a3bde4f58d'
7
- data.tar.gz: 638eefe4822c6e41e878e2e0dbb69c5cde63079dcd85740423836fad68144512f808375205e936c4e97e9e90a35ab6394c6f91998156a5f156093c08bf4db421
6
+ metadata.gz: a19769ad38b13b59ed51f27ce0ccfcebd7b044be00b6c102b708cb90295ddc6f1ae56959f07261e362d21cc1e3a2bd66dd29bf70ef9674df4b8d2a40db1cffc6
7
+ data.tar.gz: e8b5d7371b6eff5463db65f7bc3794ecb4804dac9421184308010136a76e8157513672763fbfe7d6ddb967a450d7f99e04dbf445932de6a02645849dfaf05185
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## 0.7.0 / Oct 13 2026
2
+ * Package only `lib/`, README, CHANGELOG and LICENSE in the gem, not the specs and development files
3
+ * Require fluent-logger 0.9.1 or later (TLS support for `tls_options`, and a fix for invalid messages polluting the next one)
4
+ * Add `logger` as a runtime dependency, since it is a bundled gem from Ruby 4.0
5
+ * Make `silence`, `log_at` and symbol levels like `logger.level = :warn` work. `FluentLogger` overrode `level`, so it ignored the per-thread level these use (thanks @costa, from #66)
6
+ * Don't clear `config.log_tags` when another logger is created without `log_tags` (e.g. for Delayed::Job)
7
+ * Send `log_tags` on Rails 7.2+, where `Rails::Rack::Logger` sets them with `push_tags` instead of `tagged`
8
+ * Keep Array values of `log_tags` instead of flattening them into the following tags (based on #65)
9
+ * Drop support for Rails < 7.0 and Ruby < 3.1
10
+ * Parse `FLUENTD_URL` query parameters with `URI.decode_www_form`, since `CGI.parse` is not available in Ruby 4.0
11
+ * Require only the ActiveSupport core extensions the logger uses instead of all of `active_support/core_ext`, so Rails 7.0's `disable_to_s_conversion` is not preempted during `Bundler.require`
12
+ * Require `logger` before ActiveSupport so ActiveSupport < 7.1 loads with concurrent-ruby 1.3.5+
13
+
1
14
  ## 0.6.4 / Feb 19 2022
2
15
  * Rails 7
3
16
  * Fix the name of the gemfile used by Appraisal
data/README.md CHANGED
@@ -4,7 +4,8 @@ Fluent logger.
4
4
 
5
5
  ## Supported versions
6
6
 
7
- * Rails 4, 5, 6, 7
7
+ * Rails 7.0, 7.1, 7.2, 8.0, 8.1
8
+ * Ruby 3.1+
8
9
 
9
10
  ## Installation
10
11
 
@@ -28,12 +29,16 @@ in config/environments/production.rb
28
29
  config.logger = ActFluentLoggerRails::Logger.
29
30
  new(log_tags: {
30
31
  ip: :ip,
31
- ua: :user_agent,
32
- uid: ->(request) { request.session[:uid] }
32
+ ua: :user_agent
33
33
  })
34
34
 
35
35
  Don't use config.log_tags.
36
36
 
37
+ `log_tags` are evaluated by `Rails::Rack::Logger` at the start of each request,
38
+ before the session and cookie middleware run, so they cannot read the session
39
+ (see #41). To log session values such as a user id, set them at run time from
40
+ the controller instead (see below).
41
+
37
42
  ### To define where to send messages to, either:
38
43
 
39
44
  #### create config/fluent-logger.yml
@@ -93,10 +98,22 @@ Don't use config.log_tags.
93
98
  <pre>tls_options:
94
99
  use_default_ca: true</pre>
95
100
 
96
- You can add any tags at run time.
101
+ You can add any tags at run time. They are sent with the rest of the request's
102
+ messages and cleared when the messages are flushed.
97
103
 
98
104
  logger[:foo] = "foo value"
99
105
 
106
+ For example, to log a user id stored in the session:
107
+
108
+ ```ruby
109
+ class ApplicationController < ActionController::Base
110
+ before_action { logger[:uid] = session[:uid] }
111
+ end
112
+ ```
113
+
114
+ With `flush_immediately: true` the tags are cleared after each message, so only
115
+ the next message carries them.
116
+
100
117
  ### Usage as a standalone logger
101
118
 
102
119
  Typical usage is as a replacement for the default Rails logger, in which case
@@ -1,7 +1,14 @@
1
1
  require 'fluent-logger'
2
- require 'active_support/core_ext'
2
+ # Require only what this file uses. Loading all of active_support/core_ext during
3
+ # Bundler.require would preempt app config such as Rails 7.0's disable_to_s_conversion.
4
+ require 'logger'
5
+ require 'active_support'
6
+ require 'active_support/logger'
7
+ require 'active_support/tagged_logging'
8
+ require 'active_support/core_ext/object/blank'
9
+ require 'active_support/core_ext/object/try'
10
+ require 'active_support/core_ext/hash/keys'
3
11
  require 'uri'
4
- require 'cgi'
5
12
  require 'rails/version'
6
13
 
7
14
  module ActFluentLoggerRails
@@ -15,16 +22,19 @@ module ActFluentLoggerRails
15
22
  log_tags: {},
16
23
  settings: {},
17
24
  flush_immediately: false)
18
- Rails.application.config.log_tags = log_tags.values
19
- if Rails.application.config.respond_to?(:action_cable)
20
- Rails.application.config.action_cable.log_tags = log_tags.values.map do |x|
21
- case
22
- when x.respond_to?(:call)
23
- x
24
- when x.is_a?(Symbol)
25
- -> (request) { request.send(x) }
26
- else
27
- -> (request) { x }
25
+ # A second logger without log_tags (e.g. one for background jobs) shouldn't clear the app's tags.
26
+ unless log_tags.empty?
27
+ Rails.application.config.log_tags = log_tags.values
28
+ if Rails.application.config.respond_to?(:action_cable)
29
+ Rails.application.config.action_cable.log_tags = log_tags.values.map do |x|
30
+ case
31
+ when x.respond_to?(:call)
32
+ x
33
+ when x.is_a?(Symbol)
34
+ -> (request) { request.send(x) }
35
+ else
36
+ -> (request) { x }
37
+ end
28
38
  end
29
39
  end
30
40
  end
@@ -55,30 +65,49 @@ module ActFluentLoggerRails
55
65
 
56
66
  def self.parse_url(fluentd_url)
57
67
  uri = URI.parse fluentd_url
58
- params = CGI.parse uri.query
68
+ # CGI.parse was removed from the default cgi library in Ruby 4.0. Keep the first value of duplicate keys like CGI.parse did.
69
+ params = URI.decode_www_form(uri.query.to_s).each_with_object({}) { |(key, value), h| h[key] ||= value }
59
70
 
60
71
  {
61
72
  fluent_host: uri.host,
62
73
  fluent_port: uri.port,
63
74
  tag: uri.path[1..-1],
64
- nanosecond_precision: params['nanosecond_precision'].try(:first),
65
- messages_type: params['messages_type'].try(:first),
66
- severity_key: params['severity_key'].try(:first),
75
+ nanosecond_precision: params['nanosecond_precision'],
76
+ messages_type: params['messages_type'],
77
+ severity_key: params['severity_key'],
67
78
  }.stringify_keys
68
79
  end
69
80
 
81
+ # Tags are matched to log_tags keys by position, so don't flatten Array values.
82
+ # Rails 7.0 passes them as one Array, 7.1 as separate arguments.
70
83
  def tagged(*tags)
71
- @tags_thread_key ||= "fluentd_tagged_logging_tags:#{object_id}".freeze
72
- Thread.current[@tags_thread_key] = tags.flatten
84
+ tags = tags.first if ActFluentLoggerRails::Logger.rails_passes_tags_as_array? && tags.length == 1 && tags.first.is_a?(Array)
85
+ self.request_tags = tags
73
86
  yield self
74
87
  ensure
75
88
  flush
76
89
  end
90
+
91
+ # Used instead of `tagged` by Rails 7.2+.
92
+ def push_tags(*tags)
93
+ self.request_tags = tags
94
+ tags
95
+ end
96
+
97
+ # Keep the tags until flush, which Rails calls right after this.
98
+ def pop_tags(count = 1)
99
+ []
100
+ end
101
+
102
+ def self.rails_passes_tags_as_array?
103
+ Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 0
104
+ end
77
105
  end
78
106
 
79
107
  class FluentLogger < ActiveSupport::Logger
80
108
  def initialize(options, level, log_tags)
81
- self.level = level
109
+ # No log device: messages go to fluentd in add/flush.
110
+ super(nil, level: level)
82
111
  port = options[:port]
83
112
  host = options[:host]
84
113
  nanosecond_precision = options[:nanosecond_precision]
@@ -91,7 +120,6 @@ module ActFluentLoggerRails
91
120
  @fluent_logger = ::Fluent::Logger::FluentLogger.new(nil, logger_opts)
92
121
  @severity = 0
93
122
  @log_tags = log_tags
94
- after_initialize if respond_to?(:after_initialize) && Rails::VERSION::MAJOR < 6
95
123
  end
96
124
 
97
125
  def add(severity, message = nil, progname = nil, &block)
@@ -147,17 +175,28 @@ module ActFluentLoggerRails
147
175
  @fluent_logger.post(@tag, map)
148
176
  @severity = 0
149
177
  logger_messages.clear
150
- Thread.current[@tags_thread_key] = nil if @tags_thread_key
178
+ self.request_tags = nil
151
179
  map.clear
152
180
  end
153
181
 
154
182
  def add_tags
155
- return unless @tags_thread_key && Thread.current.key?(@tags_thread_key)
156
- @log_tags.keys.zip(Thread.current[@tags_thread_key]).each do |k, v|
183
+ tags = request_tags
184
+ return unless tags
185
+ @log_tags.keys.zip(tags).each do |k, v|
157
186
  map[k] = v
158
187
  end
159
188
  end
160
189
 
190
+ def request_tags
191
+ @tags_thread_key ||= "fluentd_tagged_logging_tags:#{object_id}".freeze
192
+ Thread.current[@tags_thread_key]
193
+ end
194
+
195
+ def request_tags=(tags)
196
+ @tags_thread_key ||= "fluentd_tagged_logging_tags:#{object_id}".freeze
197
+ Thread.current[@tags_thread_key] = tags
198
+ end
199
+
161
200
  def logger_messages
162
201
  @messages_thread_key ||= "fluentd_logger_messages:#{object_id}".freeze
163
202
  Thread.current[@messages_thread_key] ||= []
@@ -172,14 +211,6 @@ module ActFluentLoggerRails
172
211
  @fluent_logger.close
173
212
  end
174
213
 
175
- def level
176
- @level
177
- end
178
-
179
- def level=(l)
180
- @level = l
181
- end
182
-
183
214
  def format_severity(severity)
184
215
  ActFluentLoggerRails::Logger::SEV_LABEL[severity] || 'ANY'
185
216
  end
@@ -1,3 +1,3 @@
1
1
  module ActFluentLoggerRails
2
- VERSION = "0.6.4"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act-fluent-logger-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAHARA Yoshinori
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-02-19 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rspec
@@ -16,16 +15,30 @@ dependencies:
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: 3.5.0
18
+ version: '3.13'
20
19
  type: :development
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: 3.5.0
25
+ version: '3.13'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: fluent-logger
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.9.1
40
+ - !ruby/object:Gem::Dependency
41
+ name: logger
29
42
  requirement: !ruby/object:Gem::Requirement
30
43
  requirements:
31
44
  - - ">="
@@ -44,28 +57,28 @@ dependencies:
44
57
  requirements:
45
58
  - - ">="
46
59
  - !ruby/object:Gem::Version
47
- version: '4'
60
+ version: '7.0'
48
61
  type: :runtime
49
62
  prerelease: false
50
63
  version_requirements: !ruby/object:Gem::Requirement
51
64
  requirements:
52
65
  - - ">="
53
66
  - !ruby/object:Gem::Version
54
- version: '4'
67
+ version: '7.0'
55
68
  - !ruby/object:Gem::Dependency
56
69
  name: activesupport
57
70
  requirement: !ruby/object:Gem::Requirement
58
71
  requirements:
59
72
  - - ">="
60
73
  - !ruby/object:Gem::Version
61
- version: '4'
74
+ version: '7.0'
62
75
  type: :runtime
63
76
  prerelease: false
64
77
  version_requirements: !ruby/object:Gem::Requirement
65
78
  requirements:
66
79
  - - ">="
67
80
  - !ruby/object:Gem::Version
68
- version: '4'
81
+ version: '7.0'
69
82
  description: Fluent logger
70
83
  email:
71
84
  - read.eval.print@gmail.com
@@ -73,31 +86,16 @@ executables: []
73
86
  extensions: []
74
87
  extra_rdoc_files: []
75
88
  files:
76
- - ".github/workflows/ci.yml"
77
- - ".gitignore"
78
- - Appraisals
79
89
  - CHANGELOG.md
80
- - Gemfile
81
90
  - LICENSE.txt
82
91
  - README.md
83
- - Rakefile
84
- - act-fluent-logger-rails.gemspec
85
- - gemfiles/rails_5_0.gemfile
86
- - gemfiles/rails_5_1.gemfile
87
- - gemfiles/rails_5_2.gemfile
88
- - gemfiles/rails_6_0.gemfile
89
- - gemfiles/rails_6_1.gemfile
90
- - gemfiles/rails_7_0.gemfile
91
92
  - lib/act-fluent-logger-rails.rb
92
93
  - lib/act-fluent-logger-rails/logger.rb
93
94
  - lib/act-fluent-logger-rails/version.rb
94
- - spec/logger_spec.rb
95
- - spec/spec_helper.rb
96
95
  homepage: https://github.com/actindi/act-fluent-logger-rails
97
96
  licenses:
98
97
  - MIT
99
98
  metadata: {}
100
- post_install_message:
101
99
  rdoc_options: []
102
100
  require_paths:
103
101
  - lib
@@ -105,17 +103,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
103
  requirements:
106
104
  - - ">="
107
105
  - !ruby/object:Gem::Version
108
- version: '0'
106
+ version: '3.1'
109
107
  required_rubygems_version: !ruby/object:Gem::Requirement
110
108
  requirements:
111
109
  - - ">="
112
110
  - !ruby/object:Gem::Version
113
111
  version: '0'
114
112
  requirements: []
115
- rubygems_version: 3.2.32
116
- signing_key:
113
+ rubygems_version: 4.0.20
117
114
  specification_version: 4
118
115
  summary: Fluent logger
119
- test_files:
120
- - spec/logger_spec.rb
121
- - spec/spec_helper.rb
116
+ test_files: []
@@ -1,36 +0,0 @@
1
- name: CI
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- build:
7
- strategy:
8
- fail-fast: false
9
- matrix:
10
- os: [ubuntu-latest]
11
- ruby: [2.4, 2.5, 2.6, 2.7, 3.0, 3.1, head]
12
- gemfile: [rails_5_0, rails_5_1, rails_5_2, rails_6_0, rails_6_1, rails_7_0]
13
- exclude:
14
- - ruby: 2.4
15
- gemfile: rails_6_0
16
- - ruby: 2.4
17
- gemfile: rails_6_1
18
- - ruby: 2.4
19
- gemfile: rails_7_0
20
- - ruby: 2.5
21
- gemfile: rails_7_0
22
- - ruby: 2.6
23
- gemfile: rails_7_0
24
- runs-on: ${{ matrix.os }}
25
- env:
26
- BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
27
- steps:
28
- - uses: actions/checkout@v2
29
- - name: Set up Ruby
30
- uses: ruby/setup-ruby@v1
31
- with:
32
- ruby-version: ${{ matrix.ruby }}
33
- bundler-cache: true
34
- - name: Test
35
- run: |
36
- bundle exec rake spec
data/.gitignore DELETED
@@ -1,22 +0,0 @@
1
- *.gem
2
- *.rbc
3
- *~
4
- .bundle
5
- .config
6
- .yardoc
7
- /vendor
8
- Gemfile.lock
9
- gemfiles/*.lock
10
- gemfiles/vendor
11
- InstalledFiles
12
- _yardoc
13
- coverage
14
- doc/
15
- lib/bundler/man
16
- pkg
17
- rdoc
18
- spec/reports
19
- test/tmp
20
- test/version_tmp
21
- tmp
22
- .ruby-version
data/Appraisals DELETED
@@ -1,29 +0,0 @@
1
- appraise 'rails-5_0' do
2
- gem 'railties', '5.0.2'
3
- gem 'activesupport', '5.0.2'
4
- end
5
-
6
- appraise 'rails-5_1' do
7
- gem 'railties', '5.1.0'
8
- gem 'activesupport', '5.1.0'
9
- end
10
-
11
- appraise 'rails-5_2' do
12
- gem 'railties', '5.2.0'
13
- gem 'activesupport', '5.2.0'
14
- end
15
-
16
- appraise 'rails-6_0' do
17
- gem 'railties', '6.0.3.4'
18
- gem 'activesupport', '6.0.3.4'
19
- end
20
-
21
- appraise 'rails-6_1' do
22
- gem 'railties', '6.1.0'
23
- gem 'activesupport', '6.1.0'
24
- end
25
-
26
- appraise 'rails-7_0' do
27
- gem 'railties', '7.0.2'
28
- gem 'activesupport', '7.0.2'
29
- end
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'appraisal'
4
-
5
- # Specify your gem's dependencies in act-fluent-logger-rails.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,25 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'act-fluent-logger-rails/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "act-fluent-logger-rails"
8
- gem.version = ActFluentLoggerRails::VERSION
9
- gem.authors = ["TAHARA Yoshinori"]
10
- gem.email = ["read.eval.print@gmail.com"]
11
- gem.description = %q{Fluent logger}
12
- gem.summary = %q{Fluent logger}
13
- gem.homepage = "https://github.com/actindi/act-fluent-logger-rails"
14
- gem.license = "MIT"
15
-
16
- gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
- gem.require_paths = ["lib"]
20
-
21
- gem.add_development_dependency "rspec", '~> 3.5.0'
22
- gem.add_runtime_dependency "fluent-logger"
23
- gem.add_runtime_dependency "railties", ">= 4"
24
- gem.add_runtime_dependency "activesupport", ">= 4"
25
- end
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "5.0.2"
7
- gem "activesupport", "5.0.2"
8
-
9
- gemspec path: "../"
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "5.1.0"
7
- gem "activesupport", "5.1.0"
8
-
9
- gemspec path: "../"
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "5.2.0"
7
- gem "activesupport", "5.2.0"
8
-
9
- gemspec path: "../"
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "6.0.3.4"
7
- gem "activesupport", "6.0.3.4"
8
-
9
- gemspec path: "../"
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "6.1.0"
7
- gem "activesupport", "6.1.0"
8
-
9
- gemspec path: "../"
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "railties", "7.0.2"
7
- gem "activesupport", "7.0.2"
8
-
9
- gemspec path: "../"
data/spec/logger_spec.rb DELETED
@@ -1,265 +0,0 @@
1
- require 'spec_helper'
2
- require 'tempfile'
3
-
4
-
5
- describe ActFluentLoggerRails::Logger do
6
- before do
7
- stub_const('Rails', Class.new) unless defined?(Rails)
8
- allow(Rails).to receive(:env).and_return('test')
9
- allow(Rails).to receive_message_chain(:application, :config, :log_level).and_return(:debug)
10
- allow(Rails).to receive_message_chain(:application, :config, :log_tags=)
11
-
12
- class MyLogger
13
- attr_accessor :log
14
- def post(tag, map)
15
- @log ||= []
16
- @log << [tag, map.merge(messages: map[:messages].dup)]
17
- end
18
- def clear
19
- @log.clear
20
- end
21
- def close
22
- end
23
- end
24
- @my_logger = MyLogger.new
25
- allow(Fluent::Logger::FluentLogger).to receive(:new).and_return(@my_logger)
26
-
27
- @config_file = Tempfile.new('fluent-logger-config')
28
- @config_file.close(false)
29
- File.open(@config_file.path, 'w') {|f|
30
- f.puts <<EOF
31
- test:
32
- fluent_host: '127.0.0.1'
33
- fluent_port: 24224
34
- tag: 'foo'
35
- EOF
36
- }
37
- end
38
-
39
- let(:log_tags) {
40
- { uuid: :uuid,
41
- foo: ->(request) { 'foo_value' }
42
- }
43
- }
44
-
45
- let(:logger) {
46
- ActFluentLoggerRails::Logger.new(config_file: File.new(@config_file.path),
47
- log_tags: log_tags)
48
- }
49
-
50
- let(:request) {
51
- double('request', uuid: 'uuid_value')
52
- }
53
-
54
- describe 'logging' do
55
- describe 'basic' do
56
- it 'info' do
57
- # see Rails::Rack::compute_tags
58
- tags = log_tags.values.collect do |tag|
59
- case tag
60
- when Proc
61
- tag.call(request)
62
- when Symbol
63
- request.send(tag)
64
- else
65
- tag
66
- end
67
- end
68
- logger[:abc] = 'xyz'
69
- logger.tagged(tags) { logger.info('hello') }
70
- expect(@my_logger.log).to eq([['foo', {
71
- abc: 'xyz',
72
- messages: ['hello'],
73
- severity: 'INFO',
74
- uuid: 'uuid_value',
75
- foo: 'foo_value'
76
- } ]])
77
- @my_logger.clear
78
- logger.tagged(tags) { logger.info('world'); logger.info('bye') }
79
- expect(@my_logger.log).to eq([['foo', {
80
- messages: ['world', 'bye'],
81
- severity: 'INFO',
82
- uuid: 'uuid_value',
83
- foo: 'foo_value'
84
- } ]])
85
- end
86
- end
87
-
88
- it 'is thread safe' do
89
- threads = ['hello', 'world'].map do |tag_name|
90
- Thread.new {
91
- if tag_name == 'hello'
92
- request = double('request', uuid: tag_name)
93
- tags = log_tags.values.collect do |tag|
94
- case tag
95
- when Proc
96
- tag.call(request)
97
- when Symbol
98
- request.send(tag)
99
- else
100
- tag
101
- end
102
- end
103
- else
104
- tags = []
105
- end
106
- logger.info(tag_name)
107
- logger.tagged(tags) { sleep(1) if tag_name == 'hello'; logger.info(tag_name) }
108
- }
109
- end
110
-
111
- while threads.any?(&:alive?)
112
- sleep(0.1)
113
- end
114
- expect(@my_logger.log).to match_array([
115
- ['foo', { messages: ['hello', 'hello'], severity: 'INFO', uuid: 'hello', foo: 'foo_value' } ],
116
- ['foo', { messages: ['world', 'world'], severity: 'INFO', uuid: nil, foo: nil } ]
117
- ])
118
- end
119
-
120
-
121
- describe 'frozen ascii-8bit string' do
122
- before do
123
- logger.instance_variable_set(:@messages_type, :string)
124
- end
125
-
126
- after do
127
- logger.instance_variable_set(:@messages_type, :array)
128
- end
129
-
130
- it 'join messages' do
131
- ascii = "\xe8\x8a\xb1".force_encoding('ascii-8bit').freeze
132
- logger.tagged([request]) {
133
- logger.info(ascii)
134
- logger.info('咲く')
135
- }
136
- expect(@my_logger.log[0][1][:messages]).to eq("花\n咲く")
137
- expect(ascii.encoding).to eq(Encoding::ASCII_8BIT)
138
- end
139
- end
140
-
141
- describe 'Exception' do
142
- it 'output message, class, backtrace' do
143
- begin
144
- 3 / 0
145
- rescue => e
146
- logger.tagged([request]) {
147
- logger.error(e)
148
- }
149
- expect(@my_logger.log[0][1][:messages][0]).
150
- to match(%r|divided by 0 \(ZeroDivisionError\).*spec/logger_spec\.rb:|m)
151
- end
152
- end
153
- end
154
-
155
- describe 'Object' do
156
- it 'output inspect' do
157
- x = Object.new
158
- logger.tagged([request]) {
159
- logger.info(x)
160
- }
161
- expect(@my_logger.log[0][1][:messages][0]).to eq(x.inspect)
162
- end
163
- end
164
-
165
- describe 'tls_options' do
166
- context 'does not has key' do
167
- before do
168
- File.open(@config_file.path, 'w') do |f|
169
- f.puts <<EOF
170
- test:
171
- fluent_host: '127.0.0.1'
172
- fluent_port: 24224
173
- tag: 'foo'
174
- EOF
175
- end
176
- end
177
- it do
178
- expect(::Fluent::Logger::FluentLogger).not_to receive(:new).with(nil, hash_including(tls_options: { use_default_ca: true }))
179
- logger
180
- end
181
- end
182
- context 'has key' do
183
- before do
184
- File.open(@config_file.path, 'w') do |f|
185
- f.puts <<EOF
186
- test:
187
- fluent_host: '127.0.0.1'
188
- fluent_port: 24224
189
- tag: 'foo'
190
- tls_options:
191
- use_default_ca: true
192
- EOF
193
- end
194
- end
195
- it do
196
- expect(::Fluent::Logger::FluentLogger).to receive(:new).with(nil, hash_including(tls_options: { use_default_ca: true }))
197
- logger
198
- end
199
- end
200
- end
201
-
202
- describe 'severity_key' do
203
- describe 'not specified' do
204
- it 'severity' do
205
- logger.tagged([request]) { logger.info('hello') }
206
- expect(@my_logger.log[0][1][:severity]).to eq('INFO')
207
- end
208
- end
209
-
210
- describe 'severity_key: level' do
211
- before do
212
- @config_file = Tempfile.new('fluent-logger-config')
213
- @config_file.close(false)
214
- File.open(@config_file.path, 'w') {|f|
215
- f.puts <<EOF
216
- test:
217
- fluent_host: '127.0.0.1'
218
- fluent_port: 24224
219
- tag: 'foo'
220
- severity_key: 'level' # default severity
221
- EOF
222
- }
223
- end
224
-
225
- it 'level' do
226
- logger.tagged([request]) { logger.info('hello') }
227
- expect(@my_logger.log[0][1][:level]).to eq('INFO')
228
- end
229
- end
230
- end
231
- end
232
-
233
- describe "use ENV['FLUENTD_URL']" do
234
- let(:fluentd_url) { "http://fluentd.example.com:42442/hoge?messages_type=string&severity_key=level" }
235
-
236
- describe ".parse_url" do
237
- subject { described_class.parse_url(fluentd_url) }
238
- it { expect(subject['tag']).to eq 'hoge' }
239
- it { expect(subject['fluent_host']).to eq 'fluentd.example.com' }
240
- it { expect(subject['fluent_port']).to eq 42442 }
241
- it { expect(subject['messages_type']).to eq 'string' }
242
- it { expect(subject['severity_key']).to eq 'level' }
243
- end
244
- end
245
-
246
- describe 'flush_immediately' do
247
- describe 'flush_immediately: true' do
248
- it 'flushed' do
249
- logger = ActFluentLoggerRails::Logger.new(config_file: File.new(@config_file.path),
250
- flush_immediately: true)
251
- logger.info('Immediately!')
252
- expect(@my_logger.log[0][1][:messages][0]).to eq('Immediately!')
253
- end
254
- end
255
-
256
- describe 'flush_immediately: false' do
257
- it 'flushed' do
258
- logger = ActFluentLoggerRails::Logger.new(config_file: File.new(@config_file.path),
259
- flush_immediately: false)
260
- logger.info('Immediately!')
261
- expect(@my_logger.log).to eq(nil)
262
- end
263
- end
264
- end
265
- end
data/spec/spec_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
-
4
- require 'active_support'
5
- require 'active_support/deprecation'
6
- require 'active_support/core_ext/module'
7
- require 'active_support/logger'
8
- require 'active_support/tagged_logging'
9
- require 'yaml'
10
- require 'act-fluent-logger-rails/logger'