betterlog 2.0.6 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/betterlog/log.rb CHANGED
@@ -5,6 +5,16 @@ require 'betterlog/log/event_formatter'
5
5
  require 'betterlog/log/severity'
6
6
 
7
7
  module Betterlog
8
+ # A flexible, framework-agnostic logging solution that provides a clean API
9
+ # for structured logging with automatic Rails integration and error recovery.
10
+ #
11
+ # This class implements a singleton pattern using Tins::SexySingleton and
12
+ # automatically detects Rails environment to use its logger when available.
13
+ # It supports structured logging with contextual information, location
14
+ # tracking, and event notification capabilities.
15
+ #
16
+ # @example Basic usage
17
+ # Betterlog::Log.info("User logged in", meta: { user_id: 123 })
8
18
  class Log
9
19
  include Tins::SexySingleton
10
20
  extend ComplexConfig::Provider::Shortcuts
@@ -15,6 +25,12 @@ module Betterlog
15
25
  default_logger.level = level
16
26
  end
17
27
 
28
+ # Returns the appropriate logger instance for the application.
29
+ #
30
+ # This method checks if Rails is defined and has a logger available,
31
+ # falling back to a default logger otherwise.
32
+ #
33
+ # @return [Logger] The Rails logger if available, otherwise the default logger.
18
34
  def logger
19
35
  defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : self.class.default_logger
20
36
  end
@@ -22,7 +38,7 @@ module Betterlog
22
38
  # Logs a message on severity info.
23
39
  #
24
40
  # @param object this object is logged
25
- # @param **rest additional data is logged as well.
41
+ # @param rest additional data is logged as well.
26
42
  # @return [ Log ] this object itself.
27
43
  def info(object, **rest)
28
44
  protect do
@@ -34,7 +50,7 @@ module Betterlog
34
50
  # Logs a message on severity warn.
35
51
  #
36
52
  # @param object this object is logged
37
- # @param **rest additional data is logged as well.
53
+ # @param rest additional data is logged as well.
38
54
  # @return [ Log ] this object itself.
39
55
  def warn(object, **rest)
40
56
  protect do
@@ -46,7 +62,7 @@ module Betterlog
46
62
  # Logs a message on severity debug.
47
63
  #
48
64
  # @param object this object is logged
49
- # @param **rest additional data is logged as well.
65
+ # @param rest additional data is logged as well.
50
66
  # @return [ Log ] this object itself.
51
67
  def debug(object, **rest)
52
68
  protect do
@@ -58,7 +74,7 @@ module Betterlog
58
74
  # Logs a message on severity error.
59
75
  #
60
76
  # @param object this object is logged
61
- # @param **rest additional data is logged as well.
77
+ # @param rest additional data is logged as well.
62
78
  # @return [ Log ] this object itself.
63
79
  def error(object, **rest)
64
80
  protect do
@@ -70,7 +86,7 @@ module Betterlog
70
86
  # Logs a message on severity fatal.
71
87
  #
72
88
  # @param object this object is logged
73
- # @param **rest additional data is logged as well.
89
+ # @param rest additional data is logged as well.
74
90
  # @return [ Log ] this object itself.
75
91
  def fatal(object, **rest)
76
92
  protect do
@@ -83,7 +99,7 @@ module Betterlog
83
99
  # passing the severity: keyword.
84
100
  #
85
101
  # @param object this object is logged
86
- # @param **rest additional data is logged as well.
102
+ # @param rest additional data is logged as well.
87
103
  # @return [ Log ] this object itself.
88
104
  def output(object, **rest)
89
105
  protect do
@@ -91,10 +107,16 @@ module Betterlog
91
107
  end
92
108
  end
93
109
 
94
- def metric(name:, value: nil, success: -> result { true }, **rest, &block)
95
- warn "#{self.class}##{__method__} is deprecated"
96
- end
97
-
110
+ # Emits a log event by adding contextual information, notifying
111
+ # subscribers, and logging through the configured logger.
112
+ #
113
+ # This method enhances the provided event with location information if
114
+ # available, sets the emitter identifier, triggers any registered
115
+ # notifiers, and finally logs the event using the application's logger at
116
+ # the event's severity level.
117
+ #
118
+ # @param event [Betterlog::Log::Event] the log event to be emitted
119
+ # @return [Betterlog::Log] the log instance itself
98
120
  def emit(event)
99
121
  l = caller_locations.reverse_each.each_cons(3).find { |c, n1, n2|
100
122
  n2.absolute_path =~ /betterlog\/log\.rb/ and break c # TODO check if this still works
@@ -1,10 +1,31 @@
1
1
  module Betterlog
2
+ # Module for managing and coordinating log event notifiers.
3
+ #
4
+ # This module provides a centralized mechanism for registering and notifying
5
+ # objects when specific log events occur. It maintains a collection of
6
+ # notifier instances that can respond to log events, allowing for flexible
7
+ # integration with external monitoring, alerting, or logging systems.
8
+ # Notifiers are invoked when log events explicitly request notification,
9
+ # enabling decoupled communication between the logging system and downstream
10
+ # services.
11
+ #
12
+ # @see Betterlog::Log#emit
13
+ # @see Betterlog::Log::Event#notify?
2
14
  module Notifiers
3
15
 
4
16
  class_attr_accessor :notifiers
5
17
 
6
18
  self.notifiers = Set[]
7
19
 
20
+ # Registers a notifier object with the Notifiers module.
21
+ #
22
+ # This method adds a notifier to the collection of registered notifiers,
23
+ # ensuring that it responds to the required notify interface. The notifier
24
+ # will subsequently be invoked when log events with notification requests
25
+ # are emitted.
26
+ #
27
+ # @param notifier [ Object ] the notifier object to be registered
28
+ # @return [ Class ] Returns the Notifiers class itself
8
29
  def self.register(notifier)
9
30
  notifier.respond_to?(:notify) or raise TypeError,
10
31
  "notifier has to respond to notify(message, hash) interface"
@@ -12,6 +33,14 @@ module Betterlog
12
33
  self
13
34
  end
14
35
 
36
+ # Notifies registered notifiers with the event data.
37
+ #
38
+ # This method checks if the provided event has notification enabled,
39
+ # and if so, iterates through all registered notifiers to send them
40
+ # the event's message and metadata. It also updates the context for
41
+ # each notifier before sending the notification.
42
+ #
43
+ # @param event [Betterlog::Log::Event] the log event to be notified about
15
44
  def self.notify(event)
16
45
  event.notify? or return
17
46
  notifiers.each do |notifier|
@@ -20,6 +49,15 @@ module Betterlog
20
49
  end
21
50
  end
22
51
 
52
+ # Sets the context for all registered notifiers with the provided data.
53
+ #
54
+ # This method iterates through all registered notifiers and invokes their
55
+ # context method if they respond to it, passing the given data hash to
56
+ # allow notifiers to update their internal state or configuration based
57
+ # on the provided context information.
58
+ #
59
+ # @param data [ Hash ] A hash containing the context data to be passed
60
+ # to each notifier that supports the context interface
23
61
  def self.context(data)
24
62
  notifiers.each do |notifier|
25
63
  notifier.respond_to?(:context) or next
@@ -1,4 +1,12 @@
1
1
  module Betterlog
2
+ # Integrates Betterlog with Rails application initialization.
3
+ #
4
+ # This Railtie is responsible for configuring Rails to use Betterlog's legacy event formatter
5
+ # as the default logger formatter. It ensures that log messages are properly structured
6
+ # and enriched with metadata when used within a Rails application context.
7
+ #
8
+ # @see Betterlog::Log::LegacyEventFormatter
9
+ # @see Rails::Railtie
2
10
  class Railtie < Rails::Railtie
3
11
  initializer "betterlog_railtie.configure_rails_initialization" do
4
12
  require 'betterlog/log/legacy_event_formatter'
@@ -1,6 +1,6 @@
1
1
  module Betterlog
2
2
  # Betterlog version
3
- VERSION = '2.0.6'
3
+ VERSION = '2.1.1'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/lib/betterlog.rb CHANGED
@@ -3,6 +3,12 @@ require 'json'
3
3
  require 'complex_config'
4
4
  require 'term/ansicolor'
5
5
 
6
+ # Main module for Betterlog logging functionality.
7
+ #
8
+ # Provides structured logging tools designed for betterplace's logging
9
+ # infrastructure in Rails applications. Offers thread-local metadata
10
+ # management, event formatting, severity handling, and integration with Rails
11
+ # logging systems.
6
12
  module Betterlog
7
13
  end
8
14
 
@@ -16,3 +22,12 @@ if defined? Rails
16
22
  end
17
23
 
18
24
  Log = Betterlog::Log
25
+
26
+ # This class serves as a convenience alias for the Betterlog::Log module,
27
+ # providing a simple way to access the logging functionality throughout a Rails
28
+ # application
29
+ #
30
+ # @see Betterlog::Log
31
+ # @see Betterlog
32
+ class Log
33
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,5 @@
1
- if ENV['START_SIMPLECOV'].to_i == 1
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter "#{File.basename(File.dirname(__FILE__))}/"
5
- end
6
- end
1
+ require 'gem_hadar/simplecov'
2
+ GemHadar::SimpleCov.start
7
3
  require 'rspec'
8
4
  begin
9
5
  require 'debug'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace Developers
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-19 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: gem_hadar
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '1.19'
18
+ version: '1.28'
19
19
  type: :development
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '1.19'
25
+ version: '1.28'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -215,11 +215,7 @@ extra_rdoc_files:
215
215
  - lib/betterlog/railtie.rb
216
216
  - lib/betterlog/version.rb
217
217
  files:
218
- - ".all_images.yml"
219
- - ".github/workflows/codeql-analysis.yml"
220
- - ".gitignore"
221
- - ".semaphore/semaphore.yml"
222
- - ".tool-versions"
218
+ - CHANGES.md
223
219
  - Gemfile
224
220
  - LICENSE
225
221
  - README.md
@@ -258,16 +254,16 @@ require_paths:
258
254
  - lib
259
255
  required_ruby_version: !ruby/object:Gem::Requirement
260
256
  requirements:
261
- - - ">="
257
+ - - "~>"
262
258
  - !ruby/object:Gem::Version
263
- version: '0'
259
+ version: '3.2'
264
260
  required_rubygems_version: !ruby/object:Gem::Requirement
265
261
  requirements:
266
262
  - - ">="
267
263
  - !ruby/object:Gem::Version
268
264
  version: '0'
269
265
  requirements: []
270
- rubygems_version: 3.6.2
266
+ rubygems_version: 3.6.9
271
267
  specification_version: 4
272
268
  summary: Structured logging support for bp
273
269
  test_files:
data/.all_images.yml DELETED
@@ -1,18 +0,0 @@
1
- dockerfile: |-
2
- RUN apk add --no-cache yaml-dev build-base git
3
- RUN gem install gem_hadar
4
-
5
- script: &script |-
6
- echo -e "\e[1m"
7
- ruby -v
8
- echo -e "\e[0m"
9
- bundle
10
- rake spec
11
-
12
- fail_fast: yes
13
-
14
- images:
15
- ruby:3.4-alpine: *script
16
- ruby:3.3-alpine: *script
17
- ruby:3.2-alpine: *script
18
- ruby:3.1-alpine: *script
@@ -1,72 +0,0 @@
1
- # For most projects, this workflow file will not need changing; you simply need
2
- # to commit it to your repository.
3
- #
4
- # You may wish to alter this file to override the set of languages analyzed,
5
- # or to provide custom queries or build logic.
6
- #
7
- # ******** NOTE ********
8
- # We have attempted to detect the languages in your repository. Please check
9
- # the `language` matrix defined below to confirm you have the correct set of
10
- # supported CodeQL languages.
11
- #
12
- name: "CodeQL"
13
-
14
- on:
15
- push:
16
- branches: [ master ]
17
- pull_request:
18
- # The branches below must be a subset of the branches above
19
- branches: [ master ]
20
- schedule:
21
- - cron: '27 21 * * 1'
22
-
23
- jobs:
24
- analyze:
25
- name: Analyze
26
- runs-on: ubuntu-latest
27
- permissions:
28
- actions: read
29
- contents: read
30
- security-events: write
31
-
32
- strategy:
33
- fail-fast: false
34
- matrix:
35
- language: [ 'go', 'ruby' ]
36
- # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
- # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
38
-
39
- steps:
40
- - name: Checkout repository
41
- uses: actions/checkout@v3
42
-
43
- # Initializes the CodeQL tools for scanning.
44
- - name: Initialize CodeQL
45
- uses: github/codeql-action/init@v2
46
- with:
47
- languages: ${{ matrix.language }}
48
- # If you wish to specify custom queries, you can do so here or in a config file.
49
- # By default, queries listed here will override any specified in a config file.
50
- # Prefix the list here with "+" to use these queries and those in the config file.
51
-
52
- # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
53
- # queries: security-extended,security-and-quality
54
-
55
-
56
- # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
57
- # If this step fails, then you should remove it and run the build manually (see below)
58
- - name: Autobuild
59
- uses: github/codeql-action/autobuild@v2
60
-
61
- # ℹ️ Command-line programs to run using the OS shell.
62
- # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
63
-
64
- # If the Autobuild fails above, remove it and uncomment the following three lines.
65
- # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
66
-
67
- # - run: |
68
- # echo "Run, Build Application using script"
69
- # ./location_of_script_within_repo/buildscript.sh
70
-
71
- - name: Perform CodeQL Analysis
72
- uses: github/codeql-action/analyze@v2
data/.gitignore DELETED
@@ -1,16 +0,0 @@
1
- .*.sw[pon]
2
- .AppleDouble
3
- .DS_Store
4
- .bundle
5
- .byebug_history
6
- .ruby-version
7
- .rvmrc
8
- .utilsrc
9
- .yardoc
10
- Gemfile.lock
11
- betterlog-server
12
- coverage
13
- errors.lst
14
- gospace
15
- pkg
16
- tags
@@ -1,45 +0,0 @@
1
- version: v1.0
2
- name: Betterlog pipeline
3
- agent:
4
- machine:
5
- type: e1-standard-2
6
- os_image: ubuntu2004
7
-
8
- blocks:
9
- - name: Caching
10
- task:
11
- prologue:
12
- commands:
13
- - checkout
14
- jobs:
15
- - name: cache bundle
16
- commands:
17
- - sem-version ruby $(awk '/^ruby/ { print $2 }' .tool-versions)
18
- - cache restore gems-$SEMAPHORE_GIT_BRANCH,gems-master
19
- - bundle config set path 'vendor/bundle'
20
- - bundle config jobs $(getconf _NPROCESSORS_ONLN)
21
- - bundle install
22
- - cache store gems-$SEMAPHORE_GIT_BRANCH vendor/bundle
23
-
24
- - name: "Unit tests"
25
- task:
26
- env_vars:
27
- - name: RAILS_ENV
28
- value: test
29
- prologue:
30
- commands:
31
- - checkout
32
-
33
- # Setup ruby
34
- - sem-version ruby $(awk '/^ruby/ { print $2 }' .tool-versions)
35
-
36
- # Setup gems
37
- - cache restore gems-$SEMAPHORE_GIT_BRANCH,gems-master
38
- - bundle config set path 'vendor/bundle'
39
- - bundle config jobs $(getconf _NPROCESSORS_ONLN)
40
- - bundle install
41
-
42
- jobs:
43
- - name: RSpec Unit Tests
44
- commands:
45
- - bundle exec rake
data/.tool-versions DELETED
@@ -1 +0,0 @@
1
- ruby 3.4.1