act-fluent-logger-rails 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 9e28f2538881b7eb28b8f09685cc092a4b602e5e
4
- data.tar.gz: 236175fa5424912c12231b24f16ae47dd187832f
3
+ metadata.gz: ba6d73a346e319350b3656f24a42809e20b161a6
4
+ data.tar.gz: eff76247f67cbe7aa92c4f396c598ec42922502f
5
5
  SHA512:
6
- metadata.gz: e302fef91fb4d21bfd52b92e029da7d14a129cab26eee6640bf96006378da41901c35b8d99128120103b28596fb1820c869f4e89d22992456e37d9a0beba7eb9
7
- data.tar.gz: 9c772565e1c5db6a976501aaa73a92d476b5098da7d25cad2df50496e2106c83f4381577e8e121c384c8837d0bcec4234f33e9afd543a6d8631cf5e3b5b75d40
6
+ metadata.gz: 040c190bfea376ec3456569668fbc3aaaacefabb76d54e86bca4d1f41a98f49b56109b3f2847e26e13fadc23db21ba2ed30cf8ba32cb54f38213ae6d91cfdb76
7
+ data.tar.gz: 65317b818e16de0efe43cbcc12d3ffdb5d6b7dc9ee2bd643ba07048aa1ac14390cb8defb1735b724a7454e5898fd882f23541f637b6714ad252ec377fa04ff0e
data/.gitignore CHANGED
@@ -1,8 +1,10 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *~
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
7
+ /vendor
6
8
  Gemfile.lock
7
9
  InstalledFiles
8
10
  _yardoc
@@ -15,4 +17,3 @@ spec/reports
15
17
  test/tmp
16
18
  test/version_tmp
17
19
  tmp
18
- *~
@@ -0,0 +1 @@
1
+ 2.0.0-p195
@@ -1,3 +1,7 @@
1
+ ## 0.1.1 / September 26 2013
2
+
3
+ * Add log_tags feature.
4
+
1
5
  ## 0.1.0 / September 16 2013
2
6
 
3
7
  * Rails 4.0.0
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Fluent logger.
4
4
 
5
+ ## Supported versions
6
+
7
+ * Rails 4.0
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -21,7 +25,14 @@ Or install it yourself as:
21
25
  in config/environments/production.rb
22
26
 
23
27
  config.log_level = :info
24
- config.logger = ActFluentLoggerRails::Logger.new
28
+ config.logger = ActFluentLoggerRails::Logger.
29
+ new(log_tags: {
30
+ ip: :ip,
31
+ ua: :user_agent,
32
+ uid: ->(request) { request.session[:uid] }
33
+ })
34
+
35
+ Don't use config.log_tags.
25
36
 
26
37
  create config/fluent-logger.yml
27
38
 
@@ -56,9 +67,9 @@ create config/fluent-logger.yml
56
67
  2013-01-18T15:04:50+09:00 foo {"messages":["Started GET \"/\" for 127.0.0.1 at 2013-01-18 15:04:49 +0900","Processing by TopController#index as HTML","Completed 200 OK in 635ms (Views: 479.3ms | ActiveRecord: 39.6ms)"],"level":"INFO"}
