Sha256: 47fb62a62d04f7cde6511cfe4fc7edabf1c7b4d21c5333b540d23e612146ebc6
Contents?: true
Size: 1.66 KB
Versions: 1
Compression:
Stored size: 1.66 KB
Contents
# frozen_string_literal: true require "time" require "logger" require "json" module Labkit module Logging class JsonLogger < ::Logger INTERNAL_LOG_KEYS = Labkit::Context::RAW_KEYS + [Labkit::Context::LOG_KEY] RESERVED_LOG_KEYS = INTERNAL_LOG_KEYS.map(&:to_sym) + [ :environment, :host, :shard, :stage, :subcomponent, :tier, :type, ].freeze def self.log_level(fallback: ::Logger::DEBUG) ENV.fetch("GITLAB_LOG_LEVEL", fallback) end def initialize(path, level: JsonLogger.log_level) super end def format_message(severity, timestamp, progname, message) data = default_attributes data[:severity] = severity data[:time] = timestamp.utc.iso8601(3) data[Labkit::Correlation::CorrelationId::LOG_KEY] = Labkit::Correlation::CorrelationId.current_id case message when String data[:message] = message when Hash reject_reserved_log_keys!(message) data.merge!(message) end dump_json(data) << "\n" end private def default_attributes {} end def dump_json(data) JSON.generate(data) end def reject_reserved_log_keys!(hash) return if ENV["RAILS_ENV"] == "production" reserved_keys_used = hash.transform_keys(&:to_sym).slice(*RESERVED_LOG_KEYS) if reserved_keys_used.any? raise "The following log keys used are reserved: #{reserved_keys_used.keys.join(", ")}" + "\n\nUse key names that are descriptive e.g. by using a prefix." end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gitlab-labkit-0.30.0 | lib/labkit/logging/json_logger.rb |