Sha256: f406f4c213d6c9b774f51f2969bcbfc767312ef82a6eec9c6b85ccd92e41ab9c

Contents?: true

Size: 950 Bytes

Versions: 4

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true
require "time"
require "logger"
require "json"

module Labkit
  module Logging
    class JsonLogger < ::Logger
      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
          data.merge!(message)
        end

        dump_json(data) << "\n"
      end

      private

      def default_attributes
        {}
      end

      def dump_json(data)
        JSON.generate(data)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gitlab-labkit-0.29.0 lib/labkit/logging/json_logger.rb
gitlab-labkit-0.28.0 lib/labkit/logging/json_logger.rb
gitlab-labkit-0.27.0 lib/labkit/logging/json_logger.rb
gitlab-labkit-0.26.0 lib/labkit/logging/json_logger.rb