57
68
  ```
58
69
 
59
- If your Rails version is older than v3.2.9, You must set a dummy value to config.log_tags.
70
+ You can add any tags at run time.
60
71
 
61
- config.log_tags = ['nothing']
72
+ logger[:foo] = "foo value"
62
73
 
63
74
 
64
75
  ## Contributing
@@ -8,8 +8,8 @@ module ActFluentLoggerRails
8
8
  # Severity label for logging. (max 5 char)
9
9
  SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
10
10
 
11
- def self.new
12
- config_file = Rails.root.join("config", "fluent-logger.yml")
11
+ def self.new(config_file: Rails.root.join("config", "fluent-logger.yml"), log_tags: {})
12
+ Rails.application.config.log_tags = [ ->(request) { request } ] unless log_tags.empty?
13
13
  fluent_config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
14
14
  settings = {
15
15
  tag: fluent_config['tag'],
@@ -18,31 +18,21 @@ module ActFluentLoggerRails
18
18
  messages_type: fluent_config['messages_type'],
19
19
  }
20
20
  level = SEV_LABEL.index(Rails.application.config.log_level.to_s.upcase)
21
- logger = ActFluentLoggerRails::FluentLogger.new(settings, level)
22
- logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
23
- logger.formatter.extend ActiveSupport::TaggedLogging::Formatter
24
- logger.extend ActiveSupport::TaggedLogging
21
+ logger = ActFluentLoggerRails::FluentLogger.new(settings, level, log_tags)
22
+ logger = ActiveSupport::TaggedLogging.new(logger)
25
23
  logger.extend self
26
24
  end
27
25
 
28
- def add(severity, message = nil, progname = nil, &block)
29
- return true if severity < level
30
- message = (block_given? ? block.call : progname) if message.blank?
31
- return true if message.blank?
32
- add_message(severity, message)
33
- true
34
- end
35
-
36
26
  def tagged(*tags)
37
- super(*tags)
27
+ @request = tags[0][0]
28
+ yield self
38
29
  ensure
39
30
  flush
40
31
  end
41
-
42
32
  end
43
33
 
44
34
  class FluentLogger < ActiveSupport::Logger
45
- def initialize(options, level=DEBUG)
35
+ def initialize(options, level, log_tags)
46
36
  self.level = level
47
37
  port = options[:port]
48
38
  host = options[:host]
@@ -51,6 +41,16 @@ module ActFluentLoggerRails
51
41
  @fluent_logger = ::Fluent::Logger::FluentLogger.new(nil, host: host, port: port)
52
42
  @severity = 0
53
43
  @messages = []
44
+ @log_tags = log_tags
45
+ @map = {}
46
+ end
47
+
48
+ def add(severity, message = nil, progname = nil, &block)
49
+ return true if severity < level
50
+ message = (block_given? ? block.call : progname) if message.blank?
51
+ return true if message.blank?
52
+ add_message(severity, message)
53
+ true
54
54
  end
55
55
 
56
56
  def add_message(severity, message)
@@ -58,6 +58,14 @@ module ActFluentLoggerRails
58
58
  @messages << message
59
59
  end
60
60
 
61
+ def [](key)
62
+ @map[key]
63
+ end
64
+
65
+ def []=(key, value)
66
+ @map[key] = value
67
+ end
68
+
61
69
  def flush
62
70
  return if @messages.empty?
63
71
  messages = if @messages_type == :string
@@ -65,9 +73,22 @@ module ActFluentLoggerRails
65
73
  else
66
74
  @messages
67
75
  end
68
- @fluent_logger.post(@tag, messages: messages, level: format_severity(@severity))
76
+ @map[:messages] = messages
77
+ @map[:level] = format_severity(@severity)
78
+ @log_tags.each do |k, v|
79
+ @map[k] = case v
80
+ when Proc
81
+ v.call(@request)
82
+ when Symbol
83
+ @request.send(v)
84
+ else
85
+ v
86
+ end rescue :error
87
+ end
88
+ @fluent_logger.post(@tag, @map)
69
89
  @severity = 0
70
90
  @messages.clear
91
+ @map.clear
71
92
  end
72
93
 
73
94
  def close
@@ -1,3 +1,3 @@
1
1
  module ActFluentLoggerRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+
5
+ describe ActFluentLoggerRails::Logger do
6
+ before do
7
+
8
+ Rails = double("Rails", env: "test")
9
+ Rails.stub_chain(:application, :config, :log_level).and_return(:debug)
10
+ Rails.stub_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
+ Fluent::Logger::FluentLogger.stub(:new) { @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(:logger) {
40
+ ActFluentLoggerRails::Logger.new(config_file: File.new(@config_file.path),
41
+ log_tags: {
42
+ uuid: :uuid,
43
+ foo: ->(request) { request.foo }
44
+ })
45
+ }
46
+
47
+ it 'info' do
48
+ request = double('request', uuid: 'uuid_value', foo: 'foo_value')
49
+ logger[:abc] = 'xyz'
50
+ logger.tagged([request]) { logger.info('hello') }
51
+ expect(@my_logger.log).to eq([['foo', {
52
+ abc: 'xyz',
53
+ messages: ['hello'],
54
+ level: 'INFO',
55
+ uuid: 'uuid_value',
56
+ foo: 'foo_value'
57
+ } ]])
58
+ @my_logger.clear
59
+ logger.tagged([request]) { logger.info('world'); logger.info('bye') }
60
+ expect(@my_logger.log).to eq([['foo', {
61
+ messages: ['world', 'bye'],
62
+ level: 'INFO',
63
+ uuid: 'uuid_value',
64
+ foo: 'foo_value'
65
+ } ]])
66
+ end
67
+ end
@@ -0,0 +1,10 @@
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'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act-fluent-logger-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAHARA Yoshinori
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2013-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
+ - .ruby-version
63
64
  - CHANGELOG.md
64
65
  - Gemfile
65
66
  - LICENSE.txt
@@ -69,6 +70,8 @@ files:
69
70
  - lib/act-fluent-logger-rails.rb
70
71
  - lib/act-fluent-logger-rails/logger.rb
71
72
  - lib/act-fluent-logger-rails/version.rb
73
+ - spec/logger_spec.rb
74
+ - spec/spec_helper.rb
72
75
  homepage: https://github.com/actindi/act-fluent-logger-rails
73
76
  licenses: []
74
77
  metadata: {}
@@ -92,4 +95,6 @@ rubygems_version: 2.0.3
92
95
  signing_key:
93
96
  specification_version: 4
94
97
  summary: Fluent logger
95
- test_files: []
98
+ test_files:
99
+ - spec/logger_spec.rb
100
+ - spec/spec_helper.rb