semantic_logger_journald 0.2.0

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: 260b0fd7d7c655738989c1600094585550ce4d35a077ca94253f9e957dcaf317
4
+ data.tar.gz: 18dfcb5cf593531d3de481ac66f73471bc9feb041727235ec70f9c19edfe2cca
5
+ SHA512:
6
+ metadata.gz: 023b495d21b0dbd9a876b4b3c1fe48f9f4a8ba78f2eadd87298186e49d173a82463db26de7049e2df92e9a84540c3edba359fbbf7069963e9299e6ef322fe36f
7
+ data.tar.gz: dc3b15c1f272f42dcb101227cc4133df9c1068020cab2d9bca1cdc8914a2bc714830a7d258cb7dd8d3394111d4f85fe953ba3a6c5a03b1b13b5fc6efa7c6ccdf
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/bundle
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in semantic_logger_journald.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+ gem "rerun"
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ semantic_logger_journald (0.2.0)
5
+ journald-logger (~> 3.0)
6
+ semantic_logger (~> 4.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ concurrent-ruby (1.1.6)
12
+ ffi (1.13.1)
13
+ journald-logger (3.0.1)
14
+ journald-native (~> 1.0)
15
+ journald-native (1.0.12)
16
+ listen (3.2.1)
17
+ rb-fsevent (~> 0.10, >= 0.10.3)
18
+ rb-inotify (~> 0.9, >= 0.9.10)
19
+ minitest (5.14.1)
20
+ rake (12.3.3)
21
+ rb-fsevent (0.10.4)
22
+ rb-inotify (0.10.1)
23
+ ffi (~> 1.0)
24
+ rerun (0.13.0)
25
+ listen (~> 3.0)
26
+ semantic_logger (4.7.1)
27
+ concurrent-ruby (~> 1.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ minitest (~> 5.0)
34
+ rake (~> 12.0)
35
+ rerun
36
+ semantic_logger_journald!
37
+
38
+ BUNDLED WITH
39
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 scarfacedeb
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,80 @@
1
+ # SemanticLoggerJournald
2
+
3
+ Plugin for semantic_logger that adds appender to send logs into journald.
4
+
5
+ It uses [journald-logger](https://github.com/theforeman/journald-logger) under the hood.
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem "semantic_logger_journald", "~> 0.1"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ To add journald appender to semantic_logger:
16
+
17
+ ```ruby
18
+ require "semantic_logger_journald"
19
+
20
+ journald_appender = SemanticLoggerJournald::Appender.new(syslog_id: "my_service")
21
+ SemanticLogger.add_appender(appender: journald_appender)
22
+
23
+ logger = SemanticLogger[MyClass]
24
+ logger.info "Signed In", user_id: 1
25
+ ```
26
+
27
+ You can also directly pass a journald-logger instance:
28
+
29
+ ```ruby
30
+ jlogger = Journald::Logger.new("my_service")
31
+ journald_appender = SemanticLoggerJournald::Appender.new(logger: jlogger)
32
+ SemanticLogger.add_appender(appender: journald_appender)
33
+ ```
34
+
35
+ ## Formatter
36
+
37
+ By default, `SemanticLoggerJournald::Formatter` flattens the payload keys to convert them into individual fields:
38
+
39
+ ```ruby
40
+ logger.info "Paid", order: { id: 1, status: "new" }
41
+ ```
42
+
43
+ It'll produce the following journald fields:
44
+
45
+ ```
46
+ MESSAGE=Paid
47
+ ORDER__ID=1
48
+ ORDER__STATUS=new
49
+ ```
50
+
51
+ If you'd rather keep the payload intact, pass a custom formatter:
52
+
53
+ ```ruby
54
+ formatter = SemanticLoggerJournald::Formatter.new(flatten_payload: false)
55
+
56
+ journald_appender = SemanticLoggerJournald::Appender.new(
57
+ syslog_id: "my_service",
58
+ formatter: formatter
59
+ )
60
+
61
+ SemanticLogger.add_appender(appender: journald_appender)
62
+
63
+ logger.info "Paid", order: { id: 1, status: "new" }
64
+ ```
65
+
66
+ It'll produce the following journald fields:
67
+
68
+ ```
69
+ MESSAGE=Paid
70
+ ORDER={ id: 1, status: "new" }
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/scarfacedeb/semantic_logger_journald.
76
+
77
+
78
+ ## License
79
+
80
+ 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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "semantic_logger_journald"
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,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rerun' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rerun", "rerun")
@@ -0,0 +1,5 @@
1
+ require "semantic_logger_journald/version"
2
+ require "semantic_logger_journald/appender"
3
+
4
+ module SemanticLoggerJournald
5
+ end
@@ -0,0 +1,24 @@
1
+ require "semantic_logger"
2
+ require "journald/logger"
3
+ require "semantic_logger_journald/formatter"
4
+
5
+ module SemanticLoggerJournald
6
+ class Appender < SemanticLogger::Subscriber
7
+ attr_reader :logger
8
+
9
+ def initialize(logger: nil, syslog_id: nil, **args, &block)
10
+ @logger = logger || Journald::Logger.new(syslog_id)
11
+ super(**args, &block)
12
+ end
13
+
14
+ def log(log)
15
+ @logger.send_message(formatter.call(log, self))
16
+ end
17
+
18
+ private
19
+
20
+ def default_formatter
21
+ SemanticLoggerJournald::Formatter.new
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ module SemanticLoggerJournald
2
+ class Formatter < SemanticLogger::Formatters::Raw
3
+ LEVEL_MAP = {
4
+ trace: Journald::LOG_DEBUG,
5
+ debug: Journald::LOG_DEBUG,
6
+ info: Journald::LOG_INFO,
7
+ warn: Journald::LOG_WARNING,
8
+ error: Journald::LOG_ERR,
9
+ fatal: Journald::LOG_CRIT
10
+ }.freeze
11
+
12
+ KEY_JOIN = "__"
13
+
14
+ def initialize(flatten_payload: KEY_JOIN, time_format: nil, **args)
15
+ @flatten_payload = flatten_payload
16
+ super(time_format: time_format, **args)
17
+ end
18
+
19
+ def level
20
+ hash[:priority] = LEVEL_MAP.fetch(log.level)
21
+ end
22
+
23
+ def time
24
+ hash[:syslog_timestamp] = time_format ? format_time(log.time) : format_syslog_time(log.time)
25
+ end
26
+
27
+ # It's already logged by journald as _PID
28
+ def pid
29
+ nil
30
+ end
31
+
32
+ def tags
33
+ hash.merge!(log.tags.map { |t| [t, 1] }.to_h) if log.tags&.any?
34
+ end
35
+
36
+ def named_tags
37
+ hash.merge!(log.named_tags) if log.named_tags&.any?
38
+ end
39
+
40
+ def payload
41
+ return if !log.payload || log.payload.empty?
42
+
43
+ payload = log.payload
44
+ payload = flatten_hash(payload) if flatten_payload?
45
+ hash.merge!(payload)
46
+ end
47
+
48
+ private
49
+
50
+ def flatten_payload?
51
+ log.payload&.any? && @flatten_payload
52
+ end
53
+
54
+ def flatten_hash(nested_hash)
55
+ nested_hash.each_with_object({}) do |(key, val), h|
56
+ if val.is_a?(Hash)
57
+ flatten_hash(val).map { |nested_key, nested_val|
58
+ h[join_keys(key, nested_key)] = nested_val
59
+ }
60
+ else
61
+ h[key] = val
62
+ end
63
+ end
64
+ end
65
+
66
+ def join_keys(*args)
67
+ args.join(@flatten_payload).to_sym
68
+ end
69
+
70
+ # from syslog_protocol gem
71
+ def format_syslog_time(time)
72
+ # The timestamp format requires that a day with fewer than 2 digits have
73
+ # what would normally be a preceding zero, be instead an extra space.
74
+ day = time.strftime("%d")
75
+ day = day.sub(/^0/, " ") if day =~ /^0\d/
76
+ time.strftime("%b #{day} %H:%M:%S")
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module SemanticLoggerJournald
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/semantic_logger_journald/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "semantic_logger_journald"
5
+ spec.version = SemanticLoggerJournald::VERSION
6
+ spec.authors = ["Andrew Volozhanin"]
7
+ spec.email = ["scarfacedeb@gmail.com"]
8
+
9
+ spec.summary = %q{Plugin for semantic_logger gem providing systemd-journal appender}
10
+ spec.description = %q{Plugin for semantic_logger that adds appender to send logs into journald. It depends on journald-native gem that provides the C-bindings}
11
+ spec.homepage = "https://rubygems.org/gems/semantic_logger_journald"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/scarfacedeb/semantic_logger_journald/"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = `git ls-files`.split("\n")
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "semantic_logger", "~> 4.7"
25
+ spec.add_dependency "journald-logger", "~> 3.0"
26
+ end
@@ -0,0 +1,49 @@
1
+ require "test_helper"
2
+ require_relative "./journald_mock_logger"
3
+
4
+ module SemanticLoggerJournald
5
+ describe Appender do
6
+ before do
7
+ @logger = JournaldMockLogger.new
8
+ @appender = Appender.new(logger: @logger)
9
+ end
10
+
11
+ it "sends messages to journald logger" do
12
+ @appender.info("EVENT")
13
+
14
+ assert_equal 1, @logger.logs.count
15
+ log = @logger.logs.first
16
+ assert_equal "EVENT", log[:message]
17
+ end
18
+
19
+ it "sends payload to journald as fields" do
20
+ @appender.info("Signed In", user: { id: 1, name: "Snowden" })
21
+
22
+ log = @logger.logs.first
23
+ assert_equal "Signed In", log[:message]
24
+ assert_equal 1, log[:user__id]
25
+ assert_equal "Snowden", log[:user__name]
26
+ end
27
+
28
+ it "sends tags to journald as fields" do
29
+ @appender.tagged(ctx: "admin") do
30
+ @appender.tagged(:authorized) do
31
+ @appender.fatal("Server unavailable")
32
+ end
33
+ end
34
+
35
+ log = @logger.logs.first
36
+ assert_equal "Server unavailable", log[:message]
37
+ assert_equal "admin", log[:ctx]
38
+ assert_equal 1, log["authorized"]
39
+ end
40
+
41
+ it "sets correct priority based on level" do
42
+ @appender.error("Low capacity!")
43
+
44
+ log = @logger.logs.first
45
+ assert_equal "Low capacity!", log[:message]
46
+ assert_equal 3, log[:priority]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ class JournaldMockLogger
2
+ attr_reader :logs
3
+
4
+ def initialize
5
+ @logs = []
6
+ end
7
+
8
+ def send_message(msg)
9
+ @logs << msg
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require "test_helper"
2
+
3
+ describe SemanticLoggerJournald do
4
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
2
+ require "semantic_logger_journald"
3
+
4
+ require "minitest/autorun"
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_logger_journald
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Volozhanin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: semantic_logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: journald-logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Plugin for semantic_logger that adds appender to send logs into journald.
42
+ It depends on journald-native gem that provides the C-bindings
43
+ email:
44
+ - scarfacedeb@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/rake
57
+ - bin/rerun
58
+ - lib/semantic_logger_journald.rb
59
+ - lib/semantic_logger_journald/appender.rb
60
+ - lib/semantic_logger_journald/formatter.rb
61
+ - lib/semantic_logger_journald/version.rb
62
+ - semantic_logger_journald.gemspec
63
+ - test/appender_test.rb
64
+ - test/journald_mock_logger.rb
65
+ - test/semantic_logger_journald_test.rb
66
+ - test/test_helper.rb
67
+ homepage: https://rubygems.org/gems/semantic_logger_journald
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://rubygems.org/gems/semantic_logger_journald
72
+ source_code_uri: https://github.com/scarfacedeb/semantic_logger_journald/
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.3.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.1.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Plugin for semantic_logger gem providing systemd-journal appender
92
+ test_files:
93
+ - test/appender_test.rb
94
+ - test/journald_mock_logger.rb
95
+ - test/semantic_logger_journald_test.rb
96
+ - test/test_helper.rb