extended_logger 0.2.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: e17adf2ff984b9032f24ae1c8253fa753c600beb
4
+ data.tar.gz: 2dbdeac01270bd403e35782cec74e12a63ad3bc0
5
+ SHA512:
6
+ metadata.gz: 93dcd7c69d32947290d0e24c7d0d737cca08d09190c25fda35fac973a0e8dc88eb35bd32310fa600943a0e36c3879efe08c8ecd9f9cd58d79737adec12a540c4
7
+ data.tar.gz: 1e5e1ffedbf2e93942250d19eab316efdd303d23f87536f402c741610a8d83b266aff747bf5cccd30306f9320efc25cb02e4c37ff99d9256cf8bbd6e5090b69d
@@ -0,0 +1,74 @@
1
+ class ExtendedLogger < Logger
2
+ def self.configure receiver, attribute_name=nil
3
+ attribute_name ||= 'logger'.freeze
4
+ setter = "#{attribute_name}=".freeze
5
+
6
+ logger = get receiver
7
+
8
+ receiver.public_send setter, logger
9
+ end
10
+
11
+ def self.get receiver
12
+ receiver_name =
13
+ if receiver.is_a? Module
14
+ receiver.name
15
+ else
16
+ receiver.class.name
17
+ end
18
+
19
+ logger = Factory.()
20
+
21
+ logger.progname = "(#{receiver_name})"
22
+ logger
23
+ end
24
+
25
+ def self.instance
26
+ @instance ||= Factory.()
27
+ end
28
+
29
+ def self.levels
30
+ @levels ||= {
31
+ -7 => 'OBSOLETE'.freeze,
32
+ -6 => 'OPT_DATA'.freeze,
33
+ -5 => 'OPT_TRACE'.freeze,
34
+ -4 => 'OPT_DEBUG'.freeze,
35
+ -3 => 'DATA'.freeze,
36
+ -2 => 'TRACE'.freeze,
37
+ -1 => 'DEBUG'.freeze,
38
+ 0 => 'INFO'.freeze,
39
+ 1 => 'PASS'.freeze,
40
+ 2 => 'FAIL'.freeze,
41
+ 3 => 'FOCUS'.freeze,
42
+ 4 => 'WARN'.freeze,
43
+ 5 => 'ERROR'.freeze,
44
+ 6 => 'FATAL'.freeze,
45
+ }
46
+ end
47
+
48
+ def self.level? level
49
+ levels.key? level
50
+ end
51
+
52
+ def format_severity severity
53
+ unless self.class.level? severity
54
+ Kernel.warn "(extended-logger) Unknown log severity level #{severity.inspect}"
55
+ end
56
+
57
+ self.class.levels.fetch severity, 'ANY'.freeze
58
+ end
59
+
60
+ def io
61
+ return unless @logdev
62
+ @logdev.dev
63
+ end
64
+
65
+ def unknown *arguments, &block
66
+ add 7, *arguments, &block
67
+ end
68
+
69
+ levels.each do |level, name|
70
+ define_method name.downcase do |*arguments, &block|
71
+ add level, *arguments, &block
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,51 @@
1
+ class ExtendedLogger
2
+ class Factory
3
+ attr_reader :env
4
+
5
+ def initialize env
6
+ @env = env
7
+ end
8
+
9
+ def self.build env=nil
10
+ env ||= ENV
11
+ new env
12
+ end
13
+
14
+ def self.call *arguments
15
+ instance = build *arguments
16
+ instance.()
17
+ end
18
+
19
+ def call
20
+ logger = ExtendedLogger.new device
21
+
22
+ formatter = Formatter.new
23
+ formatter.palette = Formatter.default_palette if colors? logger.io
24
+ logger.formatter = formatter
25
+
26
+ level = self.level
27
+ logger.level = level if level
28
+
29
+ logger
30
+ end
31
+
32
+ def colors? io
33
+ if io.tty?
34
+ ENV['LOG_COLOR'] != 'off'
35
+ else
36
+ ENV['LOG_COLOR'] == 'on'
37
+ end
38
+ end
39
+
40
+ def device
41
+ ENV['LOG_DEVICE'] || $stderr
42
+ end
43
+
44
+ def level
45
+ if ENV.key? 'LOG_LEVEL'
46
+ level , _ = ExtendedLogger.levels.rassoc ENV['LOG_LEVEL'].upcase
47
+ level
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,89 @@
1
+ class ExtendedLogger
2
+ class Formatter
3
+ attr_writer :logger_formatter
4
+ attr_writer :palette
5
+
6
+ def call severity, *arguments
7
+ log_entry = delegate severity, *arguments
8
+ color log_entry, severity
9
+ end
10
+
11
+ def color log_entry, severity
12
+ colorizer = palette[severity]
13
+
14
+ if colorizer
15
+ log_entry = "#{colorizer}#{log_entry}\e[0m"
16
+ end
17
+
18
+ log_entry
19
+ end
20
+
21
+ def delegate *arguments, message
22
+ message = format_message message, arguments
23
+ logger_formatter.(*arguments, message)
24
+ end
25
+
26
+ def format_message message, arguments
27
+ output = ''
28
+
29
+ message.each_line "\n".freeze do |line|
30
+ if line == "\n".freeze
31
+ line = "\\n"
32
+ else
33
+ line.gsub! "\r".freeze, "\\r".freeze
34
+ end
35
+
36
+ if output.empty?
37
+ output.concat line
38
+ else
39
+ formatted_line = logger_formatter.(*arguments, line.chomp("\n".freeze))
40
+ output.concat formatted_line
41
+ end
42
+ end
43
+
44
+ output.chomp "\n".freeze
45
+ end
46
+
47
+ def logger_formatter
48
+ @logger_formatter ||= Logger::Formatter.new
49
+ end
50
+
51
+ def palette
52
+ @palette ||= {}
53
+ end
54
+
55
+ def self.ansi_colors
56
+ @ansi_colors ||= %i(black red green yellow blue magenta cyan white)
57
+ end
58
+
59
+ def self.col fg, brightness, bg=nil
60
+ brightness = { :bright => 1, :normal => 0 }.fetch brightness
61
+ escape = "\e[#{brightness};3#{ansi_colors.index fg}m"
62
+
63
+ if bg
64
+ escape << "\e[4#{ansi_colors.index bg}m"
65
+ end
66
+
67
+ escape
68
+ end
69
+
70
+ def self.default_palette
71
+ @default_palette ||= {
72
+ 'OBSOLETE' => col(:black, :normal),
73
+ 'DATA' => col(:green, :normal),
74
+ 'TRACE' => col(:cyan, :normal),
75
+ 'DEBUG' => col(:blue, :normal),
76
+ 'OPT_DATA' => col(:green, :normal, :white),
77
+ 'OPT_TRACE' => col(:cyan, :normal, :white),
78
+ 'OPT_DEBUG' => col(:blue, :normal, :white),
79
+ 'PASS' => col(:white, :bright, :green),
80
+ 'FAIL' => col(:white, :bright, :red),
81
+ 'FOCUS' => col(:white, :bright, :blue),
82
+ 'WARN' => col(:yellow, :normal),
83
+ 'ERROR' => col(:red, :normal),
84
+ 'FATAL' => col(:black, :bright, :red),
85
+ 'ANY' => col(:white, :bright, :magenta),
86
+ }
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,5 @@
1
+ require 'logger'
2
+
3
+ require 'extended_logger/extended_logger'
4
+ require 'extended_logger/factory'
5
+ require 'extended_logger/formatter'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Ladd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ extended-logger provides a richer logger implementation than the built in ruby
15
+ logger, offering more log levels and colors. In addition, loggers can be
16
+ constructed for your classes and modules, which include the constant name in
17
+ output for easy filtering.
18
+ email: nathanladd+github@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/extended_logger.rb
24
+ - lib/extended_logger/extended_logger.rb
25
+ - lib/extended_logger/factory.rb
26
+ - lib/extended_logger/formatter.rb
27
+ homepage: https://github.com/ntl/extended-logger
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.4.5.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Provides an extended logger offering more features than the ruby logger
51
+ test_files: []
52
+ has_rdoc: