logger-logging 0.9.1 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2715dddd09be9273911ce809178b1005e1316ebf
4
- data.tar.gz: 3a986841694875c941e3fe3ce9e8704d6b21d604
3
+ metadata.gz: d3d9b850d7b6504436f37025a32e0cf5852c3199
4
+ data.tar.gz: 1ab5d3e62bd3263ff44a88dd15a000aa241c5366
5
5
  SHA512:
6
- metadata.gz: 2a439dc9d9f2b1faba53c5deb3cc03b5868de5d76af9ff5c1d5ff7794699041ace356545bb8f4e38dc8317337c6641a52b6aff93a58559613e5fb3b6f2b60367
7
- data.tar.gz: 6fedac0061944f7ed7cc5d00d70aed1d3c0e6d5af4dd8f52ca1adbd77a173b411946336bb62967bdc2ee18b2a6127215b456f9ced3b92a8ae77744e8d564b2bd
6
+ metadata.gz: 1e79502808ac4ea93020e1b1fe8aa5d7c076c1ce16fb7ff94e09fae03c3f4ea2587c8fd7eed66e0b23b3a4a9bc21158051c580de7a1b8eb9156bf35f259ec814
7
+ data.tar.gz: 7aa79aa2e311218e704d49924b7b664b9ba636681b50c4296a2eedcb7d62f63b1ed0707b63f05a418c3b39de51de87bc991cbb24fd7114ad17f27643db2d7ef3
@@ -3,17 +3,30 @@ require "logger"
3
3
  module Logger::Logging
4
4
  autoload :ColoredLogger, "logger/logging/colored_logger"
5
5
  autoload :ExtendedLogger, "logger/logging/extended_logger"
6
- autoload :Resolver, "logger/logging/resolver"
6
+ autoload :Factory, "logger/logging/factory"
7
+
8
+ def self.configure receiver, env: nil
9
+ if env
10
+ receiver.logger = Factory.(env)
11
+ else
12
+ receiver.logger = instance
13
+ end
14
+ nil
15
+ end
16
+
17
+ def self.instance
18
+ @logger ||= Factory.()
19
+ end
7
20
 
8
21
  def logger= logger
9
22
  if logger
10
- logger = Resolver.(logger)
11
-
12
23
  if self.is_a? Module
13
24
  logger.progname = "(#{self.name})"
14
25
  else
15
26
  logger.progname = "(#{self.class.name})"
16
27
  end
28
+
29
+ logger
17
30
  end
18
31
 
19
32
  @logger = logger
@@ -8,12 +8,28 @@ module Logger::Logging
8
8
  end
9
9
  end
10
10
 
11
- def trace progname = nil, &block
12
- add -1, nil, progname, &block
11
+ def trace message_or_progname = nil, &block
12
+ add -1, nil, message_or_progname, &block
13
13
  end
14
14
 
15
- def data progname = nil, &block
16
- add -2, nil, progname, &block
15
+ def data message_or_progname = nil, &block
16
+ if block_given?
17
+ progname = message_or_progname
18
+ message = yield
19
+ else
20
+ message = message_or_progname
21
+ end
22
+
23
+ message.each_line "\n" do |line|
24
+ if line == "\n"
25
+ line = "\\n"
26
+ else
27
+ line.gsub! "\r", "\\r"
28
+ line.chomp!
29
+ end
30
+
31
+ add -2, nil, line
32
+ end
17
33
  end
18
34
 
19
35
  def io
@@ -0,0 +1,61 @@
1
+ module Logger::Logging
2
+ class Factory
3
+ attr_reader :env
4
+
5
+ def initialize env
6
+ @env = env
7
+ end
8
+
9
+ def self.call env = nil
10
+ env ||= ENV
11
+ instance = new env
12
+ instance.()
13
+ end
14
+
15
+ def call
16
+ logger = logger_class.new device
17
+ logger.level = level
18
+ logger
19
+ end
20
+
21
+ def device
22
+ @device ||=
23
+ begin
24
+ path = env.fetch "LOG_DEVICE" do "/dev/stdout" end
25
+ if path.is_a? String then File.open path, "a" else path end
26
+ end
27
+ end
28
+
29
+ def level
30
+ @level ||=
31
+ begin
32
+ level_name = env.fetch "LOG_LEVEL" do "WARN" end
33
+ level_name = level_name.upcase
34
+
35
+ if Logger.const_defined? level_name
36
+ Logger.const_get level_name
37
+ else
38
+ {
39
+ "DATA" => -2,
40
+ "TRACE" => -1,
41
+ }.fetch level_name
42
+ end
43
+ end
44
+ end
45
+
46
+ def logger_class
47
+ if coloring? then ColoredLogger else ExtendedLogger end
48
+ end
49
+
50
+ def coloring?
51
+ return @coloring unless @coloring.nil?
52
+ @coloring =
53
+ begin
54
+ use_colors = env.fetch "LOG_COLOR" do
55
+ if device.tty? then "on" else "off" end
56
+ end
57
+ use_colors == "on"
58
+ end
59
+ end
60
+ end
61
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logger-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Ladd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-19 00:00:00.000000000 Z
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Logging module that adds colored logging to any class and puts the class
14
14
  name in the log output for easy grepping
@@ -20,7 +20,7 @@ files:
20
20
  - lib/logger/logging.rb
21
21
  - lib/logger/logging/colored_logger.rb
22
22
  - lib/logger/logging/extended_logger.rb
23
- - lib/logger/logging/resolver.rb
23
+ - lib/logger/logging/factory.rb
24
24
  homepage: https://github.com/ntl/logger-logging
25
25
  licenses:
26
26
  - MIT
@@ -1,22 +0,0 @@
1
- module Logger::Logging
2
- class Resolver
3
- def self.call logger
4
- if logger.is_a? ExtendedLogger
5
- logger
6
- elsif logger.is_a? Logger
7
- logdev = logger.instance_variable_get :@logdev
8
- device = logdev.dev
9
- if device.tty?
10
- new_logger = ColoredLogger.new device
11
- else
12
- new_logger = ExtendedLogger.new device
13
- end
14
- new_logger.level = logger.level
15
- new_logger.progname = logger.progname
16
- new_logger
17
- else
18
- raise ArgumentError, "could not resolve logger for #{logger.inspect}"
19
- end
20
- end
21
- end
22
- end