Class: SemanticLogger::Formatters::OneLine

Inherits:
Default
  • Object
show all
Defined in:
lib/semantic_logger/formatters/one_line.rb

Overview

Only output one line for each log entry.

Notes:

  • Line breaks are stripped from every text field: the name, thread name, file name, tags, named tags, message, payload and exception message. They are new lines, carriage returns, vertical tabs and form feeds (which most terminals move down a line for), and the Unicode next line (U+0085), line separator (U+2028), and paragraph separator (U+2029), at which editors, browsers, and some log viewers also start a new line. With escape_control_chars: true they are escaped instead, the Unicode ones in the \uXXXX form, and so are the other control characters, as the other text formatters escape them: the ASCII ones, and the Unicode ones that control how text is shown, such as the bidirectional controls (see Base::UNICODE_CONTROL_CHARS).
  • Exceptions only include the class and message, the stack trace is not shown.
  • Exception details, when enabled, are joined onto the same line.

Constant Summary collapse

UNICODE_LINE_BREAKS =

The line breaks that Unicode adds to the line feed and carriage return: next line, line separator, and paragraph separator.

"\u0085\u2028\u2029".freeze
ASCII_LINE_BREAKS =

The ASCII line breaks: line feed, carriage return, vertical tab, and form feed, at which most terminals move down a line, as a String#delete character set.

"\r\n\v\f".freeze
LINE_BREAKS =

Every line break, as a String#delete character set.

"#{ASCII_LINE_BREAKS}#{UNICODE_LINE_BREAKS}".freeze
DETAILS_LINE_BREAK =

A line break, with the white space around it, between two lines of exception details. Any of LINE_BREAKS counts, so that one on its own, such as a form feed, still leaves a space.

/\s*[#{LINE_BREAKS}]\s*/

Constants inherited from Base

Base::CONTROL_CHARS, Base::CONTROL_CHAR_ESCAPES, Base::PRECISION, Base::UNICODE_CONTROL_CHARS

Instance Attribute Summary

Attributes inherited from Base

#backtrace_cleaner, #escape_control_chars, #log, #log_application, #log_environment, #log_exception_details, #log_host, #logger, #precision, #time_format

Instance Method Summary collapse

Methods inherited from Default

#call, #duration, #level, #process_info

Methods inherited from Base

build_time_format, #initialize, #pid, #time

Constructor Details

This class inherits a constructor from SemanticLogger::Formatters::Base

Instance Method Details

#exception ⇒ Object



80
81
82
83
84
85
86
# File 'lib/semantic_logger/formatters/one_line.rb', line 80

def exception
  return unless log.exception

  message = one_line(Utils.exception_message(log.exception))
  details = exception_details
  "-- Exception: #{Utils.exception_name(log.exception)}: #{message}#{" -- #{details}" if details}"
end

#exception_details ⇒ Object

The exception details, with each line trimmed and joined by a space.



89
90
91
92
93
94
# File 'lib/semantic_logger/formatters/one_line.rb', line 89

def exception_details
  details = exception_details_for(log.exception) if log.exception
  return unless details

  one_line(Utils.encode_utf8_string(details).split(DETAILS_LINE_BREAK).reject(&:empty?).join(" ").strip)
end

#file_name_and_line ⇒ Object

A log call can supply the backtrace that the file name and method come from.



58
59
60
61
62
63
64
# File 'lib/semantic_logger/formatters/one_line.rb', line 58

def file_name_and_line
  file, line = log.file_name_and_line(true)
  return unless file

  label = method_name
  label ? "#{one_line(file)}:#{line} #{label}" : "#{one_line(file)}:#{line}"
end

#message ⇒ Object



71
72
73
# File 'lib/semantic_logger/formatters/one_line.rb', line 71

def message
  "-- #{one_line(log.message)}" if log.message
end

#method_name ⇒ Object



66
67
68
69
# File 'lib/semantic_logger/formatters/one_line.rb', line 66

def method_name
  label = log.call_site&.label
  one_line(label) if label
end

#name ⇒ Object



53
54
55
# File 'lib/semantic_logger/formatters/one_line.rb', line 53

def name
  one_line(log.name) if log.name
end

#named_tags ⇒ Object



45
46
47
48
49
50
51
# File 'lib/semantic_logger/formatters/one_line.rb', line 45

def named_tags
  named_tags = log.named_tags
  return if named_tags.nil? || named_tags.empty?

  list = named_tags.map { |name, value| "#{one_line(name)}: #{one_line(value)}" }
  "{#{list.join(', ')}}"
end

#payload ⇒ Object



75
76
77
78
# File 'lib/semantic_logger/formatters/one_line.rb', line 75

def payload
  pl = log.payload_to_s
  "-- #{one_line(pl)}" if pl
end

#tags ⇒ Object



39
40
41
42
43
# File 'lib/semantic_logger/formatters/one_line.rb', line 39

def tags
  return if log.tags.nil? || log.tags.empty?

  "[#{log.tags.map { |tag| one_line(tag) }.join('] [')}]"
end

#thread_name ⇒ Object

Shortened before its line breaks are removed or escaped, as the other text formatters do (see Default#shortened_thread_name), so that an escape is never cut in two.



35
36
37
# File 'lib/semantic_logger/formatters/one_line.rb', line 35

def thread_name
  one_line(shortened_thread_name)
end