structured-event-logger 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDcyNDFkNzJlNWFlYTc0NzUxNDE5YzNhMmE3MjgzNmRiOTUwYTlmYw==
5
+ data.tar.gz: !binary |-
6
+ YTE4MDdiMDZjNWJmOTAwODk2YTE5YWJiZTMwMjQ1YjA0N2I4NjQ1MA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTQwNmQzNGJmMjdjNTZiY2I3ZjYwMjA5NWY4ZGE0NjY0MTk3MWJjM2U1ZWI5
10
+ OGM2Mzc0MjA1NDdmOTE3NjFjYmJhN2ZmNjI4OGNlN2RjYjk3OTk0YmQ4NzU2
11
+ MWE0MmRmY2VhOTMzYjczYWYzMmZkMjE0NDhmMzdkYjNiMzc0ZDc=
12
+ data.tar.gz: !binary |-
13
+ NWZmMTMyMWU2NWVmZjBiMjJlOTNhODQ0NjVjMmQyM2ZiYmFhOWU2MjM0Mzhi
14
+ YzVlYWFhNDdkZmYxMGY3OTliNzZiNjMzM2VjMmQ0MDRkOTMxNmJlMzIwNGQw
15
+ OTQwOGZjNzU2NjA3MmY1NGM3MzlmNjIxMWYzYThlNjJjNWU0NmI=
data/README.md CHANGED
@@ -19,23 +19,23 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  # Creating an instance
22
- json_logger = Logger.new('events.log')
22
+ json_logger = File.open(Rails.root.join("log", "event.log"), "a")
23
23
  human_readable_logger = Rails.logger
24
24
  event_logger = StructuredEventLogger.new(json_logger, human_readable_logger)
25
25
 
26
26
  # Basic usage
27
- event_logger.event('scope', event, field: 'value', other_field, 'other value')
27
+ event_logger.event('scope', event, field: 'value', other_field: 'other value')
28
28
 
29
29
  # Add context per thread/request (e.g. in an around_filter)
30
30
  around_filter do
31
- event_logger.add_context(my_value: 'whatever')
31
+ event_logger.context[:my_value] = 'whatever'
32
32
  yield
33
- event_logger.delete_context
33
+ event_logger.context.delete(:my_value)
34
34
  end
35
-
35
+
36
36
  # later, while processing a request inside that filter
37
37
  event_logger.event('scope', 'event', other_value: 'blah') # will also include { my_value: 'whatever' }
38
-
38
+
39
39
  ## Contributing
40
40
 
41
41
  1. Fork it
data/Rakefile CHANGED
@@ -1,6 +1,17 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
-
3
+ require 'rubygems/package_task'
4
+
5
+ gemspec = eval(File.read('structured-event-logger.gemspec'))
6
+ Gem::PackageTask.new(gemspec) do |pkg|
7
+ pkg.gem_spec = gemspec
8
+ end
9
+
10
+ desc "Build the gem and release it to rubygems.org"
11
+ task :release => :gem do
12
+ sh "gem push pkg/structured-event-logger-#{gemspec.version}.gem"
13
+ end
14
+
4
15
  Rake::TestTask.new(:test) do |t|
5
16
  t.test_files = Dir.glob('test/**/*_test.rb')
6
17
  t.libs << 'test'
@@ -11,11 +11,12 @@ class StructuredEventLogger
11
11
  CYAN = "\e[36m"
12
12
  WHITE = "\e[37m"
13
13
 
14
- attr_reader :json_io, :unstructured_logger, :colorize_logging
14
+ attr_reader :json_io, :unstructured_logger, :colorize_logging, :default_context
15
15
 
16
16
  def initialize(json_io, unstructured_logger = nil)
17
17
  @json_io, @unstructured_logger = json_io, unstructured_logger
18
18
  @thread_contexts = {}
19
+ @default_context = {}
19
20
  @colorize_logging = ActiveSupport::LogSubscriber.colorize_logging
20
21
  end
21
22
 
@@ -68,7 +69,7 @@ class StructuredEventLogger
68
69
  def log_event(scope, event, hash)
69
70
  unstructured_logger.add(nil, format_hash(scope, event, hash)) if unstructured_logger
70
71
 
71
- hash = hash.merge(context)
72
+ hash = hash.merge(@default_context.merge(context))
72
73
  hash.update(event: event, scope: scope, timestamp: Time.now.utc)
73
74
  json_io.write("#{MultiJson.encode(hash)}\n")
74
75
  end
@@ -1,3 +1,3 @@
1
1
  class StructuredEventLogger
2
- VERSION = "0.0.2"
3
- end
2
+ VERSION = "0.0.3"
3
+ end
@@ -9,7 +9,7 @@ class StructuredEventLoggerTest < Minitest::Test
9
9
  @unstructured_logger = Logger.new(@nonstructured_io = StringIO.new)
10
10
  @unstructured_logger.formatter = proc { |_, _, _, msg| "#{msg}\n" }
11
11
  @event_logger = StructuredEventLogger.new(@json_io, @unstructured_logger)
12
- @time = Time.parse('2012-01-01')
12
+ @time = Time.parse('2012-01-01T05:00:00Z')
13
13
  end
14
14
 
15
15
  def test_should_log_msg_to_buffered_logger
