Class: Formatter
- Inherits:
-
Object
- Object
- Formatter
- Defined in:
- lib/federation_logger.rb
Constant Summary collapse
- COLORS =
{ 'FG' => { 'black' => '30', 'red' => '31', 'green' => '32', 'yellow' => '33', 'blue' => '34', 'violet' => '35', 'cyan' => '36', 'white' => '37', }, 'BG' => { 'black' => '40', 'red' => '41', 'green' => '42', 'yellow' => '43', 'blue' => '44', 'violet' => '45', 'cyan' => '46', 'white' => '47', } }
- DULL =
'0'
- BRIGHT =
'1'
- NULL =
'00'
- ESC =
"\e"
- RESET =
"#{ESC}[00;0;00m"
- SEVERITY_TO_TAG_MAP =
example: 033[40;0;37m white text on black background
{'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'}
- SEVERITY_TO_COLOR_MAP =
{'DEBUG'=>'white', 'INFO'=>'green', 'WARN'=>'yellow', 'ERROR'=>'red', 'FATAL'=>'red', 'UNKNOWN'=>'white'}
Instance Method Summary collapse
- #bg(name) ⇒ Object
- #call(severity, time, progname, msg) ⇒ Object
- #colorize(message, c_fg = 'white', c_bg = 'black', strong = 0) ⇒ Object
- #colors? ⇒ Boolean
- #fg(name) ⇒ Object
-
#initialize ⇒ Formatter
constructor
A new instance of Formatter.
- #pretty_message(msg) ⇒ Object
- #pretty_prefix(severity, time) ⇒ Object
- #random ⇒ Object
Constructor Details
#initialize ⇒ Formatter
44 45 46 |
# File 'lib/federation_logger.rb', line 44 def initialize @colors_enabled = true end |
Instance Method Details
#bg(name) ⇒ Object
60 61 62 |
# File 'lib/federation_logger.rb', line 60 def bg name COLORS['BG'][name] end |
#call(severity, time, progname, msg) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/federation_logger.rb', line 72 def call(severity, time, progname, msg) fmt_prefix = pretty_prefix(severity, time) fmt_msg = (msg) "#{fmt_prefix} #{fmt_msg}\n" end |
#colorize(message, c_fg = 'white', c_bg = 'black', strong = 0) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/federation_logger.rb', line 64 def colorize(, c_fg='white', c_bg='black', strong=0) if colors? "#{ESC}[#{fg(c_fg)};#{bg(c_bg)};#{strong==0 ? DULL : BRIGHT}m#{message}#{RESET}" else end end |
#colors? ⇒ Boolean
52 53 54 |
# File 'lib/federation_logger.rb', line 52 def colors? @colors_enabled end |
#fg(name) ⇒ Object
56 57 58 |
# File 'lib/federation_logger.rb', line 56 def fg name COLORS['FG'][name] end |
#pretty_message(msg) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/federation_logger.rb', line 88 def msg w = 130 txt_w = (w - Rails.env.size - 3) # get the prefix without colors, just for the length @colors_enabled = false prefix = pretty_prefix("DEBUG", Time.now).size + 1 @colors_enabled = true # restore value if (msg.size <= (w-prefix)) msg else output = msg.strip.scan(/.{1,#{txt_w}}/).flatten.map { |line| sprintf("%#{w}s", sprintf("%-#{txt_w}s", line)) }.join("\n") "\n#{output}" end end |
#pretty_prefix(severity, time) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/federation_logger.rb', line 79 def pretty_prefix(severity, time) color = SEVERITY_TO_COLOR_MAP[severity] fmt_severity = colorize(sprintf("%-3s","#{SEVERITY_TO_TAG_MAP[severity]}"), color, 'black', 1) fmt_time = colorize(time.strftime("%s.%L")) fmt_env = colorize(Rails.env, random, 'black', 1) "#{fmt_env} - #{fmt_time} [#{fmt_severity}] (pid:#{$$})" end |
#random ⇒ Object
48 49 50 |
# File 'lib/federation_logger.rb', line 48 def random @random ||= COLORS['FG'].keys[3] end |