logger-logging 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
+ SHA1:
3
+ metadata.gz: a3a60891d92bf095eea5dde18974abd657eb3581
4
+ data.tar.gz: f58dd5a65e1f9b1f1f0c6ce4a359c197dd557ec8
5
+ SHA512:
6
+ metadata.gz: 6a86403c356d0b05d00c1e1fb0e4bd258c685ae7b23bfb4d8969be0412fd3d379eeae25ec8ec65c85be57408f6b97835174771d179f963b8de056708dc64f65b
7
+ data.tar.gz: 6147849afdeb9a028572f6f7500f6a8948c73c7f6887d80f7b7e0aed57b6e5448e3a8e61761385792ce419168445202081f3e2077dd1e3b0667835c727b165bb
@@ -0,0 +1,31 @@
1
+ require "logger"
2
+
3
+ module Logger::Logging
4
+ autoload :ColoredLogger, "logger/logging/colored_logger"
5
+ autoload :ExtendedLogger, "logger/logging/extended_logger"
6
+ autoload :Resolver, "logger/logging/resolver"
7
+
8
+ def logger= logger
9
+ if logger
10
+ logger = Resolver.(logger)
11
+
12
+ if self.is_a? Module
13
+ logger.progname = "(#{self.name})"
14
+ else
15
+ logger.progname = "(#{self.class.name})"
16
+ end
17
+ end
18
+
19
+ @logger = logger
20
+ end
21
+
22
+ def logger
23
+ @logger or NullLogger
24
+ end
25
+
26
+ module NullLogger
27
+ %i(data trace debug info warn error fatal unknown progname=).each do |log_level|
28
+ define_singleton_method log_level do |*| end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ module Logger::Logging
2
+ class ColoredLogger < ExtendedLogger
3
+ ANSI_COLORS = %i(black red green yellow blue magenta cyan white)
4
+
5
+ DEFAULT_PALETTE = {
6
+ "ANY" => -> msg { bg(:blue, :bright, fg(:white, :bright, msg)) },
7
+ "FATAL" => -> msg { bg(:red, :bright, fg(:white, :bright, msg)) },
8
+ "ERROR" => -> msg { fg(:red, :bright, msg) },
9
+ "WARN" => -> msg { fg(:yellow, :bright, msg) },
10
+ "INFO" => -> msg { fg(:default, :normal, msg) },
11
+ "DEBUG" => -> msg { fg(:cyan, :bright, msg) },
12
+ "TRACE" => -> msg { fg(:magenta, :bright, msg) },
13
+ "DATA" => -> msg { fg(:green, :bright, msg) },
14
+ }
15
+
16
+ attr_writer :palette
17
+
18
+ def bg *args
19
+ col :bg, *args
20
+ end
21
+
22
+ def col fgbg, color_code, intensity_code, str
23
+ color_num = ANSI_COLORS.index color_code
24
+ intensity_num = { :normal => 0, :bright => 1 }.fetch intensity_code
25
+ fgbg_num = { :fg => 3, :bg => 4 }.fetch fgbg
26
+ "\e[#{fgbg_num}#{color_num};#{intensity_num}m#{str}\e[0m"
27
+ end
28
+
29
+ def fg *args
30
+ col :fg, *args
31
+ end
32
+
33
+ def format_message severity, *args, progname, message
34
+ formatter = palette.fetch severity
35
+ colored_message = instance_exec message, &formatter
36
+ colored_progname = instance_exec progname, &formatter
37
+ super severity, *args, colored_progname, colored_message
38
+ end
39
+
40
+ def palette
41
+ @palette or DEFAULT_PALETTE
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module Logger::Logging
2
+ class ExtendedLogger < ::Logger
3
+ def format_severity severity
4
+ case severity
5
+ when -1 then "TRACE"
6
+ when -2 then "DATA"
7
+ else super
8
+ end
9
+ end
10
+
11
+ def trace progname = nil, &block
12
+ add -1, nil, progname, &block
13
+ end
14
+
15
+ def data progname = nil, &block
16
+ add -2, nil, progname, &block
17
+ end
18
+
19
+ def io
20
+ @logdev and @logdev.dev
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
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
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logger-logging
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Ladd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Logging module that adds colored logging to any class and puts the class
14
+ name in the log output for easy grepping
15
+ email: nathanladd+github@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/logger/logging.rb
21
+ - lib/logger/logging/colored_logger.rb
22
+ - lib/logger/logging/extended_logger.rb
23
+ - lib/logger/logging/resolver.rb
24
+ homepage: https://github.com/ntl/logger-logging
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Logging module that adds colored logging to any class
48
+ test_files: []