Class: Steno::Config

Inherits:
Object show all
Defined in:
lib/steno/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/steno/config.rb', line 75

def initialize(opts = {})
  @sinks             = opts[:sinks] || []
  @codec             = opts[:codec] || Steno::Codec::Json.new
  @context           = opts[:context] || Steno::Context::Null.new

  @sinks.each { |sink| sink.codec = @codec }

  @default_log_level = if opts[:default_log_level]
                         opts[:default_log_level].to_sym
                       else
                         :info
                       end
end

Instance Attribute Details

#codecObject (readonly)

Returns the value of attribute codec.



73
74
75
# File 'lib/steno/config.rb', line 73

def codec
  @codec
end

#contextObject (readonly)

Returns the value of attribute context.



73
74
75
# File 'lib/steno/config.rb', line 73

def context
  @context
end

#default_log_levelObject (readonly)

Returns the value of attribute default_log_level.



73
74
75
# File 'lib/steno/config.rb', line 73

def default_log_level
  @default_log_level
end

#sinksObject (readonly)

Returns the value of attribute sinks.



73
74
75
# File 'lib/steno/config.rb', line 73

def sinks
  @sinks
end

Class Method Details

.from_file(path, overrides = {}) ⇒ Steno::Config

Creates a config given a yaml file of the following form:

logging:
  level:  <info, debug, etc>
  file:   </path/to/logfile>
  syslog: <syslog name>

Parameters:

  • path (String)

    Path to yaml config

  • overrides (Hash) (defaults to: {})

Returns:



24
25
26
27
28
# File 'lib/steno/config.rb', line 24

def from_file(path, overrides = {})
  h = YAML.load_file(path)
  h = h['logging'] || {}
  new(to_config_hash(h).merge(overrides))
end

.from_hash(hash) ⇒ Object



30
31
32
# File 'lib/steno/config.rb', line 30

def from_hash(hash)
  new(to_config_hash(hash))
end

.to_config_hash(hash) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/steno/config.rb', line 34

def to_config_hash(hash)
  hash ||= {}
  hash = symbolize_keys(hash)

  level = hash[:level] || hash[:default_log_level]
  opts = {
    sinks: [],
    default_log_level: level.nil? ? :info : level.to_sym
  }

  opts[:codec] = Steno::Codec::Json.new(iso8601_timestamps: true) if hash[:iso8601_timestamps]

  if hash[:file]
    max_retries = hash[:max_retries]
    opts[:sinks] << Steno::Sink::IO.for_file(hash[:file], max_retries: max_retries)
  end

  if Steno::Sink::WINDOWS
    if hash[:eventlog]
      Steno::Sink::Eventlog.instance.open(hash[:eventlog])
      opts[:sinks] << Steno::Sink::Eventlog.instance
    end
  elsif hash[:syslog]
    Steno::Sink::Syslog.instance.open(hash[:syslog])
    opts[:sinks] << Steno::Sink::Syslog.instance
  end

  opts[:sinks] << Steno::Sink::Fluentd.new(hash[:fluentd]) if hash[:fluentd]

  opts[:sinks] << Steno::Sink::IO.new(STDOUT) if opts[:sinks].empty?

  opts
end