smart_logger_wrapper 0.3.1 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f45d9e44bc4f77ee5b3f83e20fb5e2218c730eb5
4
- data.tar.gz: 3cee85975631e80310d7bf41aa00af346cefae86
3
+ metadata.gz: f821646a21a7ca5704bedff4b51aad0655417dfa
4
+ data.tar.gz: 2dea06157c0c52b008fe3e991b3c6d10947ac99a
5
5
  SHA512:
6
- metadata.gz: f6c9bd513546ab8151367defcb7a210deab757ad5c20d313b2ae3ba00a4134230457030c8c231dc5c216808c7ca53d7f017131c06ed9139b2f7ae7c93cbd94ae
7
- data.tar.gz: 8fcd107646c2e6e2af92a78eccaba36287fee0177d6d90adbe1cb78ab734a28a6d34a801b84592df51c51753dc97603448996689b427e155454f45ff9f9c43d0
6
+ metadata.gz: cddf0ac43265639c9ef533974225c874dd3ef97f94aff290c0711579ee912670fb1c59164bef8374c62faf9d4aaf29009ba6d63a10be48aa01317e26c0d2152f
7
+ data.tar.gz: c1e9928de163ef319c761c51be91aa601f0ee810e4e2349101fafe740615b37df4fcec8f09faebbf9fdac37fdcd151413ddbf1ccdd75ad3b43242805a48124ba
data/README.md CHANGED
@@ -31,12 +31,12 @@ Wrap your logger with `SmartLoggerWrapper`, for example, in `config/environments
31
31
 
32
32
  Note that it is strongly recommended to use the wrapper for all kind of environments so that you can avoid exceptions such as `NoMethodError` due to the unique features of this library.
33
33
 
34
- You may want to put log messages to `STDOUT` in your development environment. Then:
34
+ You may want to put log messages to `STDERR` in your development environment. Then:
35
35
 
36
36
  ```ruby
37
37
  config.logger = SmartLoggerWrapper.new(
38
38
  SmartLoggerWrapper.new(Logger.new("log/development.log")).with_position,
39
- ActiveSupport::Logger.new(STDOUT)
39
+ ActiveSupport::Logger.new(STDERR)
40
40
  )
41
41
  ```
42
42
 
@@ -130,6 +130,49 @@ logger.append_backtrace.info 'A message'
130
130
  logger.append_backtrace(2).info 'A message'
131
131
  ```
132
132
 
133
+ ### Define your own options
134
+
135
+ You can define a new option by your own.
136
+
137
+ For instance, in the case you want to integrate a messenger, such as Slack, in a Rails app, you will define an initializer like this:
138
+
139
+ `config/initializers/some_messenger_integration.rb`
140
+
141
+ ```ruby
142
+ SmartLoggerWrapper::Options.define_redirector :to_messenger, Class.new(SmartLoggerWrapper::Options::Base) do
143
+ def apply!(messages, argument, severity, wrapper)
144
+ channel = argument || 'general'
145
+ formatter = wrapper.formatter
146
+ formatted_messages = messages.map { |message| formatter.call(severity, time, nil, message) }
147
+ Thread.new do
148
+ SomeMessenger.new(channel: channel).post(['```', *formatted_messages, '```'].join("\n"))
149
+ end
150
+ end
151
+ end
152
+ ```
153
+
154
+ Then, you can post log messages as follows:
155
+
156
+ ```ruby
157
+ Rails.logger.to_messenger('channel').error('foo')
158
+ ```
159
+
160
+ There are three categories for `SmartLoggerWrapper::Options`. Each option will be applied in the following order according to its category:
161
+
162
+ #### 1. Tagger
163
+
164
+ A tagger is expected to be used to tag each message. To define a tagger, you will call `SmartLoggerWrapper::Options.define_tagger`.
165
+
166
+ #### 2. Appender
167
+
168
+ An appender is expected to append some additinal information to the message list. To define an appender, you will call `SmartLoggerWrapper::Options.define_appender`.
169
+
170
+ #### 3. Redirector
171
+
172
+ A redirector should put messages to another location from the one where the wrapped logger specifies. To define a redirector, you will call `SmartLoggerWrapper::Options.define_redirector`.
173
+
174
+ Indeed, these categories don't restrict how you implement your options. You can, for example, tag messages with a redirector in your responsibility.
175
+
133
176
  ## Contributing
134
177
 
135
178
  Bug reports and pull requests are welcome on GitHub at https://github.com/akeyhero/smart_logger_wrapper.
@@ -7,15 +7,15 @@ class SmartLoggerWrapper < Logger
7
7
  class AppendBacktrace < Base
8
8
  include ::SmartLoggerWrapper::Utils::Backtrace
9
9
 
10
- def apply!(messages, value, logger)
11
- length = value.is_a?(Numeric) ? value : nil
10
+ def apply!(messages, argument, severity, wrapper)
11
+ length = argument.is_a?(Numeric) ? argument : nil
12
12
  messages << [
13
13
  'BACKTRACE:',
14
- *get_backtrace(logger.offset + APPLY_CALLER_STACK_DEPTH + 1, length)
14
+ *get_backtrace(wrapper.offset + APPLY_CALLER_STACK_DEPTH + 1, length)
15
15
  ].join("\n")
16
16
  end
17
17
  end
18
18
 
19
- define_appender :append_backtrace, AppendBacktrace.new
19
+ define_appender :append_backtrace, AppendBacktrace
20
20
  end
21
21
  end
@@ -3,7 +3,7 @@ require 'logger'
3
3
  class SmartLoggerWrapper < Logger
