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
- .bin_regex ⇒ Object
-
.clean_ex(ex, exclude_gems = true, exclude_dupes = true) ⇒ String
Created a cleaned up exception, suitable for printing to the console.
- .clean_ex_for_console(ex, logger) ⇒ Object
-
.console_appender?(logger) ⇒ Boolean
(also: console?, stdout?)
Check whether the given logger is configured to append to the console (STDOUT).
- .gems_regex ⇒ Object
- .ruby_regex ⇒ Object
-
.setup_logger(opts = {}) ⇒ Object
Setup logging.
Instance Method Summary collapse
-
#log ⇒ Logger
(also: #logger)
Get a log instance for this class.
Class Method Details
.bin_regex ⇒ Object
22 23 24 |
# File 'lib/bixby-common/util/log.rb', line 22 def bin_regex @bin_regex ||= %r{#{Gem.path.first}/(bin/.*)$} end |
.clean_ex(ex, exclude_gems = true, exclude_dupes = true) ⇒ String
Created a cleaned up exception, suitable for printing to the console
This method should generally only be used for debugging as it can be quite slow.
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 |
# File 'lib/bixby-common/util/log.rb', line 64 def clean_ex(ex, exclude_gems=true, exclude_dupes=true) puts self s = [] s << "<#{ex.class}> #{ex.}" last_gem = nil ex.backtrace.each do |e| if e =~ Log.gems_regex then next if exclude_gems gem_name = $2 gem_ver = $3 trace = $4 gem_str = "#{gem_name} (#{gem_ver})" if !exclude_dupes || (last_gem.nil? || last_gem != gem_name) then s << " #{gem_str} #{trace}" elsif exclude_dupes && last_gem == gem_name then # s << " " + (" "*(gem_str.size+1)) + "..." end last_gem = gem_name elsif e =~ Log.bin_regex then next if exclude_gems rel_path = $1 s << " #{rel_path}" elsif e =~ Log.ruby_regex then next if exclude_gems last_gem = nil trace = $2 s << " ruby (#{RUBY_VERSION}) #{trace}" else s << " #{e}" end end s.join("\n") end |
.clean_ex_for_console(ex, logger) ⇒ Object
51 52 53 |
# File 'lib/bixby-common/util/log.rb', line 51 def clean_ex_for_console(ex, logger) console?(logger) ? clean_ex(ex) : ex end |
.console_appender?(logger) ⇒ Boolean Also known as: console?, stdout?
Check whether the given logger is configured to append to the console (STDOUT)
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bixby-common/util/log.rb', line 35 def console_appender?(logger) logger.appenders.each do |a| if a.kind_of? Logging::Appenders::Stdout then return true end end if logger.respond_to?(:parent) && logger.parent then return console_appender?(logger.parent) end false # at root end |
.gems_regex ⇒ Object
15 16 17 18 19 20 |
# File 'lib/bixby-common/util/log.rb', line 15 def gems_regex return @gems_regex if @gems_regex gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) } gems_paths << "#{Gem.path.first}/bundler" # include path for gems installed via git repo @gems_regex = %r{(#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)} end |
.ruby_regex ⇒ Object
26 27 28 |
# File 'lib/bixby-common/util/log.rb', line 26 def ruby_regex @ruby_regex ||= %r{^#{ENV["MY_RUBY_HOME"]}/lib(/.*?)?/ruby/[\d.]+/(.*)$} end |
.setup_logger(opts = {}) ⇒ Object
Setup logging
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/bixby-common/util/log.rb', line 130 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 = { :keep => 7, :roll_by => 'date', :age => 'daily', :truncate => false, :auto_flushing => true, :layout => layout }.merge(opts) Logging.appenders.rolling_file("file", ) 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
#log ⇒ Logger Also known as: logger
Get a log instance for this class
107 108 109 |
# File 'lib/bixby-common/util/log.rb', line 107 def log @log ||= Logging.logger[self] end |