cm_logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 761ebd63f3ecf80b8db7fbd5204718aac9982f9a43eb0866f77bba371af2ea8f
4
+ data.tar.gz: '091b2e075ec1eca95507c5bd6875787e39785e14c86294210b39e30cc719f1c0'
5
+ SHA512:
6
+ metadata.gz: 35951ffbc63e2f1a1fec2f67c429a99c874fe481056515b0f1d91b3a4a3b70850568c2d6eb3c011565c3b982f9b1f4e9a29b1b5d49ca05bd18ce5914b8ede53a
7
+ data.tar.gz: b5caf453fe5c9e44ac3e5516aa0acdf242291f88f77e94d56909528084e62adf30aabcb064a3efc050fbeebc9f4e5a3451517b3513d27760906519f612e33095
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'pry'
5
+ end
6
+
7
+ gemspec
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cm_logger (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (1.0.0)
12
+ pry (0.13.0)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rspec (3.9.0)
16
+ rspec-core (~> 3.9.0)
17
+ rspec-expectations (~> 3.9.0)
18
+ rspec-mocks (~> 3.9.0)
19
+ rspec-core (3.9.1)
20
+ rspec-support (~> 3.9.1)
21
+ rspec-expectations (3.9.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-mocks (3.9.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-support (3.9.2)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ cm_logger!
34
+ pry
35
+ rspec (~> 3.9.0)
36
+
37
+ BUNDLED WITH
38
+ 2.0.2
@@ -0,0 +1,2 @@
1
+ # cm_logger
2
+ > Log related stuff for Rails
@@ -0,0 +1,18 @@
1
+ lib_dir = File.join(File.dirname(__FILE__),'lib')
2
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'cm_logger'
6
+ s.version = '0.0.1'
7
+ s.date = '2020-04-09'
8
+ s.summary = "Log related stuff for Rails"
9
+ s.description = "Log related stuff for Rails"
10
+ s.authors = ["ben"]
11
+ s.email = 'ben@arc.dev'
12
+ s.files = `git ls-files`.split($/)
13
+ s.homepage = 'https://github.com/CodementorIO/cm_logger'
14
+ s.license = 'MIT'
15
+ s.required_ruby_version = '>= 2.6'
16
+
17
+ s.add_development_dependency("rspec", ["~> 3.9.0"])
18
+ end
@@ -0,0 +1,5 @@
1
+ require 'lograge_formatter'
2
+ require 'sidekiq_formatter'
3
+
4
+ module CmLogger
5
+ end
@@ -0,0 +1,30 @@
1
+ module CmLogger
2
+ class LogrageFormatter
3
+ def call(data)
4
+ fields_to_display(data)
5
+ .map { |key| format(key, data[key]) }
6
+ .join(' ')
7
+ end
8
+
9
+ protected
10
+
11
+ def fields_to_display(data)
12
+ data.keys
13
+ end
14
+
15
+ def format(key, value)
16
+ "#{key}=#{parse_value(key, value)}"
17
+ end
18
+
19
+ def parse_value(key, value)
20
+ if value.is_a? Float
21
+ Kernel.format('%.2f', value)
22
+ elsif value.is_a? String
23
+ safe_value = value.gsub("\n", "\t")
24
+ "'#{safe_value}'"
25
+ else
26
+ value
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ require 'logger'
2
+ require 'time'
3
+
4
+ module CmLogger
5
+ class SidekiqFormatter < Logger::Formatter
6
+ def call(severity, time, program_name, message)
7
+ clean_message = message.gsub('"', '\'').gsub("\n", ' ').slice(0, 100)
8
+
9
+ log_hash = {
10
+ time: time.utc.iso8601(3),
11
+ pid: ::Process.pid,
12
+ tid: Thread.current.object_id.to_s(36),
13
+ severity: severity,
14
+ message: "\"#{clean_message}\""
15
+ }.merge(context)
16
+
17
+ log_hash.keys.map do |key|
18
+ "#{key}=#{log_hash[key]}"
19
+ end.join(" ") + "\n"
20
+ end
21
+
22
+ def context
23
+ c = Thread.current[:sidekiq_context]
24
+ if c && c.any?
25
+ tokens = c.map(&:split).flatten
26
+ worker_name = tokens.find { |token| token.end_with?('Worker') }
27
+ jid = tokens.find { |token| token.start_with?('JID') }
28
+
29
+ {
30
+ worker_name: worker_name,
31
+ jid: jid
32
+ }.compact
33
+ else
34
+ {}
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CmLogger::LogrageFormatter' do
4
+ describe '#call' do
5
+ let(:formatter) do
6
+ CmLogger::LogrageFormatter.new
7
+ end
8
+
9
+ it "should foramt hash" do
10
+ data = { foo: "bar", baz: 123 }
11
+
12
+ result = formatter.call(data)
13
+
14
+ expect(result).to eq("foo='bar' baz=123")
15
+ end
16
+
17
+ it "should replace newline with \t" do
18
+ data = { foo: "bar\n123", baz: 123 }
19
+
20
+ result = formatter.call(data)
21
+
22
+ expect(result).to eq("foo='bar\t123' baz=123")
23
+ end
24
+
25
+ it "should adjust float format" do
26
+ data = { foo: "bar", baz: 123.45678 }
27
+
28
+ result = formatter.call(data)
29
+
30
+ expect(result).to eq("foo='bar' baz=123.46")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'CmLogger::SidekiqFormatter' do
4
+ describe '#call(severity, time, program_name, message)' do
5
+ let(:formatter) do
6
+ CmLogger::SidekiqFormatter.new
7
+ end
8
+ let(:severity) { "INFO" }
9
+ let(:message) { "xxx" }
10
+ let(:time) { Time.now }
11
+ let(:program_name) { "program_name" }
12
+
13
+ it "should return log in logfmt style" do
14
+ args = [severity, time, program_name, message]
15
+
16
+ result = formatter.call(*args)
17
+
18
+ tid = Thread.current.object_id.to_s(36)
19
+ pid = ::Process.pid
20
+ expect(result).to eq("time=#{time.utc.iso8601(3)} pid=#{pid} tid=#{tid} severity=#{severity} message=\"#{message}\"\n")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'cm_logger'
6
+ require 'pry'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cm_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ben
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.9.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.9.0
27
+ description: Log related stuff for Rails
28
+ email: ben@arc.dev
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - README.md
36
+ - cm_logger.gemspec
37
+ - lib/cm_logger.rb
38
+ - lib/lograge_formatter.rb
39
+ - lib/sidekiq_formatter.rb
40
+ - spec/lograge_formatter_spec.rb
41
+ - spec/sidekiq_formatter_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: https://github.com/CodementorIO/cm_logger
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '2.6'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.0.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Log related stuff for Rails
66
+ test_files: []