Class: Lumberjack::LocalLogTemplate
- Inherits:
-
Object
- Object
- Lumberjack::LocalLogTemplate
- 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.
Instance Attribute Summary collapse
-
#exception_formatter ⇒ #call?
Return the exception formatter.
-
#severity_format ⇒ String
Return the current severity format.
Instance Method Summary collapse
-
#call(entry) ⇒ String
Format a log entry according to the template.
-
#colorize=(value) ⇒ void
Set whether to enable colorization.
-
#colorize? ⇒ Boolean
Return true if colorization is enabled, false otherwise.
-
#exclude_attributes=(value) ⇒ void
Set the attributes to exclude.
-
#exclude_attributes? ⇒ Boolean
Return true if all attributes are excluded, false otherwise.
-
#exclude_pid=(value) ⇒ void
Set whether to exclude the pid.
-
#exclude_pid? ⇒ Boolean
Return true if the pid is excluded, false otherwise.
-
#exclude_progname=(value) ⇒ void
Set whether to exclude the progname.
-
#exclude_progname? ⇒ Boolean
Return true if the progname is excluded, false otherwise.
-
#exclude_time=(value) ⇒ void
Set whether to exclude the time.
-
#exclude_time? ⇒ Boolean
Return true if the time is excluded, false otherwise.
-
#excluded_attributes ⇒ Array<String>
Return the list of excluded attribute names.
-
#initialize(options = {}) ⇒ LocalLogTemplate
constructor
Create a new LocalLogTemplate instance.
Constructor Details
#initialize(options = {}) ⇒ LocalLogTemplate
Create a new LocalLogTemplate instance.
31 32 33 34 35 36 37 38 39 |
# File 'lib/lumberjack/local_log_template.rb', line 31 def initialize( = {}) self.exclude_progname = .fetch(:exclude_progname, false) self.exclude_pid = .fetch(:exclude_pid, true) self.exclude_time = .fetch(:exclude_time, true) self.exclude_attributes = .fetch(:exclude_attributes, nil) self.colorize = .fetch(:colorize, false) self.severity_format = .fetch(:severity_format, nil) self.exception_formatter = .fetch(:exception_formatter, :exception) end |
Instance Attribute Details
#exception_formatter ⇒ #call?
Return the exception formatter.
189 190 191 |
# File 'lib/lumberjack/local_log_template.rb', line 189 def exception_formatter @exception_formatter end |
#severity_format ⇒ String
Return the current severity format.
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.
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) = entry. if .is_a?(Exception) && exception_formatter = exception_formatter.call() 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
137 138 139 |
# File 'lib/lumberjack/local_log_template.rb', line 137 def exclude_time? @exclude_time end |
#excluded_attributes ⇒ Array<String>
Return the list of excluded attribute names.
84 85 86 |
# File 'lib/lumberjack/local_log_template.rb', line 84 def excluded_attributes @attribute_filter.dup end |