Class: Blinkbox::CommonLogging

Inherits:
GELF::Logger
  • Object
show all
Defined in:
lib/blinkbox/common_logging.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "localhost", port: 12201, facility: $0, max_size: 8192, facility_version: nil, echo_to_console: false) ⇒ CommonLogging

Returns a new instance of CommonLogging.

Raises:

  • (ArgumentError)

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blinkbox/common_logging.rb', line 7

def initialize(host: "localhost", port: 12201, facility: $0, max_size: 8192, facility_version: nil, echo_to_console: false)
  validity_issues = []
  validity_issues.push("host") unless host.is_a?(String)
  validity_issues.push("port") unless port.is_a?(Integer)
  validity_issues.push("facility") unless facility.is_a?(String)
  validity_issues.push("max_size") unless max_size.is_a?(Integer)
  raise ArgumentError, "Cannot start the logger, the following settings weren't valid: #{validity_issues.join(", ")}" if !validity_issues.empty?

  if echo_to_console
    @stdout_logger = Logger.new(STDOUT)
    @stdout_logger.formatter = proc do |severity, datetime, progname, msg|
      # Using console output is only for live-debug, so we can strip the timestamp, progname
      l = msg.dup
      l.delete("facility")
      l.delete(:facility)
      log_msg = "#{severity[0..0]} #{l.delete("short_message")}#{l.delete(:short_message)}"
      log_msg << " (#{l})" if l.any?
      log_msg << "\n"
      log_msg
    end
  end
  options = { facility: facility }
  options[:facilityVersion] = facility_version unless facility_version.nil?
  super(host, port, max_size, options)
end

Class Method Details

.from_config(hash) ⇒ CommonLogging

Accepts a hash detailing log config settings in the format used in blinkbox Books config files and returns a CommonLogging object.

Examples:

Load from config

config = Blinkbox::CommonConfig.new
logger = Blinkbox::CommonLogging.from_config(config.tree(:logging))

Parameters:

  • hash (Hash)

    The returned hash from CommonConfig#tree

Options Hash (hash):

  • :udp.host (String) — default: localhost

    The Graylog host

  • :udp.port (Integer) — default: 12201

    The Graylog port number

  • :gelf.facility (String)

    Organisation/app_name eg. Marvin/cover_processor

  • :gelf.maxChunkSize (Integer) — default: 8192

    The default size for UDP chunks sent to the server.

  • :level ("DEBUG", "INFO", "WARN", "ERROR", "FATAL") — default: "INFO"

    The level below which messages will not be sent to Graylog.

Returns:


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/blinkbox/common_logging.rb', line 53

def self.from_config(hash)
  mapping = {
    host:            :'udp.host',
    port:            :'udp.port',
    facility:        :'gelf.facility',
    max_size:        :'gelf.maxChunkSize',
    echo_to_console: :'console.enabled'
  }
  begin
    logger = new(Hash[mapping.map { |k, v| [k, hash[v]] }])
  rescue ArgumentError => e
    msg = e.message
    mapping.each do |k, v|
      msg.sub!(k.to_s, v.to_s)
    end
    raise e.class, msg
  end
  logger.level = GELF.const_get(hash[:level].upcase) rescue GELF::INFO
  logger.level_mapping = :direct
  logger
end

Instance Method Details

#facility_version=(semver) ⇒ Object

Allows the setting of the facility version default parameter.

Parameters:

  • semver (String)

    The semantic version of the service which is sending logs


35
36
37
# File 'lib/blinkbox/common_logging.rb', line 35

def facility_version=(semver)
  self.default_options['facilityVersion'] = semver
end