Sha256: e922e3427c7910aef4d1119a7e30fe4227d84f09152a2f927e6f93997950638c

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

begin
  require 'syslog_protocol'
rescue LoadError
  raise 'Gem syslog_protocol is required for remote logging using the Syslog protol. Please add the gem "syslog_protocol" to your Gemfile.'
end

module SemanticLogger
  module Formatters
    class SyslogCee < Raw
      attr_accessor :level_map, :facility

      # CEE JSON Syslog format
      #   Untested prototype code. Based on documentation only.
      #   If this works for you, please let us know by opening an issue.
      #
      # Parameters:
      #   facility: [Integer]
      #     Default: ::Syslog::LOG_USER
      #
      #   level_map: [Hash | SemanticLogger::Formatters::Syslog::LevelMap]
      #     Supply a custom map of SemanticLogger levels to syslog levels.
      #
      # Example:
      #   # Log via udp to a remote syslog server on host: `server1` and port `8514`, using the CEE format.
      #   SemanticLogger.add_appender(appender: :syslog, formatter: syslog_cee, url: 'udp://server1:8514')
      def initialize(facility: ::Syslog::LOG_USER, level_map: SemanticLogger::Formatters::Syslog::LevelMap.new)
        @facility  = facility
        @level_map = level_map.is_a?(SemanticLogger::Formatters::Syslog::LevelMap) ? level_map : SemanticLogger::Formatters::Syslog::LevelMap.new(level_map)
        super()
      end

      # Time is part of the syslog packet and is not included in the formatted message.
      def time
      end

      def call(log, logger)
        hash = super(log, logger)
        create_syslog_packet("@cee: #{hash.to_json}")
      end

      private

      # Create Syslog Packet
      def create_syslog_packet(message)
        packet          = SyslogProtocol::Packet.new
        packet.hostname = logger.host
        packet.facility = facility
        packet.tag      = logger.application.gsub(' ', '')
        packet.content  = message
        packet.time     = log.time
        packet.severity = level_map[log.level]
        packet.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
semantic_logger-4.2.0 lib/semantic_logger/formatters/syslog_cee.rb
semantic_logger-4.1.1 lib/semantic_logger/formatters/syslog_cee.rb
semantic_logger-4.1.0 lib/semantic_logger/formatters/syslog_cee.rb