Module: PrettyLogger

Included in:
RequestLogger
Defined in:
lib/pretty_logger.rb,
lib/pretty_logger/railtie.rb,
lib/pretty_logger/version.rb,
lib/pretty_logger/pretty_logger.rb

Defined Under Namespace

Modules: ControllerMethods Classes: Error, Railtie, RequestLogger

Constant Summary collapse

VERSION =
"1.4.7"
COLORS =
{
  debug: "\e[90m",
  info: "\e[96m",
  warn: "\e[93m",
  error: "\e[31m",
}

Class Method Summary collapse

Class Method Details

.ansi_colorsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pretty_logger/pretty_logger.rb', line 96

def ansi_colors
  # timestamp: `light_black`
  # url:        `white`
  # hashkeys:   `cyan`
  # strings:    `green`
  # light_ colors don't work for bg in tmux
  {
    black:         30,
    light_black:   90,
    red:           31,
    light_red:     91,
    green:         32,
    light_green:   92,
    yellow:        33,
    light_yellow:  93,
    blue:          34,
    light_blue:    94,
    magenta:       35,
    light_magenta: 95,
    cyan:          36,
    light_cyan:    96,
    white:         37,
    light_white:   97,
  }
end

.clObject



58
59
60
# File 'lib/pretty_logger/pretty_logger.rb', line 58

def cl
  "\033[0m"
end

.clean_message(message) ⇒ Object



131
132
133
134
135
# File 'lib/pretty_logger/pretty_logger.rb', line 131

def clean_message(message)
  message.gsub(/\#\<([\w\:]+)( id: \d+)?.*?\>\n/im) { |found|
    "#<#{Regexp.last_match(1)}#{Regexp.last_match(2)}>\n"
  }
end

.colorize(name, text) ⇒ Object



66
67
68
69
# File 'lib/pretty_logger/pretty_logger.rb', line 66

def colorize(name, text)
  return "" if text.blank?
  "#{colors[name]}#{text}#{cl}"
end

.colorsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pretty_logger/pretty_logger.rb', line 71

def colors
  {
    black:   rgb(0, 0, 0),
    white:   rgb(255, 255, 255),
    lime:    rgb(0, 255, 0),
    red:     rgb(255, 0, 0),
    blue:    rgb(0, 0, 255),
    yellow:  rgb(255, 255, 0),
    cyan:    rgb(0, 255, 255),
    magenta: rgb(255, 0, 255),
    gold:    rgb(218, 165, 32),
    silver:  rgb(192, 192, 192),
    grey:    rgb(150, 150, 150),
    maroon:  rgb(128, 0, 0),
    olive:   rgb(128, 128, 0),
    green:   rgb(0, 128, 0),
    purple:  rgb(128, 0, 128),
    teal:    rgb(0, 128, 128),
    navy:    rgb(0, 0, 128),
    rocco:   rgb(1, 96, 255),
    orange:  rgb(255, 150, 0),
    pink:    rgb(255, 150, 150),
  }
end

.debug(*messages) ⇒ Object



42
43
44
# File 'lib/pretty_logger/pretty_logger.rb', line 42

def debug(*messages)
  pretty_log(:debug, *messages)
end

.error(*messages) ⇒ Object



54
55
56
# File 'lib/pretty_logger/pretty_logger.rb', line 54

def error(*messages)
  pretty_log(:error, *messages)
end

.focused_backtrace(trace) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/pretty_logger/pretty_logger.rb', line 122

def focused_backtrace(trace)
  return [] unless trace
  trace.select { |line|
    line.include?("/app/")
  }.map { |line|
    line.gsub(/^.*?#{Rails.root}/, "").gsub(/(app)?\/app\//, "app/").gsub(":in `", " `").gsub(/(:\d+) .*?$/, '\1')
  }
end

.info(*messages) ⇒ Object



46
47
48
# File 'lib/pretty_logger/pretty_logger.rb', line 46

def info(*messages)
  pretty_log(nil, *messages) # nil makes it so `info` level doesn't show a prefix
end

.instanceObject



11
12
13
# File 'lib/pretty_logger/pretty_logger.rb', line 11

def instance
  @instance ||= ::ActiveSupport::Logger.new("log/custom.log")
end

.log(*messages) ⇒ Object



38
39
40
# File 'lib/pretty_logger/pretty_logger.rb', line 38

def log(*messages)
  pretty_log(nil, *messages)
end

.pretty_log(level, *messages) ⇒ Object



32
33
34
35
36
# File 'lib/pretty_logger/pretty_logger.rb', line 32

def pretty_log(level, *messages)
  message = messages.compact.map { |m| pretty_message(m) }.join("\n")
  level_indicator = level.presence && "#{COLORS[level]}[#{level.to_s.upcase}]"
  instance.send(level || :info, "\e[90m#{timestamp}#{level_indicator}\e[0m#{message}")
end

.pretty_message(obj) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pretty_logger/pretty_logger.rb', line 19

def pretty_message(obj)
  return obj if obj.is_a?(::String)

  ::CodeRay.scan(obj, :ruby).terminal.gsub(
    /\e\[36m:(\w+)\e\[0m=>/i, ("\e[36m" + '\1: ' + "\e[0m") # hashrocket(sym) to colon(sym)
  ).gsub(
    /\e\[0m=>/, "\e[0m: " # all hashrockets to colons
  ).gsub(
    # grey out nils
    "\e[1;36mnil\e[0m", "\e[1;90mnil\e[0m"
  )
end

.rgb(r, g, b) ⇒ Object



62
63
64
# File 'lib/pretty_logger/pretty_logger.rb', line 62

def rgb(r, g, b)
  "\033[38;2;#{r};#{g};#{b}m"
end

.timestampObject



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

def timestamp
  Time.current.in_time_zone("Mountain Time (US & Canada)").strftime("[%b %d, %I:%M:%S%P]")
end

.truncate(input, max_visible_length = 2000, with: "...") ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pretty_logger/pretty_logger.rb', line 137

def truncate(input, max_visible_length=2000, with: "...")
  full = input.length
  return input if full <= max_visible_length

  clean = input.gsub(/\e\[[\d;]*[a-z]/i, "").length
  return input if clean <= max_visible_length

  max_visible_length -= with.gsub(/\e\[[\d;]*[a-z]/i, "").length

  visible_length = 0
  truncated_string = ""

  input.scan(/(\e\[[\d;]*[a-z]|[^\e]+)/i) do |match|
    part = match[0]
    if part.match?(/\e\[[\d;]*[a-z]/i) # Match ANSI escape sequences
      truncated_string += part # Do not add to the count
    else
      part.each_char do |char|
        if visible_length < max_visible_length
          truncated_string += char
          visible_length += 1
        else
          break
        end
      end
    end
  end

  "#{truncated_string}#{with}"
end

.warn(*messages) ⇒ Object



50
51
52
# File 'lib/pretty_logger/pretty_logger.rb', line 50

def warn(*messages)
  pretty_log(:warn, *messages)
end