logtail 0.1.2 → 0.1.3

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: bd9d406f360e1d9c3f56c4dad3f5fc6cc945504a635eee458826859d7aede911
4
- data.tar.gz: 2927bc187d6bca9477b3b5b42fecfee091619c1ec4d12a7ac2f9f4d3fa050a63
3
+ metadata.gz: 2ba9fd4e5a7de0392375297a8ac475049499afebb82b904ce43615f1b1c5e000
4
+ data.tar.gz: 0073e828790ded81976eefb1d42d48e0c9eff3123dc47c4c861960bf9a611ff2
5
5
  SHA512:
6
- metadata.gz: 398c6f33b16776fdefc451525d51e6ce2d5aa2a4b5eb4f771bc1712428f5b316136001ff3b827e5b6ab8a0161eb84aca2cd311e647e87ee1396f3f47807aab35
7
- data.tar.gz: 447794cf96fde4eae26d23d1e7e85003c331ec4d1777bbc773de1cb676b502f369a65d926745493303302e5ed95032b2b41bd24d8d0d5198af83789490985473
6
+ metadata.gz: 9c79321cad971e6fa844c08525b6b308594572e728f465561d4f9f594ddb4009047491014f4bc7267292e5d3f3c3d0249e5c0a27d5fb6f7107fd1a79cc10c36e
7
+ data.tar.gz: 37614b8402998ebfbf9d40741fcbe8085fa4beaf86cd0dfe2b6a2028e08b25a238e26dbd4efb6b5e2406116db2599e135f4f802135d2fc6bbf14e2d3809fcb96
data/CHANGELOG.md CHANGED
@@ -14,3 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
14
14
  ## [0.1.2] - 2021-04-22
15
15
 
16
16
  - Fixed string encoding.
17
+
18
+ ## [0.1.3] - 2021-06-11
19
+
20
+ - Fixed detection of the frame that calls the logger.
@@ -12,7 +12,7 @@ module Logtail
12
12
  BINARY_LIMIT_THRESHOLD = 1_000.freeze
13
13
  DT_PRECISION = 6.freeze
14
14
  MESSAGE_MAX_BYTES = 8192.freeze
15
- LOGTAIL_GEM_REGEX = /\/logtail(?:-ruby|-rails|-rack)?(?:-\d+(?:\.\d+)*)?\/lib$/.freeze
15
+ LOGGER_FILE = '/logtail/logger.rb'.freeze
16
16
 
17
17
  attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time
18
18
 
@@ -39,6 +39,7 @@ module Logtail
39
39
  @tags = options[:tags]
40
40
  @context_snapshot = context_snapshot
41
41
  @event = event
42
+ @runtime_context = current_runtime_context || {}
42
43
  end
43
44
 
44
45
  # Builds a hash representation containing simple objects, suitable for serialization (JSON).
@@ -64,7 +65,7 @@ module Logtail
64
65
 
65
66
  hash[:context] ||= {}
66
67
  hash[:context][:runtime] ||= {}
67
- hash[:context][:runtime].merge!(current_runtime_context || {})
68
+ hash[:context][:runtime].merge!(@runtime_context)
68
69
 
69
70
  if options[:only]
70
71
  hash.select do |key, _value|
@@ -114,32 +115,40 @@ module Logtail
114
115
  end
115
116
 
116
117
  def current_runtime_context
117
- index = caller_locations.rindex { |x| logtail_frame?(x) }
118
- frame = caller_locations[index + 1] unless index.nil?
119
- return convert_to_runtime_context(frame) unless frame.nil?
118
+ last_logger_invocation_index = caller_locations.rindex { |frame| logtail_logger_frame?(frame) }
119
+ return {} if last_logger_invocation_index.nil?
120
+
121
+ calling_frame_index = last_logger_invocation_index + 1
122
+ frame = caller_locations[calling_frame_index]
123
+
124
+ return convert_to_runtime_context(frame)
120
125
  end
