on_strum-logs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8185297963aeab5a76271658b4efca0e09834f30d1252ee89b3100f70cfd791a
4
+ data.tar.gz: d994258f60226574ea0c6f8a4c5729a16000f6300ade60003da39ffea8e4af9a
5
+ SHA512:
6
+ metadata.gz: 5a790f196642f7d785cc71f1c4fc3ea16ba14c73e62ec21a7cfc8a0845a580d5102a7b720c417465e41d570c5fa52d2f5630fba52d26f3c6db96c3331193b587
7
+ data.tar.gz: f57c8918327915eba796f8c28c41544f29e5af213bc2194621b99ccb78cbc8fc45e5e7b2ec89264db27f5cdc0dd7e0aeeb1f23333ee7aae411cc8247c209a896
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-2023 onStrum and friends
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.
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'on_strum'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ class Configuration
6
+ INCOMPLETE_CONFIG = 'service_name, service_version are required parameters'
7
+ SETTERS = %i[custom_formatter service_name service_version].freeze
8
+
9
+ attr_reader(*OnStrum::Logs::Configuration::SETTERS)
10
+ attr_accessor :detailed_formatter
11
+
12
+ def initialize(&block)
13
+ tap(&block) if block
14
+ end
15
+
16
+ OnStrum::Logs::Configuration::SETTERS.each do |method|
17
+ define_method("#{method}=") do |argument|
18
+ raise_unless(argument, __method__, valid_argument_type?(method, argument))
19
+ instance_variable_set(:"@#{method}", argument)
20
+ end
21
+ end
22
+
23
+ def complete?
24
+ !!(service_name && service_version)
25
+ end
26
+
27
+ def formatter
28
+ custom_formatter || builded_formatter
29
+ end
30
+
31
+ private
32
+
33
+ def valid_argument_type?(method_name, argument)
34
+ argument.is_a?(
35
+ case method_name
36
+ when :service_name, :service_version then ::String
37
+ when :custom_formatter then ::Class
38
+ end
39
+ )
40
+ end
41
+
42
+ def raise_unless(argument_context, argument_name, condition)
43
+ raise OnStrum::Logs::Error::ArgumentType.new(argument_context, argument_name) unless condition
44
+ end
45
+
46
+ def builded_formatter
47
+ return OnStrum::Logs::Formatter::Detailed if detailed_formatter
48
+
49
+ OnStrum::Logs::Formatter::Json
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Error
6
+ require_relative 'error/argument_type'
7
+ require_relative 'error/configuration'
8
+ require_relative 'error/logger'
9
+ end
10
+
11
+ module Formatter
12
+ require_relative 'formatter/base'
13
+ require_relative 'formatter/json'
14
+ require_relative 'formatter/detailed'
15
+ end
16
+
17
+ require_relative 'version'
18
+ require_relative 'configuration'
19
+
20
+ module Logger
21
+ require_relative 'logger/base'
22
+ require_relative 'logger/default'
23
+ end
24
+
25
+ require_relative '../logs'
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Error
6
+ ArgumentType = ::Class.new(::ArgumentError) do
7
+ def initialize(arg_value, arg_name)
8
+ super("#{arg_value} is not a valid #{arg_name}")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Error
6
+ Configuration = ::Class.new(::StandardError) do
7
+ def initialize(message = 'use OnStrum::Logs.configure before')
8
+ super(message)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Error
6
+ Logger = ::Class.new(::RuntimeError) do
7
+ def initialize(message = 'attribute :message is required')
8
+ super(message)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Formatter
6
+ class Base
7
+ DATETIME_FORMAT = '%FT%T.%3N%:z'
8
+ LOG_ATTRIBUTES_ORDER = %i[level time message context service_name service_version].freeze
9
+
10
+ def self.arrange_attrs(**log_data)
11
+ OnStrum::Logs::Formatter::Base::LOG_ATTRIBUTES_ORDER.each_with_object({}) do |attribute, arranged_attrs|
12
+ arranged_attrs[attribute] = log_data[attribute]
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Formatter
6
+ class Detailed < Base
7
+ require 'amazing_print'
8
+
9
+ def self.call(**log_data)
10
+ ::Kernel.ap(arrange_attrs(**log_data))
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Formatter
6
+ class Json < Base
7
+ require 'json'
8
+
9
+ def self.call(time:, **log_data)
10
+ json_log = arrange_attrs(
11
+ time: time.strftime(OnStrum::Logs::Formatter::Base::DATETIME_FORMAT),
12
+ **log_data
13
+ ).to_json
14
+
15
+ "#{json_log}\n"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Logger
6
+ require 'logger'
7
+
8
+ Base = ::Class.new(::Logger)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ module Logger
6
+ class Default
7
+ LOG_LEVELS = %i[info error debug].freeze
8
+
9
+ def self.instance
10
+ @instance ||= new
11
+ end
12
+
13
+ def initialize
14
+ @logger = OnStrum::Logs::Logger::Base.new($stdout, formatter: formatter)
15
+ end
16
+
17
+ OnStrum::Logs::Logger::Default::LOG_LEVELS.each do |method_name|
18
+ define_method(method_name) do |arg|
19
+ raise OnStrum::Logs::Error::Configuration unless configuration
20
+
21
+ # TODO: we need to have ability to process log data before render it to STDOUT/STDERR
22
+ # hash_normalizer(arg); after_callback.call(arg)
23
+ logger.public_send(method_name, hash_normalizer(arg).merge(level: method_name.to_s.upcase))
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :logger
30
+
31
+ def configuration
32
+ OnStrum::Logs.configuration
33
+ end
34
+
35
+ def formatter
36
+ @formatter ||= proc do |_severity, datetime, _progname, log_data|
37
+ configuration.formatter.call(
38
+ time: datetime,
39
+ service_name: configuration.service_name,
40
+ service_version: configuration.service_version,
41
+ **log_data
42
+ )
43
+ end
44
+ end
45
+
46
+ def hash_normalizer(object)
47
+ case object
48
+ when ::Hash
49
+ raise OnStrum::Logs::Error::Logger unless object.key?(:message)
50
+
51
+ { message: object.delete(:message), context: (object.empty? ? nil : object) }
52
+ when ::Exception
53
+ {
54
+ message: "Exception: #{object.class}",
55
+ context: {
56
+ message: object.message,
57
+ stack_trace: object.backtrace
58
+ }
59
+ }
60
+ else { message: object.to_s, context: nil }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ class << self
6
+ def configuration(&block)
7
+ @configuration ||= begin
8
+ return unless block
9
+
10
+ configuration = OnStrum::Logs::Configuration.new(&block)
11
+ raise OnStrum::Logs::Error::Configuration, OnStrum::Logs::Configuration::INCOMPLETE_CONFIG unless configuration.complete?
12
+
13
+ configuration
14
+ end
15
+ end
16
+
17
+ def configure(&block)
18
+ configuration(&block)
19
+ end
20
+
21
+ def reset_configuration!
22
+ @configuration = nil
23
+ end
24
+
25
+ OnStrum::Logs::Logger::Default::LOG_LEVELS.each do |method|
26
+ define_method(method) do |*arg|
27
+ OnStrum::Logs::Logger::Default.instance.public_send(method, *arg)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/on_strum.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnStrum
4
+ module Logs
5
+ require_relative 'on_strum/logs/core'
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/on_strum/logs/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'on_strum-logs'
7
+ spec.version = OnStrum::Logs::VERSION
8
+ spec.authors = ['Vladislav Trotsenko']
9
+ spec.email = %w[admin@on-strum.org]
10
+
11
+ spec.summary = %(on_strum-logs)
12
+ spec.description = %(Simple structured logger)
13
+
14
+ spec.homepage = 'https://github.com/on-strum/ruby-on-strum-logs'
15
+ spec.license = 'MIT'
16
+
17
+ spec.metadata = {
18
+ 'homepage_uri' => 'https://github.com/on-strum/ruby-on-strum-logs',
19
+ 'changelog_uri' => 'https://github.com/on-strum/ruby-on-strum-logs/blob/master/CHANGELOG.md',
20
+ 'source_code_uri' => 'https://github.com/on-strum/ruby-on-strum-logs',
21
+ 'documentation_uri' => 'https://github.com/on-strum/ruby-on-strum-logs/blob/master/README.md',
22
+ 'bug_tracker_uri' => 'https://github.com/on-strum/ruby-on-strum-logs/issues'
23
+ }
24
+
25
+ spec.required_ruby_version = '>= 2.5.0'
26
+ spec.files = `git ls-files -z`.split("\x0").select { |f| f.match(%r{^(bin|lib)/|.ruby-version|on_strum-logs.gemspec|LICENSE}) }
27
+ spec.require_paths = %w[lib]
28
+
29
+ spec.add_runtime_dependency 'amazing_print', '~> 1.5'
30
+
31
+ spec.add_development_dependency 'ffaker', '~> 2.21'
32
+ spec.add_development_dependency 'json_matchers', '~> 0.11.1'
33
+ spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6'
34
+ spec.add_development_dependency 'rspec', '~> 3.12'
35
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: on_strum-logs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladislav Trotsenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: amazing_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffaker
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.21'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.21'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json_matchers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.11.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.11.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 13.0.6
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '13.0'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 13.0.6
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.12'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.12'
89
+ description: Simple structured logger
90
+ email:
91
+ - admin@on-strum.org
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".ruby-version"
97
+ - LICENSE.txt
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/on_strum.rb
101
+ - lib/on_strum/logs.rb
102
+ - lib/on_strum/logs/configuration.rb
103
+ - lib/on_strum/logs/core.rb
104
+ - lib/on_strum/logs/error/argument_type.rb
105
+ - lib/on_strum/logs/error/configuration.rb
106
+ - lib/on_strum/logs/error/logger.rb
107
+ - lib/on_strum/logs/formatter/base.rb
108
+ - lib/on_strum/logs/formatter/detailed.rb
109
+ - lib/on_strum/logs/formatter/json.rb
110
+ - lib/on_strum/logs/logger/base.rb
111
+ - lib/on_strum/logs/logger/default.rb
112
+ - lib/on_strum/logs/version.rb
113
+ - on_strum-logs.gemspec
114
+ homepage: https://github.com/on-strum/ruby-on-strum-logs
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ homepage_uri: https://github.com/on-strum/ruby-on-strum-logs
119
+ changelog_uri: https://github.com/on-strum/ruby-on-strum-logs/blob/master/CHANGELOG.md
120
+ source_code_uri: https://github.com/on-strum/ruby-on-strum-logs
121
+ documentation_uri: https://github.com/on-strum/ruby-on-strum-logs/blob/master/README.md
122
+ bug_tracker_uri: https://github.com/on-strum/ruby-on-strum-logs/issues
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.5.0
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.2.15
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: on_strum-logs
142
+ test_files: []