4
4
  module Options
5
5
  class Base
6
- def apply!(messages, value, logger)
6
+ def apply!(messages, argument, severity, wrapper)
7
7
  raise NotImplementedError, __callee__
8
8
  end
9
9
  end
@@ -4,14 +4,16 @@ require 'smart_logger_wrapper/options/base'
4
4
  class SmartLoggerWrapper < Logger
5
5
  module Options
6
6
  class To < Base
7
- def apply!(messages, value, logger)
8
- raise ApplicationError, 'No handler given' if value == nil
9
- value.puts messages.join("\n")
7
+ def apply!(messages, argument, severity, wrapper)
8
+ raise ApplicationError, 'No handler given' if argument == nil
9
+ time = Time.now
10
+ formatter = wrapper.formatter
11
+ argument.puts messages.map { |message| formatter.call(severity, time, nil, message) }.join("\n")
10
12
  rescue NoMethodError => e
11
13
  raise ApplicationError, e.message
12
14
  end
13
15
  end
14
16
 
15
- define_redirector :to, To.new
17
+ define_redirector :to, To
16
18
  end
17
19
  end
@@ -7,10 +7,10 @@ class SmartLoggerWrapper < Logger
7
7
  class WithPosition < Base
8
8
  include ::SmartLoggerWrapper::Utils::Path
9
9
 
10
- def apply!(messages, value, logger)
11
- return if value == false
10
+ def apply!(messages, argument, severity, wrapper)
11
+ return if argument == false
12
12
  # add 1 to `start` because this method dug the backtrace by 1
13
- location = caller_locations(logger.offset + APPLY_CALLER_STACK_DEPTH + 1, 1)
13
+ location = caller_locations(wrapper.offset + APPLY_CALLER_STACK_DEPTH + 1, 1)
14
14
  prefix =
15
15
  if location && location.length > 0
16
16
  method_name = location[0].label
@@ -24,6 +24,6 @@ class SmartLoggerWrapper < Logger
24
24
  end
25
25
  end
26
26
 
27
- define_tagger :with_position, WithPosition.new
27
+ define_tagger :with_position, WithPosition
28
28
  end
29
29
  end
@@ -9,10 +9,10 @@ class SmartLoggerWrapper < Logger
9
9
 
10
10
  module_function
11
11
 
12
- def apply_all!(messages, logger)
12
+ def apply_all!(messages, severity, logger)
13
13
  [defined_appenders, defined_taggers, defined_redirectors].flatten.each do |option_key|
14
14
  if logger.options.include?(option_key)
15
- defined_options[option_key].apply!(messages, logger.options[option_key], logger)
15
+ defined_options[option_key].apply!(messages, logger.options[option_key], severity, logger)
16
16
  end
17
17
  end
18
18
  end
@@ -20,7 +20,7 @@ class SmartLoggerWrapper < Logger
20
20
  def define_option(option_name, option_object, defined_option_keys)
21
21
  key = option_name.to_sym
22
22
  defined_option_keys.push(key)
23
- defined_options.merge!(key => option_object)
23
+ defined_options.merge!(key => option_object.new)
24
24
  end
25
25
 
26
26
  def define_appender(option_name, option_object)
@@ -1,5 +1,5 @@
1
1
  require 'logger'
2
2
 
3
3
  class SmartLoggerWrapper < Logger
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -31,7 +31,7 @@ class SmartLoggerWrapper < Logger
31
31
  # The return value is the first one of the first logger's.
32
32
  SEVERITY_MAPPING.each do |severity_name, severity|
33
33
  define_method(severity_name) do |*args, &block|
34
- format_messages(*args, &block).map do |message|
34
+ format_messages(severity, *args, &block).map do |message|
35
35
  loggers.map do |logger|
36
36
  logger.public_send(severity_name, message)
37
37
  end.first
@@ -61,11 +61,11 @@ class SmartLoggerWrapper < Logger
61
61
 
62
62
  private
63
63
 
64
- def format_messages(*args, &block)
64
+ def format_messages(severity, *args, &block)
65
65
  messages = args.map { |arg| to_message(arg) }
66
66
  messages << to_message(block.call) if block_given?
67
67
  begin
68
- Options.apply_all!(messages, self)
68
+ Options.apply_all!(messages, severity, self)
69
69
  rescue Options::ApplicationError => e
70
70
  loggers.each do |logger|
71
71
  logger.error(<<~EOM)
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Akihiro Katsura"]
10
10
  spec.email = ["info@kats.la"]
11
11
 
12
- spec.summary = %q{SmartLoggerWrapper adds some useful features to the Ruby Logger or the compatibles.}
13
- # spec.description = %q{SmartLoggerWrapper adds some useful features to the Ruby Logger or the compatibles.}
12
+ spec.summary = %q{SmartLoggerWrapper adds some useful features to the Ruby Logger or a subclass.}
13
+ # spec.description = %q{SmartLoggerWrapper adds some useful features to the Ruby Logger or a subclass.}
14
14
  spec.homepage = "https://github.com/akeyhero/smart_logger_wrapper"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_logger_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akihiro Katsura
@@ -101,5 +101,5 @@ rubyforge_project:
101
101
  rubygems_version: 2.6.8
102
102
  signing_key:
103
103
  specification_version: 4
104
- summary: SmartLoggerWrapper adds some useful features to the Ruby Logger or the compatibles.
104
+ summary: SmartLoggerWrapper adds some useful features to the Ruby Logger or a subclass.
105
105
  test_files: []