121
126
 
122
127
  def convert_to_runtime_context(frame)
123
128
  {
124
- file: relative_to_main_module(frame.absolute_path),
129
+ file: path_relative_to_app_root(frame),
125
130
  line: frame.lineno,
126
131
  frame_label: frame.label,
127
132
  }
128
133
  end
129
134
 
130
- def logtail_frame?(frame)
131
- return false if frame.absolute_path.nil? || logtail_gem_paths.empty?
132
- logtail_gem_paths.any? { |path| frame.absolute_path.start_with?(path) }
135
+ def logtail_logger_frame?(frame)
136
+ !frame.absolute_path.nil? && frame.absolute_path.end_with?(LOGGER_FILE)
133
137
  end
134
138
 
135
- def logtail_gem_paths
136
- @logtail_gem_paths ||= $LOAD_PATH.select { |path| path.match(LOGTAIL_GEM_REGEX) }
139
+ def path_relative_to_app_root(frame)
140
+ Pathname.new(frame.absolute_path).relative_path_from(root_path).to_s
137
141
  end
138
142
 
139
- def relative_to_main_module(path)
140
- base_file = caller_locations.last.absolute_path
141
- base_path = Pathname.new(File.dirname(base_file || '/'))
142
- Pathname.new(path).relative_path_from(base_path).to_s
143
+ def root_path
144
+ if Object.const_defined?('Rails')
145
+ Rails.root.to_s
146
+ elsif Object.const_defined?('Rack::Directory')
147
+ Rack::Directory.new('').root
148
+ else
149
+ base_file = caller_locations.last.absolute_path
150
+ Pathname.new(File.dirname(base_file || '/'))
151
+ end
143
152
  end
144
153
  end
145
154
  end
@@ -1,3 +1,3 @@
1
1
  module Logtail
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Logtail::LogEntry do
4
- let(:time) { Time.utc(2016, 9, 1, 12, 0, 0) }
4
+ let(:time) { Time.utc(2021, 06, 11, 12, 0, 0) }
5
5
 
6
6
  describe "#to_msgpack" do
7
7
  it "should encode properly with an event and context" do
@@ -16,20 +16,19 @@ describe Logtail::LogEntry do
16
16
  context = {custom: {a: "b"}}
17
17
  log_entry = described_class.new("INFO", time, nil, "log message", context, event)
18
18
  msgpack = log_entry.to_msgpack
19
- expect(msgpack).to start_with("\x85\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z".force_encoding("ASCII-8BIT"))
19
+ expect(msgpack).to start_with("\x85\xA5level\xA4INFO\xA2dt\xBB2021-06-11T12:00:00.000000Z".force_encoding("ASCII-8BIT"))
20
20
  end
21
21
  end
22
22
 
23
23
  describe "#to_hash" do
24
24
  it "should include runtime context information" do
25
- $:.unshift(File.expand_path(__dir__ + '/../../lib'))
25
+ log_entry = Logtail::Logger::PassThroughFormatter.new.call("INFO", time, "", "log message")
26
26
 
27
- log_entry = described_class.new("INFO", time, nil, "log message", {}, {})
28
27
  hash = log_entry.to_hash
29
28
  expect(hash[:context]).to_not be_nil
30
29
  expect(hash[:context][:runtime]).to_not be_nil
31
- expect(hash[:context][:runtime][:file]).to_not be_nil
32
- expect(hash[:context][:runtime][:line]).to_not be_nil
30
+ expect(hash[:context][:runtime][:file]).to end_with('/spec/logtail/log_entry_spec.rb')
31
+ expect(hash[:context][:runtime][:line]).to be(25)
33
32
  expect(hash[:context][:runtime][:frame_label]).to_not be_nil
34
33
  end
35
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logtail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Logtail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-22 00:00:00.000000000 Z
11
+ date: 2021-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack