Class: SemanticLogger::Formatters::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger/formatters/default.rb

Overview

Default non-colored text log output

Direct Known Subclasses

Color, OneLine, Pattern, Syslog

Constant Summary

Constants inherited from Base

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

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

build_time_format, #initialize, #pid, #time

Constructor Details

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

Instance Method Details

#call(log, logger) ⇒ Object

Default text log format Generates logs of the form:

2011-07-19 14:36:15.660235 D [1149:ScriptThreadProcess] Rails -- Hello World


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

def call(log, logger)
  self.log    = log
  self.logger = logger

  [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
end

#durationObject

Duration



51
52
53
# File 'lib/semantic_logger/formatters/default.rb', line 51

def duration
  "(#{log.duration_human})" if log.duration
end

#exceptionObject

Exception



74
75
76
# File 'lib/semantic_logger/formatters/default.rb', line 74

def exception
  "-- Exception: #{log.exception.class}: #{escape_control_characters(log.exception.message)}\n#{log.backtrace_to_s}" if log.exception
end

#file_name_and_lineObject

Ruby file name and line number that logged the message.



19
20
21
22
# File 'lib/semantic_logger/formatters/default.rb', line 19

def file_name_and_line
  file, line = log.file_name_and_line(true)
  "#{file}:#{line}" if file
end

#levelObject

Log level



9
10
11
# File 'lib/semantic_logger/formatters/default.rb', line 9

def level
  log.level_to_s
end

#messageObject

Log message



61
62
63
# File 'lib/semantic_logger/formatters/default.rb', line 61

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

#nameObject

Class / app name



56
57
58
# File 'lib/semantic_logger/formatters/default.rb', line 56

def name
  log.name
end

#named_tagsObject

Named Tags



41
42
43
44
45
46
47
48
# File 'lib/semantic_logger/formatters/default.rb', line 41

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

  list = []
  named_tags.each_pair { |name, value| list << "#{escape_control_characters(name)}: #{escape_control_characters(value)}" }
  "{#{list.join(', ')}}"
end

#payloadObject

Payload



66
67
68
69
70
71
# File 'lib/semantic_logger/formatters/default.rb', line 66

def payload
  pl = log.payload_to_s
  return unless pl

  "-- #{pl}"
end

#process_infoObject

Returns [String] the available process info Example:

[18934:thread_name test_logging.rb:51]


27
28
29
30
31
# File 'lib/semantic_logger/formatters/default.rb', line 27

def process_info
  process_id = "#{pid}:" if pid
  fname      = file_name_and_line
  fname ? "[#{process_id}#{thread_name} #{fname}]" : "[#{process_id}#{thread_name}]"
end

#tagsObject

Tags



34
35
36
37
38
# File 'lib/semantic_logger/formatters/default.rb', line 34

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

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

#thread_nameObject

Name of the thread that logged the message.



14
15
16
# File 'lib/semantic_logger/formatters/default.rb', line 14

def thread_name
  format("%.30s", log.thread_name)
end