better_log_formatter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3f44ee8af1a96ddddea0cc3d888845901cef7a7
4
+ data.tar.gz: f1ffe485b3bf5db84d3d24097cad7a9b78d97e3c
5
+ SHA512:
6
+ metadata.gz: b85a989e39b8d2348988e2da43a81881c128a801144af8177739fee376d44a169ef016df6aeee67e518748d6e4c5674a1d2d64009879d848e1328c4b10def878
7
+ data.tar.gz: 879733a3ed579c02f1a50f8ca233e46eb78054074d905de1def9d099da5d307b5ad8ead9bc5b20c437ac90e12a244f98d032e22b750e7c8eb6b0cfc3156733c6
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in better_log_formatter.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sébastien Puyet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # Better Log Formatter
2
+ A formatter allowing you to add dynamic tags to your logs with sidekiq tags support
3
+
4
+ ## Usage
5
+
6
+ ```ruby
7
+ logger = Logger.new(STDOUT)
8
+ formatter = BetterLogFormatter.new("my_app")
9
+ formatter.custom_tag(:foo) { 'Bar' }
10
+ formatter.custom_tag(:request_id) { Request.id }
11
+ logger.formatter = formatter
12
+ logger.info "Hello world!" # 2018-09-16T12:42:27.843291 my_app[1337][Bar][a776fea1-e24c-4976-b8c4-df790b1aefcb] -- INFO: Hello world!
13
+ ```
14
+
15
+ Also, timestamp can be hidden or removed:
16
+ ```ruby
17
+ formatter.datetime_format = "%m/%d/%y - %H:%M:%S "
18
+ logger.info "Hello world!" # 09/16/2018T12:42:27 my_app[1337][Bar][a776fea1-e24c-4976-b8c4-df790b1aefcb] -- INFO: Hello world!
19
+ formatter.datetime_display = false
20
+ logger.info "Hello world!" # my_app[1337][Bar][a776fea1-e24c-4976-b8c4-df790b1aefcb] -- INFO: Hello world!
21
+ ```
22
+
23
+
24
+ ## Contributing
25
+
26
+ Bug reports and pull requests are welcome on GitHub at https://github.com/spuyet/better_log_formatter.
27
+
28
+ ## License
29
+
30
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "better_log_formatter"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "better_log_formatter"
7
+ spec.version = BetterLogFormatter::VERSION
8
+ spec.authors = ["Sébastien Puyet"]
9
+ spec.email = ["sebastien@puyet.fr"]
10
+
11
+ spec.summary = %q{A better log formatter}
12
+ spec.homepage = "https://git.dimelo.com/shared/better_log_formatter"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "timecop", "~> 0.9"
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "better_log_formatter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,51 @@
1
+ require 'logger'
2
+
3
+ class BetterLogFormatter < ::Logger::Formatter
4
+ VERSION = "0.1.0".freeze
5
+ Format = "%s%s[%d]%s%s -- %s: %s\n".freeze
6
+
7
+ attr_accessor :app_name
8
+ attr_accessor :datetime_display
9
+
10
+ def initialize(app_name)
11
+ @app_name = app_name
12
+ @datetime = datetime_display
13
+ super()
14
+ end
15
+
16
+ def call(severity, datetime, _, message)
17
+ Format % [formatted_datetime(datetime), app_name, $$, custom_tags, sidekiq_tags, severity_label(severity), msg2str(message)]
18
+ end
19
+
20
+ def custom_tag(tag_name, &block)
21
+ @custom_tags ||= {}
22
+ @custom_tags[tag_name] = block
23
+ true
24
+ end
25
+
26
+ private
27
+
28
+ def custom_tags
29
+ @custom_tags ||= {}
30
+ @custom_tags.map { |_, block| block.call }.compact.map{ |tag| "[#{tag}]" }.join
31
+ end
32
+
33
+ def thread_tag
34
+ "TID-#{Thread.current.object_id.to_s(36)}"
35
+ end
36
+
37
+ def sidekiq_tags
38
+ return if Thread.current[:sidekiq_context].nil?
39
+
40
+ tags = Thread.current[:sidekiq_context].flat_map(&:split) || []
41
+ "[#{thread_tag}]#{tags.map { |tag| "[#{tag}]" }.join}"
42
+ end
43
+
44
+ def formatted_datetime(datetime)
45
+ format_datetime(datetime) if datetime_display
46
+ end
47
+
48
+ def severity_label(severity)
49
+ severity.is_a?(Fixnum) ? ::Logger::SEV_LABEL[severity] : severity
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_log_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sébastien Puyet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description:
70
+ email:
71
+ - sebastien@puyet.fr
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - better_log_formatter.gemspec
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/better_log_formatter.rb
86
+ homepage: https://git.dimelo.com/shared/better_log_formatter
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A better log formatter
110
+ test_files: []