@@ -50,10 +50,31 @@ class StructuredEventLoggerTest < Minitest::Test
50
50
  assert_equal "{\"request_id\":\"2\",\"event\":\"error\",\"scope\":\"render\",\"timestamp\":\"2012-01-01T05:00:00Z\"}\n", @json_io.string
51
51
  end
52
52
 
53
- def test_should_clear_context
53
+ def assert_event_contains_value(value, key)
54
+ @event_logger.event :some_scope, :some_event
55
+ assert_equal value, JSON.parse(@json_io.string)[key.to_s]
56
+ end
57
+
58
+ def test_default_context_gets_merged
59
+ @event_logger.default_context[:foo] = 42
60
+ assert_event_contains_value 42, :foo
61
+ end
62
+
63
+ def test_default_context_values_can_be_overriden
64
+ @event_logger.default_context[:foo] = 42
65
+ @event_logger.context[:foo] = 43
66
+ assert_event_contains_value 43, :foo
67
+ end
68
+
69
+ def test_default_context_gets_merged_again_after_clear
70
+ @event_logger.default_context[:foo] = 42
71
+ @event_logger.context.clear
72
+ assert_event_contains_value 42, :foo
73
+ end
74
+
75
+ def test_should_clear_context
54
76
  Timecop.travel(@time) do
55
-
56
- Thread.new do
77
+ Thread.new do
57
78
  @event_logger.context[:request_id] = '1'
58
79
  @event_logger.event :render, :in_thread
59
80
  @event_logger.context.clear
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structured-event-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Emilie Noel
@@ -11,12 +10,11 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-07-19 00:00:00.000000000 Z
13
+ date: 2013-08-08 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activesupport
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
19
  - - ~>
22
20
  - !ruby/object:Gem::Version
@@ -24,7 +22,6 @@ dependencies:
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
26
  - - ~>
30
27
  - !ruby/object:Gem::Version
@@ -32,7 +29,6 @@ dependencies:
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: multi_json
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
33
  - - ! '>='
38
34
  - !ruby/object:Gem::Version
@@ -40,7 +36,6 @@ dependencies:
40
36
  type: :runtime
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
40
  - - ! '>='
46
41
  - !ruby/object:Gem::Version
@@ -48,7 +43,6 @@ dependencies:
48
43
  - !ruby/object:Gem::Dependency
49
44
  name: bundler
50
45
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
46
  requirements:
53
47
  - - ~>
54
48
  - !ruby/object:Gem::Version
@@ -56,7 +50,6 @@ dependencies:
56
50
  type: :development
57
51
  prerelease: false
58
52
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
53
  requirements:
61
54
  - - ~>
62
55
  - !ruby/object:Gem::Version
@@ -64,7 +57,6 @@ dependencies:
64
57
  - !ruby/object:Gem::Dependency
65
58
  name: rake
66
59
  requirement: !ruby/object:Gem::Requirement
67
- none: false
68
60
  requirements:
69
61
  - - ! '>='
70
62
  - !ruby/object:Gem::Version
@@ -72,7 +64,6 @@ dependencies:
72
64
  type: :development
73
65
  prerelease: false
74
66
  version_requirements: !ruby/object:Gem::Requirement
75
- none: false
76
67
  requirements:
77
68
  - - ! '>='
78
69
  - !ruby/object:Gem::Version
@@ -80,7 +71,6 @@ dependencies:
80
71
  - !ruby/object:Gem::Dependency
81
72
  name: minitest
82
73
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
74
  requirements:
85
75
  - - ~>
86
76
  - !ruby/object:Gem::Version
@@ -88,7 +78,6 @@ dependencies:
88
78
  type: :development
89
79
  prerelease: false
90
80
  version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
81
  requirements:
93
82
  - - ~>
94
83
  - !ruby/object:Gem::Version
@@ -96,7 +85,6 @@ dependencies:
96
85
  - !ruby/object:Gem::Dependency
97
86
  name: timecop
98
87
  requirement: !ruby/object:Gem::Requirement
99
- none: false
100
88
  requirements:
101
89
  - - ! '>='
102
90
  - !ruby/object:Gem::Version
@@ -104,7 +92,6 @@ dependencies:
104
92
  type: :development
105
93
  prerelease: false
106
94
  version_requirements: !ruby/object:Gem::Requirement
107
- none: false
108
95
  requirements:
109
96
  - - ! '>='
110
97
  - !ruby/object:Gem::Version
@@ -131,27 +118,26 @@ files:
131
118
  homepage: https://github.com/Shopify/structured-event-logger
132
119
  licenses:
133
120
  - MIT
121
+ metadata: {}
134
122
  post_install_message:
135
123
  rdoc_options: []
136
124
  require_paths:
137
125
  - lib
138
126
  required_ruby_version: !ruby/object:Gem::Requirement
139
- none: false
140
127
  requirements:
141
128
  - - ! '>='
142
129
  - !ruby/object:Gem::Version
143
130
  version: '0'
144
131
  required_rubygems_version: !ruby/object:Gem::Requirement
145
- none: false
146
132
  requirements:
147
133
  - - ! '>='
148
134
  - !ruby/object:Gem::Version
149
135
  version: '0'
150
136
  requirements: []
151
137
  rubyforge_project:
152
- rubygems_version: 1.8.23
138
+ rubygems_version: 2.0.6
153
139
  signing_key:
154
- specification_version: 3
140
+ specification_version: 4
155
141
  summary: Structured event logger that writes events to both a human readable log and
156
142
  a JSON formatted log
157
143
  test_files: