dry-logger 1.0.0.rc2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dry/logger.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "stringio"
4
-
3
+ require "dry/logger/global"
5
4
  require "dry/logger/constants"
5
+ require "dry/logger/clock"
6
6
  require "dry/logger/dispatcher"
7
7
 
8
8
  require "dry/logger/formatters/string"
@@ -42,74 +42,58 @@ module Dry
42
42
  # logger.info(Hello: "World!")
43
43
  # # {"progname":"my_app","severity":"INFO","time":"2022-11-06T10:11:29Z","Hello":"World!"}
44
44
  #
45
+ # @example Setting up multiple backends
46
+ # logger = Dry.Logger(:my_app)
47
+ # add_backend(formatter: :string, template: :details)
48
+ # add_backend(formatter: :string, template: :details)
49
+ #
50
+ # @example Setting up conditional logging
51
+ # logger = Dry.Logger(:my_app) { |setup|
52
+ # setup.add_backend(formatter: :string, template: :details) { |b| b.log_if = :error?.to_proc }
53
+ # }
54
+ #
55
+ # @param [String, Symbol] id The dispatcher id, can be used as progname in log entries
56
+ # @param [Hash] options Options for backends and formatters
57
+ # @option options [Symbol] :level (:info) The minimum level that should be logged,
58
+ # @option options [Symbol] :stream (optional) The output stream, default is $stdout
59
+ # @option options [Symbol, Class, #call] :formatter (:string) The default formatter or its id,
60
+ # @option options [String, Symbol] :template (:default) The default template that should be used
61
+ # @option options [Boolean] :colorize (false) Enable/disable colorized severity
62
+ # @option options [Hash<Symbol=>Symbol>] :severity_colors ({}) A severity=>color mapping
63
+ # @option options [#call] :on_crash (Dry::Logger::Dispatcher::ON_CRASH) A crash-handling proc.
64
+ # This is used whenever logging crashes.
65
+ #
45
66
  # @since 1.0.0
46
- # @return [Dispatcher]
47
67
  # @api public
48
- def self.Logger(id, **opts, &block)
49
- Logger::Dispatcher.setup(id, **opts, &block)
68
+ # @return [Dispatcher]
69
+ def self.Logger(id, **options, &block)
70
+ Logger::Dispatcher.setup(id, **options, &block)
50
71
  end
51
72
 
52
73
  module Logger
53
- # Register a new formatter
54
- #
55
- # @example
56
- # class MyFormatter < Dry::Logger::Formatters::Structured
57
- # def format(entry)
58
- # "WOAH: #{entry.message}"
59
- # end
60
- # end
61
- #
62
- # Dry::Logger.register_formatter(MyFormatter)
63
- #
64
- # @since 1.0.0
65
- # @return [Hash]
66
- # @api public
67
- def self.register_formatter(name, formatter)
68
- formatters[name] = formatter
69
- formatters
70
- end
71
-
72
- # Build a logging backend instance
73
- #
74
- # @since 1.0.0
75
- # @return [Backends::Stream]
76
- # @api private
77
- def self.new(stream: $stdout, **opts)
78
- backend =
79
- case stream
80
- when IO, StringIO then Backends::IO
81
- when String, Pathname then Backends::File
82
- else
83
- raise ArgumentError, "unsupported stream type #{stream.class}"
84
- end
74
+ extend Global
85
75
 
86
- formatter_opt = opts[:formatter]
87
-
88
- formatter =
89
- case formatter_opt
90
- when Symbol then formatters.fetch(formatter_opt).new(**opts)
91
- when Class then formatter_opt.new(**opts)
92
- when NilClass then formatters[:string].new(**opts)
93
- when ::Logger::Formatter then formatter_opt
94
- else
95
- raise ArgumentError, "unsupported formatter option #{formatter_opt.inspect}"
96
- end
76
+ # Built-in formatters
77
+ register_formatter(:string, Formatters::String)
78
+ register_formatter(:rack, Formatters::Rack)
79
+ register_formatter(:json, Formatters::JSON)
97
80
 
98
- backend_opts = opts.select { |key, _| BACKEND_OPT_KEYS.include?(key) }
81
+ # Built-in templates
82
+ register_template(:default, "%<message>s %<payload>s")
99
83
 
100
- backend.new(stream: stream, **backend_opts, formatter: formatter)
101
- end
84
+ register_template(:details, "[%<progname>s] [%<severity>s] [%<time>s] %<message>s %<payload>s")
102
85
 
103
- # Internal formatters registry
104
- #
105
- # @since 1.0.0
106
- # @api private
107
- def self.formatters
108
- @formatters ||= {}
109
- end
86
+ register_template(:crash, <<~STR)
87
+ [%<progname>s] [%<severity>s] [%<time>s] Logging crashed
88
+ %<log_entry>s
89
+ %<message>s (%<exception>s)
90
+ %<backtrace>s
91
+ STR
110
92
 
111
- register_formatter(:string, Formatters::String)
112
- register_formatter(:rack, Formatters::Rack)
113
- register_formatter(:json, Formatters::JSON)
93
+ register_template(:rack, <<~STR)
94
+ [%<progname>s] [%<severity>s] [%<time>s] \
95
+ %<verb>s %<status>s %<elapsed>s %<ip>s %<path>s %<length>s %<payload>s
96
+ %<params>s
97
+ STR
114
98
  end
115
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc2
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-08 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -36,17 +36,23 @@ files:
36
36
  - README.md
37
37
  - dry-logger.gemspec
38
38
  - lib/dry/logger.rb
39
+ - lib/dry/logger/backends/core.rb
39
40
  - lib/dry/logger/backends/file.rb
40
41
  - lib/dry/logger/backends/io.rb
42
+ - lib/dry/logger/backends/proxy.rb
41
43
  - lib/dry/logger/backends/stream.rb
44
+ - lib/dry/logger/clock.rb
42
45
  - lib/dry/logger/constants.rb
43
46
  - lib/dry/logger/dispatcher.rb
44
47
  - lib/dry/logger/entry.rb
45
48
  - lib/dry/logger/filter.rb
49
+ - lib/dry/logger/formatters/colors.rb
46
50
  - lib/dry/logger/formatters/json.rb
47
51
  - lib/dry/logger/formatters/rack.rb
48
52
  - lib/dry/logger/formatters/string.rb
49
53
  - lib/dry/logger/formatters/structured.rb
54
+ - lib/dry/logger/formatters/template.rb
55
+ - lib/dry/logger/global.rb
50
56
  - lib/dry/logger/version.rb
51
57
  homepage: https://dry-rb.org/gems/dry-logger
52
58
  licenses:
@@ -67,9 +73,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
73
  version: '3.0'
68
74
  required_rubygems_version: !ruby/object:Gem::Requirement
69
75
  requirements:
70
- - - ">"
76
+ - - ">="
71
77
  - !ruby/object:Gem::Version
72
- version: 1.3.1
78
+ version: '0'
73
79
  requirements: []
74
80
  rubygems_version: 3.1.6
75
81
  signing_key: