color-console 0.3 → 0.3.2

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.
@@ -49,7 +49,10 @@ module Console
49
49
  # suitable for output to the console.
50
50
  class RubyFormatter < Formatter
51
51
 
52
- DEFAULT_FORMAT = '%4$-6s %5$s%n'
52
+ # Default format pattern
53
+ DEFAULT_FORMAT = '%4$-6s %7$s%5$s'
54
+ # System line-ending
55
+ LINE_END = java.lang.String.format('%n')
53
56
 
54
57
  # A format string to use when formatting a log record.
55
58
  # @see Java function String.format for the format string syntax. The
@@ -59,8 +62,16 @@ module Console
59
62
  # - logger_name The name of the logger that logged the record
60
63
  # - level The level of the message
61
64
  # - message The log message
62
- # - thrown Any exception that forms part of the log record.
65
+ # - thrown Any exception that forms part of the log record
66
+ # - spacer A spacer that will consist of 2 spaces if the log level
67
+ # is config or greater.
63
68
  attr_accessor :format_string
69
+ # Width at which to split lines
70
+ attr_accessor :width
71
+ # Amount by which to indent lines
72
+ attr_accessor :indent
73
+ # Level labels
74
+ attr_reader :level_labels
64
75
 
65
76
 
66
77
  # Constructs a new formatter for formatting log records according to
@@ -68,27 +79,52 @@ module Console
68
79
  #
69
80
  # @param format The format string to use when building a String for
70
81
  # logging.
71
- def initialize(format = DEFAULT_FORMAT)
82
+ def initialize(format = DEFAULT_FORMAT, width = nil)
72
83
  super()
73
84
  @format_string = format
85
+ @width = width || Console.width
86
+ mark = java.lang.String.format(@format_string, Time.now,
87
+ '', '', '', '!$!', nil, nil)
88
+ @indent = mark.lines.first.index('!$!')
89
+ @level_labels = Hash.new{ |h, k| h[k] = k }
90
+ @level_labels[JavaUtilLogger::Level::WARNING] = 'WARN'
91
+ @level_labels[JavaUtilLogger::Level::SEVERE] = 'ERROR'
92
+ @level_labels[JavaUtilLogger::Level::FINEST] = 'DEBUG'
74
93
  end
75
94
 
76
95
 
77
96
  # Format a log record and return a string for publishing by a log handler.
78
97
  def format(log_record)
79
- lvl = case log_record.level
80
- when JavaUtilLogger::Level::WARNING then 'WARN'
81
- when JavaUtilLogger::Level::SEVERE then 'ERROR'
82
- when JavaUtilLogger::Level::FINEST then 'DEBUG'
83
- else log_record.level
84
- end
85
- java.lang.String.format(@format_string,
86
- log_record.millis,
87
- log_record.logger_name,
88
- log_record.logger_name,
89
- lvl,
90
- log_record.message,
91
- log_record.thrown)
98
+ lvl = @level_labels[log_record.level]
99
+ indent = @indent || 0
100
+ spacer = ''
101
+ wrap_width = @width - indent
102
+ if log_record.level.intValue < JavaUtilLogger::Level::INFO.intValue
103
+ spacer = ' '
104
+ wrap_width -= 2
105
+ end
106
+
107
+ msg = wrap_width > 0 ? Console.wrap_text(log_record.message, wrap_width) :
108
+ [log_record.message]
109
+ sb = java.lang.StringBuilder.new()
110
+ msg.each_with_index do |line, i|
111
+ if i == 0
112
+ fmt = java.lang.String.format(@format_string,
113
+ log_record.millis,
114
+ log_record.logger_name,
115
+ log_record.logger_name,
116
+ lvl,
117
+ msg[i],
118
+ log_record.thrown,
119
+ spacer)
120
+ else
121
+ fmt = java.lang.String.format(@format_string,
122
+ log_record.millis, '', '', '', msg[i], nil, spacer)
123
+ end
124
+ sb.append(fmt)
125
+ sb.append(LINE_END) if @width < 0 || fmt.length < @width
126
+ end
127
+ sb.toString()
92
128
  end
93
129
 
94
130
  end
@@ -117,6 +153,9 @@ module Console
117
153
 
118
154
  # Add a ColorConsoleHandler
119
155
  h = JavaUtilLogger::ColorConsoleHandler.new(format)
156
+ if lbls = options[:level_labels]
157
+ h.formatter.level_labels.merge!(lbls)
158
+ end
120
159
  l.addHandler(h)
121
160
 
122
161
  # Set the log level
@@ -2,7 +2,7 @@ require 'log4r'
2
2
  require_relative '../color_console'
3
3
 
4
4
 
5
- # A module for using our color console for log output with the java.util.logging
5
+ # A module for using our color console for log output with the Log4r
6
6
  # log framework.
7
7
  module Console
8
8
 
@@ -22,7 +22,8 @@ module Console
22
22
  def format(event)
23
23
  @fg = case event.level
24
24
  when Log4r::INFO then :white
25
- when Log4r::WARN then :yellow
25
+ when Log4r::CONFIG then :cyan
26
+ when Log4r::WARNING then :yellow
26
27
  when Log4r::ERROR then :red
27
28
  when Log4r::DEBUG then :dark_gray
28
29
  else :light_gray
@@ -33,7 +34,8 @@ module Console
33
34
  thread = Log4r::NDC.peek.to_s.upcase[0, 2]
