hatchet 0.2.11 → 0.2.12

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: a3dbb2119f1401a7452c4a9fc14d9c4d6116992e
4
- data.tar.gz: cf5a34782e06ff8d65104ac72c23d7d612f51263
3
+ metadata.gz: 8cbe273a0b241069c2de2607562f507b473bb2f8
4
+ data.tar.gz: 7fc75b084ea6f95ce11d3303d31a115eb11be899
5
5
  SHA512:
6
- metadata.gz: 705e5334495aedd841228eb8aa51e1393c8e98936d8be1783f92a5536d08d43e3ab1f6ba89ea3197641f82d9d5af065234fe47ff29afdca60d3bad87f7132828
7
- data.tar.gz: 4991d0d3761a20cb12eab7c45f4700bbc5f94a13607b1bf38f8320ae8ac9761e4081ab399f1bc3f84f127220021ddf1aba56aae436e4f6ae3150b54aa6bf713a
6
+ metadata.gz: ebf4aaeba91a3676b32c2227de90b1a140608020a2092b9ad232b83d6c62a4d19235151a2105dcf4b6390005220bafe1798839b13b5b053b79d0db291360cb9c
7
+ data.tar.gz: db44d98c902fabb1ad7d0231ab113acff8c6bbd99ecc5e17c9b8669386672a6424af0b1cb50eda30e1aefda01f847d1ac7c15d9d95fdd2a3dfe864009e7cdf64
data/RELEASE.md CHANGED
@@ -1,22 +1,26 @@
1
1
  # Release notes
2
2
 
3
+ ## 0.2.11
4
+
5
+ * Railtie no longer tries to replace the asset logger when it is not present
6
+
3
7
  ## 0.2.10
4
8
 
5
- * Introduced the ability to filter backtraces
9
+ * Introduced the ability to filter backtraces
6
10
 
7
- Example:
11
+ Example:
8
12
 
9
- configuration.configure do |config|
10
- config.backtrace_filter '/applications/my_app/releases/current' => '[ROOT]'
11
- end
13
+ configuration.configure do |config|
14
+ config.backtrace_filter '/applications/my_app/releases/current' => '[ROOT]'
15
+ end
12
16
 
13
- Will filter a backtrace line like:
17
+ Will filter a backtrace line like:
14
18
 
15
- /applications/my_app/releases/current/lib/example.rb:42:in `main'
19
+ /applications/my_app/releases/current/lib/example.rb:42:in `main'
16
20
 
17
- Into:
21
+ Into:
18
22
 
19
- [ROOT]/lib/example.rb:42:in `main'
23
+ [ROOT]/lib/example.rb:42:in `main'
20
24
 
21
25
  ## 0.2.9
22
26
 
@@ -12,6 +12,7 @@ require_relative 'hatchet/nested_diagnostic_context'
12
12
  require_relative 'hatchet/plain_formatter'
13
13
  require_relative 'hatchet/simple_formatter'
14
14
  require_relative 'hatchet/standard_formatter'
15
+ require_relative 'hatchet/structured_formatter'
15
16
  require_relative 'hatchet/version'
16
17
 
17
18
  # Public: Hatchet is a library for providing logging facilities whose levels are
@@ -421,7 +421,7 @@ module Hatchet
421
421
  @configuration.appenders.any? do |appender|
422
422
  begin
423
423
  appender.enabled? level, @context
424
- rescue => e
424
+ rescue
425
425
  puts "Failed to check if level #{level} enabled for #{context} with appender #{appender}\n"
426
426
  false
427
427
  end
@@ -81,6 +81,8 @@ module Hatchet
81
81
  # block is preferred as it is assumed to provide more detail.
82
82
  #
83
83
  def initialize(args = {}, error = nil, &block)
84
+ @message = nil
85
+
84
86
  if args.kind_of? Hash
85
87
  # If args is a Hash then using new constructor format or no parameters
86
88
  # specified. Either way, use the new format.
@@ -101,7 +103,13 @@ module Hatchet
101
103
  # Public: Returns the String representation of the message.
102
104
  #
103
105
  def to_s
104
- @evaluated_message ||= (@message || @block.call).to_s
106
+ evaluated_message.to_s
107
+ end
108
+
109
+ # Public: Returns the evaluated message.
110
+ #
111
+ def evaluated_message
112
+ @evaluated_message ||= (@message || @block.call)
105
113
  end
106
114
 
107
115
  end
@@ -152,6 +152,11 @@ module Hatchet
152
152
  end
153
153
  end
154
154
 
155
+ # Internal: Returns a copy of the stack as an array.
156
+ #
157
+ def to_a
158
+ @stack.clone.to_a
159
+ end
155
160
  end
156
161
 
157
162
  end
@@ -0,0 +1,104 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'json'
4
+
5
+ module Hatchet
6
+
7
+ # Public: Structured formatter class. Outputs messages as JSON strings.
8
+ #
9
+ class StructuredFormatter
10
+ include BacktraceFormatter
11
+
12
+ # Public: Creates a new instance.
13
+ #
14
+ def initialize
15
+ @backtrace = true
16
+ @secs = 0
17
+ @millis = -1
18
+ @level_cache = {}
19
+ end
20
+
21
+ # Public: Returns the formatted message.
22
+ #
23
+ # level - The severity of the log message.
24
+ # context - The context of the log message.
25
+ # message - The message provided by the log caller.
26
+ #
27
+ # Returns messages in the format:
28
+ #
29
+ # %Y-%m-%d %H:%M:%S.%L [THREAD] LEVEL CONTEXT - MESSAGE
30
+ # BACKTRACE
31
+ #
32
+ # The backtrace is only present if the message contains an error.
33
+ #
34
+ def format(level, context, message)
35
+ msg = message.evaluated_message
36
+
37
+ case msg
38
+ when Hash
39
+ # Assume caller is following conventions
40
+ log = msg.dup
41
+ else
42
+ # Otherwise treat as String
43
+ log = { :message => msg.to_s.strip }
44
+ end
45
+
46
+ log[:timestamp] = timestamp
47
+ log[:level] = format_level(level)
48
+ log[:pid] = Process.pid
49
+
50
+ unless Thread.current == Thread.main
51
+ log[:thread] = Thread.current.object_id
52
+ end
53
+
54
+ log[:context] = context
55
+
56
+ if message.ndc.any?
57
+ log[:ndc] = message.ndc.to_a
58
+ end
59
+
60
+ if message.error
61
+ error = message.error
62
+
63
+ log[:error_class] = error.class.to_s
64
+ log[:error_message] = error.message
65
+ log[:error_backtrace]
66
+
67
+ if error.respond_to?(:backtrace)
68
+ backtrace = error.backtrace
69
+ backtrace = backtrace.take(backtrace_limit) if backtrace_limit
70
+ log[:error_backtrace] = backtrace
71
+ end
72
+ end
73
+
74
+ JSON.generate(log.to_h)
75
+ end
76
+
77
+ private
78
+
79
+ # Private: Returns the current time as a String.
80
+ #
81
+ def timestamp
82
+ time = Time.now.getutc
83
+
84
+ secs = time.to_i
85
+ millis = time.nsec/1000000
86
+
87
+ return @last if @millis == millis && @secs == secs
88
+
89
+ unless secs == @secs
90
+ @secs = secs
91
+ @date = time.strftime('%Y-%m-%d %H:%M:%S.')
92
+ end
93
+
94
+ @millis = millis
95
+ @last = @date + "00#{millis}"[-3..-1]
96
+ end
97
+
98
+ # Private: Returns the level formatted for log output as a String.
99
+ #
100
+ def format_level(level)
101
+ @level_cache[level] ||= level.to_s.upcase
102
+ end
103
+ end
104
+ end
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.2.11'
7
+ VERSION = '0.2.12'
8
8
 
9
9
  end
