Class: Vigilant::Rails::Logger

Inherits:
ActiveSupport::Logger
  • Object
show all
Includes:
ActiveSupport::LoggerSilence, ActiveSupport::LoggerThreadSafeLevel, LoggerSilence
Defined in:
lib/vigilant-ruby/rails/logger.rb

Overview

A wrapper that delegates Rails-style logging calls to Vigilant::Logger

Constant Summary collapse

SEVERITY_MAP =
{
  ::Logger::DEBUG => Vigilant::DEBUG,
  ::Logger::INFO => Vigilant::INFO,
  ::Logger::WARN => Vigilant::WARNING,
  ::Logger::ERROR => Vigilant::ERROR,
  ::Logger::FATAL => Vigilant::ERROR,
  ::Logger::UNKNOWN => Vigilant::ERROR
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: Vigilant.configuration.name, endpoint: Vigilant.configuration.endpoint, token: Vigilant.configuration.token, insecure: Vigilant.configuration.insecure, passthrough: Vigilant.configuration.passthrough) ⇒ Logger

Returns a new instance of Logger.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vigilant-ruby/rails/logger.rb', line 27

def initialize(name: Vigilant.configuration.name,
               endpoint: Vigilant.configuration.endpoint,
               token: Vigilant.configuration.token,
               insecure: Vigilant.configuration.insecure,
               passthrough: Vigilant.configuration.passthrough)
  super(nil)

  @vigilant_logger = Vigilant::Logger.new(
    endpoint: endpoint,
    token: token,
    name: name,
    insecure: insecure,
    passthrough: passthrough
  )

  at_exit { close }

  self.level = ::Logger::DEBUG
  @tags = []
  @extra_loggers = []
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



173
174
175
# File 'lib/vigilant-ruby/rails/logger.rb', line 173

def formatter
  @formatter
end

Instance Method Details

#add(severity, message_or_block = nil, progname = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vigilant-ruby/rails/logger.rb', line 107

def add(severity, message_or_block = nil, progname = nil)
  return true if severity < level

  msg =
    if message_or_block.respond_to?(:call)
      message_or_block.call.to_s
    else
      (message_or_block || progname).to_s
    end.strip

  vigilant_severity = SEVERITY_MAP.fetch(severity, Vigilant::ERROR)
  log_to_vigilant(vigilant_severity, msg)

  @extra_loggers.each { |logger| logger.add(severity, msg, progname) }
  true
end

#broadcast_to(*io_devices_and_loggers) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vigilant-ruby/rails/logger.rb', line 60

def broadcast_to(*io_devices_and_loggers)
  io_devices_and_loggers.each do |io_device_or_logger|
    extra_logger =
      if io_device_or_logger.is_a?(::Logger)
        io_device_or_logger
      else
        ::ActiveSupport::Logger.new(io_device_or_logger)
      end
    @extra_loggers << extra_logger
  end
end

#broadcastsObject



56
57
58
# File 'lib/vigilant-ruby/rails/logger.rb', line 56

def broadcasts
  [self] + @extra_loggers
end

#closeObject



161
162
163
# File 'lib/vigilant-ruby/rails/logger.rb', line 161

def close
  @vigilant_logger.shutdown if @vigilant_logger.respond_to?(:shutdown)
end

#current_tagsObject



139
140
141
# File 'lib/vigilant-ruby/rails/logger.rb', line 139

def current_tags
  @tags
end

#datetime_formatObject



169
170
171
# File 'lib/vigilant-ruby/rails/logger.rb', line 169

def datetime_format
  nil
end

#datetime_format=(_format) ⇒ Object



165
166
167
# File 'lib/vigilant-ruby/rails/logger.rb', line 165

def datetime_format=(_format)
  nil
end

#debug(progname = nil, &block) ⇒ Object



83
84
85
# File 'lib/vigilant-ruby/rails/logger.rb', line 83

def debug(progname = nil, &block)
  add(::Logger::DEBUG, block, progname)
end

#error(progname = nil, &block) ⇒ Object



95
96
97
# File 'lib/vigilant-ruby/rails/logger.rb', line 95

def error(progname = nil, &block)
  add(::Logger::ERROR, block, progname)
end

#fatal(progname = nil, &block) ⇒ Object



99
100
101
# File 'lib/vigilant-ruby/rails/logger.rb', line 99

def fatal(progname = nil, &block)
  add(::Logger::FATAL, block, progname)
end

#flushObject



151
152
153
154
155
# File 'lib/vigilant-ruby/rails/logger.rb', line 151

def flush
  @vigilant_logger.flush if @vigilant_logger.respond_to?(:flush)
rescue StandardError
  nil
end

#info(progname = nil, &block) ⇒ Object



87
88
89
# File 'lib/vigilant-ruby/rails/logger.rb', line 87

def info(progname = nil, &block)
  add(::Logger::INFO, block, progname)
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/vigilant-ruby/rails/logger.rb', line 49

def kind_of?(klass)
  return true if defined?(::ActiveSupport::BroadcastLogger) && klass == ::ActiveSupport::BroadcastLogger

  super(klass)
end

#pop_tags(amount = 1) ⇒ Object



135
136
137
# File 'lib/vigilant-ruby/rails/logger.rb', line 135

def pop_tags(amount = 1)
  @tags.pop(amount)
end

#push_tags(*tags) ⇒ Object



131
132
133
# File 'lib/vigilant-ruby/rails/logger.rb', line 131

def push_tags(*tags)
  @tags.concat(tags)
end

#reopen(_device = nil) ⇒ Object



157
158
159
# File 'lib/vigilant-ruby/rails/logger.rb', line 157

def reopen(_device = nil)
  nil
end

#silence(temporary_level = ::Logger::ERROR) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/vigilant-ruby/rails/logger.rb', line 143

def silence(temporary_level = ::Logger::ERROR)
  old_level = level
  self.level = temporary_level
  yield self
ensure
  self.level = old_level
end

#stop_broadcasting_to(io_device_or_logger) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/vigilant-ruby/rails/logger.rb', line 72

def stop_broadcasting_to(io_device_or_logger)
  if io_device_or_logger.is_a?(::Logger)
    @extra_loggers.delete(io_device_or_logger)
  else
    @extra_loggers.reject! do |logger|
      defined?(::ActiveSupport::Logger) &&
        ::ActiveSupport::Logger.logger_outputs_to?(logger, io_device_or_logger)
    end
  end
end

#tagged(*tags) ⇒ Object



124
125
126
127
128
129
# File 'lib/vigilant-ruby/rails/logger.rb', line 124

def tagged(*tags)
  push_tags(*tags)
  yield self
ensure
  pop_tags(tags.size)
end

#unknown(progname = nil, &block) ⇒ Object



103
104
105
# File 'lib/vigilant-ruby/rails/logger.rb', line 103

def unknown(progname = nil, &block)
  add(::Logger::UNKNOWN, block, progname)
end

#warn(progname = nil, &block) ⇒ Object



91
92
93
# File 'lib/vigilant-ruby/rails/logger.rb', line 91

def warn(progname = nil, &block)
  add(::Logger::WARN, block, progname)
end