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

Class Method Summary collapse

Class Attribute Details

.color_enabledObject

Returns the value of attribute color_enabled.


15
16
17
# File 'lib/lazy_graph/logger.rb', line 15

def color_enabled
  @color_enabled
end

.structuredObject

Returns the value of attribute structured.


15
16
17
# File 'lib/lazy_graph/logger.rb', line 15

def structured
  @structured
end

Class Method Details

.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

.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

.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

.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

.default_loggerObject

[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, message|
      light_gray_timestamp = "\e[90m[##{Process.pid}] #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N')}\e[0m" # Light gray timestamp
      "#{light_gray_timestamp} \e[1m#{severity}\e[0m #{progname}: #{message}\n"
    end
  elsif self.structured
    logger.formatter = proc do |severity, datetime, progname, message|
      "#{{severity:, datetime:, progname:, **(message.is_a?(Hash) ? message : {message: }) }.to_json}\n"
    end
  else
    logger.formatter = proc do |severity, datetime, progname, message|
      "[##{Process.pid}] #{datetime.strftime('%Y-%m-%dT%H:%M:%S.%6N')} #{severity} #{progname}: #{message}\n"
    end
  end
  logger
end

.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

.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

.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

.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

.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

.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