vx-lib-logger 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +8 -0
- data/lib/vx/lib/logger.rb +6 -3
- data/lib/vx/lib/logger/instance.rb +7 -6
- data/lib/vx/lib/logger/json_formatter.rb +4 -39
- data/lib/vx/lib/logger/{handle_exceptions.rb → rack/handle_exceptions.rb} +0 -0
- data/lib/vx/lib/logger/raw_formatter.rb +10 -0
- data/lib/vx/lib/logger/version.rb +1 -1
- data/spec/lib/instance_spec.rb +80 -0
- data/spec/lib/logger_spec.rb +14 -0
- data/spec/lib/rack_handle_exceptions_spec.rb +19 -0
- data/spec/spec_helper.rb +4 -0
- metadata +13 -5
- data/lib/vx/lib/logger/instrumentations.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f98e748e5656d369a8c12d57f0ad8c358060c06
|
4
|
+
data.tar.gz: 83b4d6156cb3ec83c2a2f8dd23fcc61f6eb51933
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fad51d812538094eeafa610b05046d6536ce76fcc0da99864237cfdcf4d5a3cdb1ca850215239e0ed947a76b63136fe1ea4d670f262669e4d6f40470ae28aaf6
|
7
|
+
data.tar.gz: 4954cf928b9d44e2fa3035ba4a1b38c3f7f03fa8964e8c08889bedff7071e2dd33d7e9492de60062467516260d702981dfa9365ccd9ccd615d1732ca6665158b
|
data/Rakefile
CHANGED
data/lib/vx/lib/logger.rb
CHANGED
@@ -6,12 +6,15 @@ module Vx ; module Lib
|
|
6
6
|
|
7
7
|
autoload :Instance, File.expand_path("../logger/instance", __FILE__)
|
8
8
|
autoload :JsonFormatter, File.expand_path("../logger/json_formatter", __FILE__)
|
9
|
-
autoload :
|
10
|
-
|
9
|
+
autoload :RawFormatter, File.expand_path("../logger/raw_formatter", __FILE__)
|
10
|
+
|
11
|
+
module Rack
|
12
|
+
autoload :HandleExceptions, File.expand_path("../logger/rack/handle_exceptions", __FILE__)
|
13
|
+
end
|
11
14
|
|
12
15
|
@@default = Instance.new(STDOUT)
|
13
16
|
|
14
|
-
def self.get(io, options)
|
17
|
+
def self.get(io = nil, options = {})
|
15
18
|
Instance.new(io, options)
|
16
19
|
end
|
17
20
|
|
@@ -10,10 +10,10 @@ module Vx ; module Lib ; module Logger
|
|
10
10
|
def initialize(io, params = {})
|
11
11
|
@params = params
|
12
12
|
@logger = ::Logger.new(io, 7, 50_000_000)
|
13
|
-
@logger.formatter =
|
13
|
+
@logger.formatter = RawFormatter.new
|
14
14
|
end
|
15
15
|
|
16
|
-
[:fatal, :warn, :debug, :error, :info
|
16
|
+
[:fatal, :warn, :debug, :error, :info].each do |m|
|
17
17
|
define_method m do |*args|
|
18
18
|
process_message(m, *args)
|
19
19
|
end
|
@@ -81,11 +81,8 @@ module Vx ; module Lib ; module Logger
|
|
81
81
|
end
|
82
82
|
|
83
83
|
body = {
|
84
|
-
message: message.to_s,
|
85
84
|
thread_id: ::Thread.current.object_id,
|
86
85
|
process_id: ::Process.pid,
|
87
|
-
progname: (params[:progname] || :ruby),
|
88
|
-
level: level,
|
89
86
|
}
|
90
87
|
|
91
88
|
if options && options != {}
|
@@ -94,7 +91,11 @@ module Vx ; module Lib ; module Logger
|
|
94
91
|
)
|
95
92
|
end
|
96
93
|
|
97
|
-
@logger.public_send level, body
|
94
|
+
@logger.public_send level, format_message(message, body)
|
95
|
+
end
|
96
|
+
|
97
|
+
def format_message(message, payload)
|
98
|
+
JsonFormatter.call(message, payload)
|
98
99
|
end
|
99
100
|
|
100
101
|
end
|
@@ -2,48 +2,13 @@ require 'oj'
|
|
2
2
|
|
3
3
|
module Vx ; module Lib ; module Logger
|
4
4
|
|
5
|
-
JsonFormatter
|
5
|
+
module JsonFormatter
|
6
6
|
|
7
|
-
def call(
|
8
|
-
::Oj.dump(
|
7
|
+
def self.call(message, payload)
|
8
|
+
payload = ::Oj.dump(payload, mode: :compat)
|
9
|
+
"#{message} :--: #{payload}"
|
9
10
|
end
|
10
11
|
|
11
|
-
=begin
|
12
|
-
def safe_value(value, options = {})
|
13
|
-
new_value = case value.class.to_s
|
14
|
-
when "String", "Fixnum", "Float"
|
15
|
-
value
|
16
|
-
when "Symbol", "BigDecimal"
|
17
|
-
value.to_s
|
18
|
-
when "Array"
|
19
|
-
value = value.map(&:to_s)
|
20
|
-
options[:join_arrays] ? value.join("\n") : value
|
21
|
-
when 'NilClass'
|
22
|
-
nil
|
23
|
-
else
|
24
|
-
value.inspect
|
25
|
-
end
|
26
|
-
if new_value.is_a?(String)
|
27
|
-
new_value.encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'})
|
28
|
-
else
|
29
|
-
new_value
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def make_safe_hash(msg, options = {})
|
34
|
-
msg.inject({}) do |acc, pair|
|
35
|
-
msg_key, msg_value = pair
|
36
|
-
|
37
|
-
if msg_key == :fields
|
38
|
-
acc[msg_key] = make_safe_hash(msg_value, join_arrays: true)
|
39
|
-
else
|
40
|
-
acc[msg_key] = safe_value(msg_value, options)
|
41
|
-
end
|
42
|
-
acc
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
=end
|
47
12
|
end
|
48
13
|
|
49
14
|
end ; end ; end
|
File without changes
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
describe Vx::Lib::Logger::Instance do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@out = StringIO.new
|
9
|
+
@log = Vx::Lib::Logger.get(@out)
|
10
|
+
assert @log
|
11
|
+
end
|
12
|
+
|
13
|
+
[:fatal, :warn, :debug, :error, :info].each do |m|
|
14
|
+
it "should write #{m} message" do
|
15
|
+
@log.public_send(m, "send #{m}")
|
16
|
+
text = "[#{m.upcase}] send #{m} :--: {\"thread_id\":#{tid},\"process_id\":#{pid}}\n"
|
17
|
+
assert_equal get_out, text
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should write message with params" do
|
22
|
+
@log.info "text message", param: :value
|
23
|
+
text = "[INFO] text message :--: {\"thread_id\":#{tid},\"process_id\":#{pid},\"fields\":{\"param\":\"value\"}}\n"
|
24
|
+
assert_equal get_out, text
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should write message with object in params" do
|
28
|
+
@log.info "text message", param: self
|
29
|
+
assert get_out
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should write message with exception in params" do
|
33
|
+
@log.info "text message", exception: Exception.new("got!")
|
34
|
+
text = "[INFO] text message :--: {\"thread_id\":#{tid},\"process_id\":#{pid},\"fields\":{\"exception\":[\"Exception\",\"got!\"],\"backtrace\":\"\"}}\n"
|
35
|
+
assert_equal get_out, text
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should handle block" do
|
39
|
+
@log.handle "text message" do
|
40
|
+
sleep 0.1
|
41
|
+
end
|
42
|
+
assert_match(/duration/, get_out)
|
43
|
+
|
44
|
+
begin
|
45
|
+
@log.handle "text message", key: :value do
|
46
|
+
raise 'got!'
|
47
|
+
end
|
48
|
+
rescue Exception
|
49
|
+
end
|
50
|
+
|
51
|
+
body = get_out
|
52
|
+
assert_match(/duration/, body)
|
53
|
+
assert_match(/key/, body)
|
54
|
+
assert_match(/value/, body)
|
55
|
+
assert_match(/got\!/, body)
|
56
|
+
assert_match(/backtrace/, body)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should dump invalid unicode key" do
|
60
|
+
@log.info "Le Caf\xc3\xa9 \xa9", key: "Le Caf\xc3\xa9 \xa9"
|
61
|
+
text = "[INFO] Le Café \xA9 :--: {\"thread_id\":#{tid},\"process_id\":#{pid},\"fields\":{\"key\":\"Le Café \xA9\"}}\n"
|
62
|
+
assert_equal get_out, text
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_out
|
66
|
+
@out.rewind
|
67
|
+
body = @out.read
|
68
|
+
@out.rewind
|
69
|
+
body
|
70
|
+
end
|
71
|
+
|
72
|
+
def tid
|
73
|
+
Thread.current.object_id
|
74
|
+
end
|
75
|
+
|
76
|
+
def pid
|
77
|
+
Process.pid
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vx::Lib::Logger::Rack::HandleExceptions do
|
4
|
+
|
5
|
+
it "should run successfuly" do
|
6
|
+
app = ->(env) { env }
|
7
|
+
handle = Vx::Lib::Logger::Rack::HandleExceptions.new(app)
|
8
|
+
re = handle.call('env')
|
9
|
+
assert_equal re, 'env'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should run with exception" do
|
13
|
+
app = ->(env) { raise 'got!' }
|
14
|
+
handle = Vx::Lib::Logger::Rack::HandleExceptions.new(app)
|
15
|
+
re = handle.call({})
|
16
|
+
assert_equal re, 'env'
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vx-lib-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Galinsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -65,11 +65,15 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- lib/vx/lib/logger.rb
|
68
|
-
- lib/vx/lib/logger/handle_exceptions.rb
|
69
68
|
- lib/vx/lib/logger/instance.rb
|
70
|
-
- lib/vx/lib/logger/instrumentations.rb
|
71
69
|
- lib/vx/lib/logger/json_formatter.rb
|
70
|
+
- lib/vx/lib/logger/rack/handle_exceptions.rb
|
71
|
+
- lib/vx/lib/logger/raw_formatter.rb
|
72
72
|
- lib/vx/lib/logger/version.rb
|
73
|
+
- spec/lib/instance_spec.rb
|
74
|
+
- spec/lib/logger_spec.rb
|
75
|
+
- spec/lib/rack_handle_exceptions_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
73
77
|
- vx-lib-logger.gemspec
|
74
78
|
homepage: ''
|
75
79
|
licenses:
|
@@ -95,4 +99,8 @@ rubygems_version: 2.2.2
|
|
95
99
|
signing_key:
|
96
100
|
specification_version: 4
|
97
101
|
summary: summary
|
98
|
-
test_files:
|
102
|
+
test_files:
|
103
|
+
- spec/lib/instance_spec.rb
|
104
|
+
- spec/lib/logger_spec.rb
|
105
|
+
- spec/lib/rack_handle_exceptions_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
|
3
|
-
module Vx ; module Lib ; module Logger
|
4
|
-
class Instrumentations
|
5
|
-
|
6
|
-
class << self
|
7
|
-
|
8
|
-
def activate
|
9
|
-
ActiveSupport::Notifications.subscribe(/.*/) do |event, started, finished, _, payload|
|
10
|
-
case event
|
11
|
-
when /\.action_controller$/
|
12
|
-
process_action_controller(event, started, finished, payload)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def process_action_controller(event, started, finished, payload)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end ; end ; end
|