Class: Lumberjack::LocalLogTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/local_log_template.rb

Overview

This is a log template designed for local environments. It provides a simple, human-readable format that includes key information about log entries while omitting extraneous details. The template can be configured to include or exclude certain components such as the times, process ID, program name, and attributes.

It is registered with the TemplateRegistry as :local.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LocalLogTemplate

Create a new LocalLogTemplate instance.

Parameters:

  • options (Hash) (defaults to: {})

    Options for configuring the template.

Options Hash (options):

  • :exclude_attributes (Boolean, Array<String>, nil)

    If true, all attributes are excluded. If an array of strings is provided, those attributes (and their sub-attributes) are excluded. Defaults to nil (include all attributes).

  • :exclude_progname (Boolean)

    If true, the progname is excluded. Defaults to false.

  • :exclude_pid (Boolean)

    If true, the process ID is excluded. Defaults to true.

  • :exclude_time (Boolean)

    If true, the time is excluded. Defaults to true.

  • :colorize (Boolean)

    If true, colorize the output based on severity. Defaults to false.

  • :exception_formatter (Symbol, Formatter, #call)

    The formatter to use for exceptions in messages. Can be a symbol registered in the FormatterRegistry, a Formatter instance, or any object that responds to #call. Defaults to nil (use default exception formatting). If the logger does not have an exception formatter configured, then the device will use this to format exceptions.

  • :severity_format (String, Symbol)

    The optional format for severity labels (padded, char, emoji).



31
32
33
34
35
36
37
38
39
# File 'lib/lumberjack/local_log_template.rb', line 31

def initialize(options = {})
  self.exclude_progname = options.fetch(:exclude_progname, false)
  self.exclude_pid = options.fetch(:exclude_pid, true)
  self.exclude_time = options.fetch(:exclude_time, true)
  self.exclude_attributes = options.fetch(:exclude_attributes, nil)
  self.colorize = options.fetch(:colorize, false)
  self.severity_format = options.fetch(:severity_format, nil)
  self.exception_formatter = options.fetch(:exception_formatter, :exception)
end

Instance Attribute Details

#exception_formatter#call?

Return the exception formatter.

Returns:



189
190
191
# File 'lib/lumberjack/local_log_template.rb', line 189

def exception_formatter
  @exception_formatter
end

#severity_formatString

Return the current severity format.

Returns:

  • (String)


175
176
177
# File 'lib/lumberjack/local_log_template.rb', line 175

def severity_format
  @severity_format
end

Instance Method Details

#call(entry) ⇒ String

Format a log entry according to the template.

Parameters:

  • entry (LogEntry)

    The log entry to format.

Returns:

  • (String)

    The formatted log entry.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lumberjack/local_log_template.rb', line 45

def call(entry)
  message = entry.message
  if message.is_a?(Exception) && exception_formatter
    message = exception_formatter.call(message)
  end

  formatted = +""
  formatted << entry.time.strftime("%Y-%m-%d %H:%M:%S.%6N ") unless exclude_time?
  formatted << "#{severity_label(entry)} #{message}"
  formatted << "#{Lumberjack::LINE_SEPARATOR}    progname: #{entry.progname}" if entry.progname.to_s != "" && !exclude_progname?
  formatted << "#{Lumberjack::LINE_SEPARATOR}    pid: #{entry.pid}" unless exclude_pid?

  if entry.attributes && !entry.attributes.empty? && !exclude_attributes?
    Lumberjack::Utils.flatten_attributes(entry.attributes).to_a.sort_by(&:first).each do |name, value|
      next if @attribute_filter.any? do |filter_name|
        if name.start_with?(filter_name)
          next_char = name[filter_name.length]
          next_char.nil? || next_char == "."
        end
      end

      formatted << "#{Lumberjack::LINE_SEPARATOR}    #{name}: #{value}"
    end
  end

  formatted = Template.colorize_entry(formatted, entry) if colorize?
  formatted << Lumberjack::LINE_SEPARATOR
end

#colorize=(value) ⇒ void

This method returns an undefined value.

Set whether to enable colorization.

Parameters:

  • value (Boolean)


160
161
162
# File 'lib/lumberjack/local_log_template.rb', line 160

def colorize=(value)
  @colorize = !!value
end

#colorize?Boolean

Return true if colorization is enabled, false otherwise.

Returns:

  • (Boolean)


152
153
154
# File 'lib/lumberjack/local_log_template.rb', line 152

def colorize?
  @colorize
end

#exclude_attributes=(value) ⇒ void

This method returns an undefined value.

Set the attributes to exclude. If set to true, all attributes are excluded. If set to an array of strings, those attributes (and their sub-attributes) are excluded. If set to false or nil, no attributes are excluded.

Parameters:

  • value (Boolean, Array<String>, nil)


94
95
96
97
98
99
100
101
102
# File 'lib/lumberjack/local_log_template.rb', line 94

def exclude_attributes=(value)
  @exclude_attributes = false
  @attribute_filter = []
  if value == true
    @exclude_attributes = true
  elsif value
    @attribute_filter = Array(value).map(&:to_s)
  end
end

#exclude_attributes?Boolean

Return true if all attributes are excluded, false otherwise.

Returns:

  • (Boolean)


77
78
79
# File 'lib/lumberjack/local_log_template.rb', line 77

def exclude_attributes?
  @exclude_attributes
end

#exclude_pid=(value) ⇒ void

This method returns an undefined value.

Set whether to exclude the pid.

Parameters:

  • value (Boolean)


130
131
132
# File 'lib/lumberjack/local_log_template.rb', line 130

def exclude_pid=(value)
  @exclude_pid = !!value
end

#exclude_pid?Boolean

Return true if the pid is excluded, false otherwise.

Returns:

  • (Boolean)


122
123
124
# File 'lib/lumberjack/local_log_template.rb', line 122

def exclude_pid?
  @exclude_pid
end

#exclude_progname=(value) ⇒ void

This method returns an undefined value.

Set whether to exclude the progname.

Parameters:

  • value (Boolean)


115
116
117
# File 'lib/lumberjack/local_log_template.rb', line 115

def exclude_progname=(value)
  @exclude_progname = !!value
end

#exclude_progname?Boolean

Return true if the progname is excluded, false otherwise.

Returns:

  • (Boolean)


107
108
109
# File 'lib/lumberjack/local_log_template.rb', line 107

def exclude_progname?
  @exclude_progname
end

#exclude_time=(value) ⇒ void

This method returns an undefined value.

Set whether to exclude the time.

Parameters:

  • value (Boolean)


145
146
147
# File 'lib/lumberjack/local_log_template.rb', line 145

def exclude_time=(value)
  @exclude_time = !!value
end

#exclude_time?Boolean

Return true if the time is excluded, false otherwise.

Returns:

  • (Boolean)


137
138
139
# File 'lib/lumberjack/local_log_template.rb', line 137

def exclude_time?
  @exclude_time
end

#excluded_attributesArray<String>

Return the list of excluded attribute names.

Returns:

  • (Array<String>)


84
85
86
# File 'lib/lumberjack/local_log_template.rb', line 84

def excluded_attributes
  @attribute_filter.dup
end