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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/steno/config.rb', line 86

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 }

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

Instance Attribute Details

#codecObject (readonly)

Returns the value of attribute codec.



82
83
84
# File 'lib/steno/config.rb', line 82

def codec
  @codec
end

#contextObject (readonly)

Returns the value of attribute context.



83
84
85
# File 'lib/steno/config.rb', line 83

def context
  @context
end

#default_log_levelObject (readonly)

Returns the value of attribute default_log_level.



84
85
86
# File 'lib/steno/config.rb', line 84

def default_log_level
  @default_log_level
end

#sinksObject (readonly)

Returns the value of attribute sinks.



81
82
83
# File 'lib/steno/config.rb', line 81

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
67
68
69
70
71
72
73
74
# 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
  }

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

  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
  else
    if hash[:syslog]
      Steno::Sink::Syslog.instance.open(hash[:syslog])
      opts[:sinks] << Steno::Sink::Syslog.instance
    end
  end

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

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

  opts
end