Method: VCAP::Logging.setup_from_config

Defined in:
lib/vcap/logging.rb

.setup_from_config(config = {}) ⇒ Object

Configures the logging infrastructure using a hash parsed from a config file. The config file is expected to contain a section with the following format: logging:

 level: <default_log_level>
  file: <filename>
syslog: <program name to use with the syslog sink>

This interface is limiting, but it should satisfy the majority of our use cases. I’m imagining usage will be something like:

config = YAML.load(<file>)
...
VCAP::Logging.setup_from_config(config[:logging])


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vcap/logging.rb', line 81

def setup_from_config(config={})
  level = config[:level] || config['level']
  if level
    level_sym = level.to_sym
    raise ArgumentError, "Unknown level: #{level}" unless LOG_LEVELS[level_sym]
    @default_log_level = level_sym
  end

  logfile = config[:file] || config['file']
  # Undecided as to whether or not we should enable buffering here. For now, don't buffer to stay consistent with the current logger.
  add_sink(nil, nil, VCAP::Logging::Sink::FileSink.new(logfile, FORMATTER)) if logfile

  syslog_name = config[:syslog] || config['syslog']
  add_sink(nil, nil, VCAP::Logging::Sink::SyslogSink.new(syslog_name, :formatter => FORMATTER)) if syslog_name

  # Log to stdout if no other sinks are supplied
  add_sink(nil, nil, VCAP::Logging::Sink::StdioSink.new(STDOUT, FORMATTER)) unless (logfile || syslog_name)
end