Module: Bixby::Log

Included in:
APIChannel, APIChannel, ThreadPool, ThreadPool::Worker
Defined in:
lib/bixby-common/util/log.rb,
lib/bixby-common/util/log/filtering_layout.rb

Overview

A simple logging mixin

Defined Under Namespace

Classes: FilteringLayout

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_logger(opts = {}) ⇒ Object

Setup logging

Parameters:

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

    Options for the rolling file appender

Options Hash (opts):

  • :filename (String)

    Filename to log to

  • :level (Symbol)

    Log level to use (default = :warn)

  • :pattern (String)

    Log pattern



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bixby-common/util/log.rb', line 39

def self.setup_logger(opts={})

  # set level: ENV flag overrides; default to warn
  opts[:level] = ENV["BIXBY_LOG"] if ENV["BIXBY_LOG"]

  if opts[:level].nil? then
    # try to read from config file
    c = Bixby.path("etc", "bixby.yml")
    if File.exists? c then
      if config = YAML.load_file(c) then
        log_level = config["log_level"]
        log_level = log_level.strip.downcase if log_level.kind_of? String
        opts[:level] = log_level
      end
    end
  end

  opts[:level] ||= :info

  pattern = opts.delete(:pattern) || '%.1l, [%d] %5l -- %c:%L: %m\n'
  layout = Logging.layouts.pattern(:pattern => pattern)

  opts[:filename] ||= Bixby.path("var", "bixby-agent.log")
  log_dir = File.dirname(opts[:filename])
  FileUtils.mkdir_p(log_dir)

  # make sure we have the correct permissions
  if Process.uid == 0 then
    if !File.exists? opts[:filename] then
      FileUtils.touch([opts[:filename], opts[:filename] + ".age"])
    end
    File.chmod(0777, log_dir)
    File.chmod(0777, opts[:filename])
    File.chmod(0777, opts[:filename] + ".age")
  end

  # configure stdout appender (used in debug modes, etc)
  Logging.color_scheme( 'bright',
    :levels => {
      :info  => :green,
      :warn  => :yellow,
      :error => :red,
      :fatal => [:white, :on_red]
    },
    :date => :blue,
    :logger => :cyan,
    :message => :magenta
  )
  Logging.appenders.stdout( 'stdout',
    :auto_flushing => true,
    :layout => Logging.layouts.pattern(
      :pattern => pattern,
      :color_scheme => 'bright'
    )
  )

  # configure rolling file appender
  options = {
    :keep          => 7,
    :roll_by       => 'date',
    :age           => 'daily',
    :truncate      => false,
    :auto_flushing => true,
    :layout        => layout
  }.merge(opts)
  Logging.appenders.rolling_file("file", options)

  root = Logging::Logger.root
  root.add_appenders("file") if !root.appenders.find{ |a| a.name == "file" }
  root.level = opts[:level]
  root.trace = true

  # setup HTTPI logger
  HTTPI.log = true
  HTTPI.logger = Logging.logger[HTTPI]
end

Instance Method Details

#logLogger Also known as: logger

Get a log instance for this class

Returns:

  • (Logger)


16
17
18
# File 'lib/bixby-common/util/log.rb', line 16

def log
  @log ||= Logging.logger[self]
end