34
35
  level = Log4r::LNAMES[event.level]
35
36
  case
36
- when event.data.is_a?(Exception) || event.data.java_kind_of?(java.lang.Throwable)
37
+ when event.data.is_a?(Exception) || (RUBY_ENGINE == 'jruby' &&
38
+ event.data.java_kind_of?(java.lang.Throwable))
37
39
  msg = Exception.format(event.data)
38
40
  when event.data.is_a?(Array)
39
41
  msg = event.data
@@ -42,17 +44,18 @@ module Console
42
44
  when Array then msg[0][0] = "%-8s %-2s %s" % [level, thread, msg[0][0]]
43
45
  end
44
46
  else
45
- msg = Console.wrap_text(event.data, @console_width - 16)
46
- msg = msg.each_with_index.map do |line, i|
47
- "%-8s %-2s %s" % [[level][i], [thread][i], line]
48
- end.join("\n")
47
+ msg_lines = Console.wrap_text(event.data, @console_width - 13)
48
+ msg = ""
49
+ msg_lines.each_with_index do |line, i|
50
+ fmt = "%-8s %-2s %s" % [[level][i], [thread][i], line]
51
+ msg += fmt
52
+ msg += "\n" if @console_width < 0 || fmt.length < @console_width
53
+ end
49
54
  end
50
- msg
55
+ msg.chomp
51
56
  rescue Object => ex
52
- "(Unable to format event.data)" # due to #{ex})\n"
57
+ "(Unable to format event.data due to #{ex})\n"
53
58
  end
54
- else
55
- ""
56
59
  end
57
60
  end
58
61
 
@@ -60,14 +63,14 @@ module Console
60
63
  def write(data)
61
64
  if data.is_a?(Array)
62
65
  if data[0].is_a?(String) && (data.length == 0 || data[1].is_a?(Symbol))
63
- Console.puts *data
66
+ Console.puts(*data)
64
67
  else
65
68
  data.each do |chunk|
66
- Console.write *chunk
69
+ Console.write(*chunk)
67
70
  end
68
71
  Console.puts
69
72
  end
70
- else
73
+ elsif data
71
74
  Console.puts(data, @fg)
72
75
  end
73
76
  end
@@ -91,14 +94,15 @@ module Console
91
94
  level = case options.delete(:level)
92
95
  when String, Symbol then Log4r::LNAMES.index(options[:level].to_s.upcase)
93
96
  end
94
- STDOUT.puts level
95
97
 
96
- # Remove any existing console handler
97
98
  log = logger ? Log4r::Logger[logger] : Log4r::Logger.root
98
99
  log = Log4r::Logger.new(logger) unless log
99
100
 
100
- log.outputters.each do |o|
101
- log.remove(o.name) if h.is_a?(Log4r::StdoutOutputter)
101
+ # Remove any existing console handler
102
+ Log4r::Logger.each_logger do |l|
103
+ l.outputters.each do |o|
104
+ l.remove(o.name) if o.is_a?(Log4r::StdoutOutputter)
105
+ end
102
106
  end
103
107
 
104
108
  # Add a ColorConsoleHandler
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color-console
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.3.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Adam Gardiner
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-12-01 00:00:00.000000000 Z
12
+ date: 2015-05-28 00:00:00.000000000 Z
12
13
  dependencies: []
13
- description: |2
14
- ColorConsole supports cross-platform (ANSI and Windows) colored text output to the console.
15
- It also provides useful methods for building command-line interfaces that provide status
16
- messages and progress bars.
14
+ description: ! " ColorConsole supports cross-platform (ANSI and Windows) colored
15
+ text output to the console.\n It also provides useful methods for building
16
+ command-line interfaces that provide status\n messages and progress bars.\n"
17
17
  email: adam.b.gardiner@gmail.com
18
18
  executables: []
19
19
  extensions: []
@@ -34,26 +34,27 @@ files:
34
34
  - lib/color_console.rb
35
35
  homepage: https://github.com/agardiner/color-console
36
36
  licenses: []
37
- metadata: {}
38
37
  post_install_message:
39
38
  rdoc_options: []
40
39
  require_paths:
41
40
  - lib
42
41
  required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
43
  requirements:
44
- - - '>='
44
+ - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
48
49
  requirements:
49
- - - '>='
50
+ - - ! '>='
50
51
  - !ruby/object:Gem::Version
51
52
  version: '0'
52
53
  requirements: []
53
54
  rubyforge_project:
54
- rubygems_version: 2.0.14
55
+ rubygems_version: 1.8.21
55
56
  signing_key:
56
- specification_version: 4
57
+ specification_version: 3
57
58
  summary: ColorConsole is a cross-platform library for outputting colored text to the
58
59
  console
59
60
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: eac6c2a764d6f516366d6f5e8470d2ed3b63170d
4
- data.tar.gz: aa2c094cf2ad8961fda8bdc3a74c484e0752bab5
5
- SHA512:
6
- metadata.gz: 118c4ed399645b94a34d071314f3c0716048ca7044522421cb79b73c337992eab90f41e1326c4983c54d10f90c844fffef86982aa8508022b6ae77cc06b65b60
7
- data.tar.gz: 3d60e42deccc08d8e9a9b318e8832b93413410dd9dc7390992fc92bc6ac9cc45a838d258acef608aed02af18fd37457bd7c8730b64cc0201f5f090de35dc3193