Class: Appsignal::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/appsignal/logger.rb

Overview

Logger that flushes logs to the AppSignal logging service.

Constant Summary collapse

PLAINTEXT =
0
LOGFMT =
1
JSON =
2
SEVERITY_MAP =
{
  DEBUG => 2,
  INFO => 3,
  WARN => 5,
  ERROR => 6,
  FATAL => 7
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, level: INFO, format: PLAINTEXT, attributes: {}) ⇒ void

Create a new logger instance

Parameters:

  • group

    Name of the group for this logger.

  • level (defaults to: INFO)

    Minimum log level to report. Log lines below this level will be ignored.

  • format (defaults to: PLAINTEXT)

    Format to use to parse log line attributes.

  • attributes (defaults to: {})

    Default attributes for all log lines.

Raises:

  • (TypeError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/appsignal/logger.rb', line 32

def initialize(group, level: INFO, format: PLAINTEXT, attributes: {})
  raise TypeError, "group must be a string" unless group.is_a? String

  @group = group
  @level = level
  @format = format
  @mutex = Mutex.new
  @default_attributes = attributes
  @appsignal_attributes = {}
  @loggers = []
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



23
24
25
# File 'lib/appsignal/logger.rb', line 23

def level
  @level
end

Instance Method Details

#add(severity, message = nil, group = nil) ⇒ Object Also known as: log

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

We support the various methods in the Ruby logger class by supplying this method.



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
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/appsignal/logger.rb', line 47

def add(severity, message = nil, group = nil)
  severity ||= UNKNOWN
  return true if severity < level

  group = @group if group.nil?
  if message.nil?
    if block_given?
      message = yield
    else
      message = group
      group = @group
    end
  end
  return if message.nil?

  message = formatter.call(severity, Time.now, group, message) if formatter

  @loggers.each do |logger|
    logger.add(severity, message, group)
  rescue
    nil
  end

  unless message.is_a?(String)
    Appsignal.internal_logger.warn(
      "Logger message was ignored, because it was not a String: #{message.inspect}"
    )
    return
  end

  Appsignal::Extension.log(
    group,
    SEVERITY_MAP.fetch(severity, 0),
    @format,
    message,
    Appsignal::Utils::Data.generate(appsignal_attributes)
  )
end

#broadcast_to(logger) ⇒ Object



169
170
171
# File 'lib/appsignal/logger.rb', line 169

def broadcast_to(logger)
  @loggers << logger
end

#debug(message = nil, attributes = {}) ⇒ void

This method returns an undefined value.

Log a debug level message

Parameters:

  • message (defaults to: nil)

    Message to log

  • attributes (defaults to: {})

    Attributes to tag the log with



91
92
93
94
95
96
97
98
# File 'lib/appsignal/logger.rb', line 91

def debug(message = nil, attributes = {})
  return if level > DEBUG

  message = yield if message.nil? && block_given?
  return if message.nil?

  add_with_attributes(DEBUG, message, @group, attributes)
end

#error(message = nil, attributes = {}) ⇒ void

This method returns an undefined value.

Log an error level message

Parameters:

  • message (defaults to: nil)

    Message to log

  • attributes (defaults to: {})

    Attributes to tag the log with



130
131
132
133
134
135
136
137
138
139
# File 'lib/appsignal/logger.rb', line 130

def error(message = nil, attributes = {})
  return if level > ERROR

  message = yield if message.nil? && block_given?
  return if message.nil?

  message = "#{message.class}: #{message.message}" if message.is_a?(Exception)

  add_with_attributes(ERROR, message, @group, attributes)
end

#fatal(message = nil, attributes = {}) ⇒ void

This method returns an undefined value.

Log a fatal level message

Parameters:

  • message (defaults to: nil)

    Message to log

  • attributes (defaults to: {})

    Attributes to tag the log with



145
146
147
148
149
150
151
152
# File 'lib/appsignal/logger.rb', line 145

def fatal(message = nil, attributes = {})
  return if level > FATAL

  message = yield if message.nil? && block_given?
  return if message.nil?

  add_with_attributes(FATAL, message, @group, attributes)
end

#info(message = nil, attributes = {}) ⇒ void

This method returns an undefined value.

Log an info level message

Parameters:

  • message (defaults to: nil)

    Message to log

  • attributes (defaults to: {})

    Attributes to tag the log with



104
105
106
107
108
109
110
111
# File 'lib/appsignal/logger.rb', line 104

def info(message = nil, attributes = {})
  return if level > INFO

  message = yield if message.nil? && block_given?
  return if message.nil?

  add_with_attributes(INFO, message, @group, attributes)
end

#silence(severity = ERROR, &block) ⇒ Object

When using ActiveSupport::TaggedLogging without the broadcast feature, the passed logger is required to respond to the ‘silence` method.

Reference links:



161
162
163
164
165
166
167
# File 'lib/appsignal/logger.rb', line 161

def silence(severity = ERROR, &block)
  previous_level = @level
  @level = severity
  block.call(self)
ensure
  @level = previous_level
end

#warn(message = nil, attributes = {}) ⇒ void

This method returns an undefined value.

Log a warn level message

Parameters:

  • message (defaults to: nil)

    Message to log

  • attributes (defaults to: {})

    Attributes to tag the log with



117
118
119
120
121
122
123
124
# File 'lib/appsignal/logger.rb', line 117

def warn(message = nil, attributes = {})
  return if level > WARN

  message = yield if message.nil? && block_given?
  return if message.nil?

  add_with_attributes(WARN, message, @group, attributes)
end