Class: Beautiful::Log::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Includes:
Modules::CodeRangeExtractable, Modules::CompleteLogFormatter, Modules::ErrorFormattable, Modules::PathOmmittable, Modules::RenderLogFoematter, Modules::Stylable
Defined in:
lib/beautiful/log/formatter.rb

Constant Summary collapse

DEFAULT_STATUS_CODE_STYLES =
{ (1..3) => [:green, :bold], 'other' => [:red, :bold] }.freeze
DEFAULT_SEVERITY_STYLES =
{ FATAL: [:red, :swap], ERROR: :red, WARN: :light_red }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(only_project_code: true, shrink_bundle_path: true, backtrace_ignore_paths: [], highlighted_line_range: 3, highlighted_line_styles: :cyan, backtrace_styles: :light_red, error_file_path_styles: :red, status_code_styles: {}, severity_styles: {}, occurence_line: :light_blue) ⇒ Formatter

rubocop: disable Metrics/AbcSize, Style/ParameterLists, Style/MethodLength



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/beautiful/log/formatter.rb', line 23

def initialize(
  only_project_code: true,
  shrink_bundle_path: true,
  backtrace_ignore_paths: [],
  highlighted_line_range: 3,
  highlighted_line_styles: :cyan,
  backtrace_styles: :light_red,
  error_file_path_styles: :red,
  status_code_styles: {},
  severity_styles: {},
  occurence_line: :light_blue
)
  @only_project_code = only_project_code
  @shrink_bundle_path = shrink_bundle_path
  @ignore_paths = backtrace_ignore_paths.map do |path|
    Regexp.new "#{Rails.root}/#{path}"
  end << Regexp.new(bundle_path)
  @allow_path = Regexp.new bundle_install_path
  @backtrace_ignore_paths = backtrace_ignore_paths
  @highlighted_line_range = highlighted_line_range
  @highlighted_line_styles = highlighted_line_styles
  @backtrace_styles = backtrace_styles
  @error_file_path_styles = error_file_path_styles
  @status_code_styles = DEFAULT_STATUS_CODE_STYLES.merge(status_code_styles).with_indifferent_access
  @severity_styles = DEFAULT_SEVERITY_STYLES.merge(severity_styles).with_indifferent_access
  @occurence_line = occurence_line
end

Instance Attribute Details

#allow_pathObject (readonly)

Returns the value of attribute allow_path.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def allow_path
  @allow_path
end

#backtrace_ignore_pathsObject (readonly)

Returns the value of attribute backtrace_ignore_paths.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def backtrace_ignore_paths
  @backtrace_ignore_paths
end

#backtrace_stylesObject (readonly)

Returns the value of attribute backtrace_styles.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def backtrace_styles
  @backtrace_styles
end

#error_file_path_stylesObject (readonly)

Returns the value of attribute error_file_path_styles.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def error_file_path_styles
  @error_file_path_styles
end

#highlighted_line_rangeObject (readonly)

Returns the value of attribute highlighted_line_range.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def highlighted_line_range
  @highlighted_line_range
end

#highlighted_line_stylesObject (readonly)

Returns the value of attribute highlighted_line_styles.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def highlighted_line_styles
  @highlighted_line_styles
end

#ignore_pathsObject (readonly)

Returns the value of attribute ignore_paths.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def ignore_paths
  @ignore_paths
end

#only_project_codeObject (readonly)

Returns the value of attribute only_project_code.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def only_project_code
  @only_project_code
end

#severity_stylesObject (readonly)

Returns the value of attribute severity_styles.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def severity_styles
  @severity_styles
end

#status_code_stylesObject (readonly)

Returns the value of attribute status_code_styles.



9
10
11
# File 'lib/beautiful/log/formatter.rb', line 9

def status_code_styles
  @status_code_styles
end

Instance Method Details

#call(severity, timestamp, _progname, message) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/beautiful/log/formatter.rb', line 51

def call(severity, timestamp, _progname, message)
  problem_code = highlighted_code(message) if message.is_a?(Exception)
  header = message_header(timestamp, severity, caller)
  message = "#{header} -- : #{message_body(message, header.uncolorize.length + 6)}\n"
  message = "#{message}\n#{problem_code}" if problem_code.present?
  message = "\n#{message}\n" if %w(FATAL ERROR).include?(severity)
  message
end