Module: LazyGraph::Logger
- Defined in:
- lib/lazy_graph/logger.rb
Constant Summary collapse
- COLORIZED_LOGS =
!ENV['DISABLED_COLORIZED_LOGS'] && ENV.fetch('RACK_ENV', 'development') == 'development'
Class Attribute Summary collapse
-
.color_enabled ⇒ Object
Returns the value of attribute color_enabled.
-
.structured ⇒ Object
Returns the value of attribute structured.
Class Method Summary collapse
- .blue(text) ⇒ Object
- .bold(text) ⇒ Object
- .build_color_string(&blk) ⇒ Object
- .colorize(text, color_code) ⇒ Object
- .default_logger ⇒ Object
- .dim(text) ⇒ Object
- .green(text) ⇒ Object
- .light_gray(text) ⇒ Object
- .orange(text) ⇒ Object
- .red(text) ⇒ Object
- .yellow(text) ⇒ Object
Class Attribute Details
permalink .color_enabled ⇒ Object
Returns the value of attribute color_enabled.
15 16 17 |
# File 'lib/lazy_graph/logger.rb', line 15 def color_enabled @color_enabled end |
permalink .structured ⇒ Object
Returns the value of attribute structured.
15 16 17 |
# File 'lib/lazy_graph/logger.rb', line 15 def structured @structured end |
Class Method Details
permalink .blue(text) ⇒ Object
[View source]
65 66 67 |
# File 'lib/lazy_graph/logger.rb', line 65 def blue(text) colorize(text, 34) # Blue for info end |
permalink .bold(text) ⇒ Object
[View source]
77 78 79 |
# File 'lib/lazy_graph/logger.rb', line 77 def bold(text) colorize(text, 1) # Bold text end |
permalink .build_color_string(&blk) ⇒ Object
[View source]
43 44 45 46 47 |
# File 'lib/lazy_graph/logger.rb', line 43 def build_color_string(&blk) return unless block_given? instance_eval(&blk) end |
permalink .colorize(text, color_code) ⇒ Object
[View source]
49 50 51 |
# File 'lib/lazy_graph/logger.rb', line 49 def colorize(text, color_code) @color_enabled ? "\e[#{color_code}m#{text}\e[0m" : text end |
permalink .default_logger ⇒ Object
[View source]
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lazy_graph/logger.rb', line 23 def default_logger logger = ::Logger.new($stdout) self.color_enabled ||= Logger::COLORIZED_LOGS if self.color_enabled logger.formatter = proc do |severity, datetime, progname, | = "\e[90m[##{Process.pid}] #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N')}\e[0m" # Light gray timestamp "#{} \e[1m#{severity}\e[0m #{progname}: #{}\n" end elsif self.structured logger.formatter = proc do |severity, datetime, progname, | "#{{severity:, datetime:, progname:, **(.is_a?(Hash) ? : {message: }) }.to_json}\n" end else logger.formatter = proc do |severity, datetime, progname, | "[##{Process.pid}] #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N')} #{severity} #{progname}: #{}\n" end end logger end |
permalink .dim(text) ⇒ Object
[View source]
81 82 83 |
# File 'lib/lazy_graph/logger.rb', line 81 def dim(text) colorize(text, 2) # Italic text end |
permalink .green(text) ⇒ Object
[View source]
53 54 55 |
# File 'lib/lazy_graph/logger.rb', line 53 def green(text) colorize(text, 32) # Green for success end |
permalink .light_gray(text) ⇒ Object
[View source]
69 70 71 |
# File 'lib/lazy_graph/logger.rb', line 69 def light_gray(text) colorize(text, 90) # Light gray for faded text end |
permalink .orange(text) ⇒ Object
[View source]
73 74 75 |
# File 'lib/lazy_graph/logger.rb', line 73 def orange(text) colorize(text, '38;5;214') end |
permalink .red(text) ⇒ Object
[View source]
57 58 59 |
# File 'lib/lazy_graph/logger.rb', line 57 def red(text) colorize(text, 31) # Red for errors end |
permalink .yellow(text) ⇒ Object
[View source]
61 62 63 |
# File 'lib/lazy_graph/logger.rb', line 61 def yellow(text) colorize(text, 33) # Yellow for warnings or debug end |