Class: Labkit::Logging::JsonLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/labkit/logging/json_logger.rb

Constant Summary collapse

RESERVED_LOG_KEYS =

We should also reject log keys coming from Labkit::Context, but we cannot do this without breaking clients currently. This is tracked in gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/issues/35

[
  :environment,
  :host,
  :shard,
  :stage,
  :subcomponent,
  :tier,
  :type,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, level: JsonLogger.log_level) ⇒ JsonLogger

Returns a new instance of JsonLogger.



26
27
28
# File 'lib/labkit/logging/json_logger.rb', line 26

def initialize(path, level: JsonLogger.log_level)
  super
end

Class Method Details

.log_level(fallback: ::Logger::DEBUG) ⇒ Object



22
23
24
# File 'lib/labkit/logging/json_logger.rb', line 22

def self.log_level(fallback: ::Logger::DEBUG)
  ENV.fetch("GITLAB_LOG_LEVEL", fallback)
end

Instance Method Details

#format_message(severity, timestamp, progname, message) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/labkit/logging/json_logger.rb', line 30

def format_message(severity, timestamp, progname, message)
  data = default_attributes
  data[:severity] = severity
  data[:time] = timestamp.utc.iso8601(3)
  data.merge!(Labkit::Context.current.to_h)

  case message
  when String
    data[:message] = message
  when Hash
    reject_reserved_log_keys!(message)
    data.merge!(message)
  end

  dump_json(data) << "\n"
end