@@ -0,0 +1,79 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ describe StructuredFormatter do
6
+ let(:subject) { StructuredFormatter.new }
7
+
8
+ describe 'when formatting a message' do
9
+ let(:log_message) { 'Hello, World' }
10
+
11
+ before do
12
+ ndc = NestedDiagnosticContext::ContextStack.new([:foo, 12])
13
+ @message = Message.new(ndc: ndc, message: log_message)
14
+ @context = 'Custom::Context'
15
+ @level = :info
16
+ @formatted_message = subject.format(@level, @context, @message)
17
+ end
18
+
19
+ it "encodes the message as JSON" do
20
+ expected = {
21
+ "timestamp" => Time.now.getutc.strftime(TIME_FORMAT),
22
+ "level" => "INFO",
23
+ "pid" => Process.pid,
24
+ "context" => @context,
25
+ "ndc" => ["foo", 12],
26
+ "message" => @message.to_s.strip,
27
+ }
28
+
29
+ assert_equal JSON.parse(@formatted_message), expected
30
+ end
31
+
32
+ describe 'with an error' do
33
+ before do
34
+ error = OpenStruct.new(message: 'Boom!', backtrace: ['foo.rb:1:a', 'foo.rb:20:b'])
35
+ @message = Message.new(ndc: [], message: ' Hello, World ', error: error)
36
+ end
37
+
38
+ it 'encodes the error' do
39
+ expected = {
40
+ "timestamp" => Time.now.getutc.strftime(TIME_FORMAT),
41
+ "level" => "INFO",
42
+ "pid" => Process.pid,
43
+ "context" => @context,
44
+ "message" => @message.to_s.strip,
45
+ "error_class" => "OpenStruct",
46
+ "error_message" => "Boom!",
47
+ "error_backtrace" => @message.error.backtrace,
48
+ }
49
+
50
+ formatted_message = subject.format(@level, @context, @message)
51
+
52
+ assert_equal JSON.parse(formatted_message), expected
53
+ end
54
+ end
55
+
56
+ describe 'with a structured message' do
57
+ let(:log_message) do
58
+ {
59
+ :message => "Hi, there",
60
+ :other => 123,
61
+ }
62
+ end
63
+
64
+ it "encodes the message as JSON" do
65
+ expected = {
66
+ "timestamp" => Time.now.getutc.strftime(TIME_FORMAT),
67
+ "level" => "INFO",
68
+ "pid" => Process.pid,
69
+ "context" => @context,
70
+ "ndc" => ["foo", 12],
71
+ "message" => log_message[:message],
72
+ "other" => log_message[:other],
73
+ }
74
+
75
+ assert_equal JSON.parse(@formatted_message), expected
76
+ end
77
+ end
78
+ end
79
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hatchet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garry Shutler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-11 00:00:00.000000000 Z
11
+ date: 2019-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -49,6 +49,7 @@ files:
49
49
  - lib/hatchet/railtie.rb
50
50
  - lib/hatchet/simple_formatter.rb
51
51
  - lib/hatchet/standard_formatter.rb
52
+ - lib/hatchet/structured_formatter.rb
52
53
  - lib/hatchet/thread_name_formatter.rb
53
54
  - lib/hatchet/version.rb
54
55
  - spec/configuration_spec.rb
@@ -67,6 +68,7 @@ files:
67
68
  - spec/simple_formatter_spec.rb
68
69
  - spec/spec_helper.rb
69
70
  - spec/standard_formatter_spec.rb
71
+ - spec/structured_formatter_spec.rb
70
72
  homepage: http://gshutler.github.com/hatchet/
71
73
  licenses:
72
74
  - MIT
@@ -92,19 +94,20 @@ signing_key:
92
94
  specification_version: 4
93
95
  summary: Logging library that provides the ability to add class/module specific filters
94
96
  test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/standard_formatter_spec.rb
99
+ - spec/logger_spec.rb
100
+ - spec/logger_appender_spec.rb
95
101
  - spec/configuration_spec.rb
102
+ - spec/level_manager_spec.rb
103
+ - spec/simple_formatter_spec.rb
104
+ - spec/middleware_spec.rb
105
+ - spec/message_spec.rb
106
+ - spec/structured_formatter_spec.rb
107
+ - spec/plain_formatter_spec.rb
108
+ - spec/helpers/logger_double.rb
109
+ - spec/helpers/test_formatter.rb
96
110
  - spec/helpers/disabled_appender.rb
97
111
  - spec/helpers/failing_appender.rb
98
- - spec/helpers/logger_double.rb
99
112
  - spec/helpers/storing_appender.rb
100
- - spec/helpers/test_formatter.rb
101
113
  - spec/include_spec.rb
102
- - spec/level_manager_spec.rb
103
- - spec/logger_appender_spec.rb
104
- - spec/logger_spec.rb
105
- - spec/message_spec.rb
106
- - spec/middleware_spec.rb
107
- - spec/plain_formatter_spec.rb
108
- - spec/simple_formatter_spec.rb
109
- - spec/spec_helper.rb
110
- - spec/standard_formatter_spec.rb