Class: Logger
- Inherits:
-
Object
- Object
- Logger
- Defined in:
- lib/releaseable/extensions.rb
Overview
Logger extensions
Constant Summary collapse
- INPUTS =
{:default => 'c568432d-8f50-4b12-9322-5f6ffda7883b', :opts => 'b9a22025-b148-4dfe-8355-ce5dba40b6a2', :json => '62a8ef41-fd80-460f-a6f7-e45f232dd1d8', :aux => '207f124e-9de6-434f-b100-af1ac3585089', }
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#logdev ⇒ Object
Returns the value of attribute logdev.
Class Method Summary collapse
-
.sound_alarm(beeps = 3) ⇒ Object
Alarm sound to get my attention.
Instance Method Summary collapse
- #add(*args, &block) ⇒ Object
-
#configure(config = {}, &formatter) ⇒ Object
Extended configuration for Logger.
- #fatal(*args) ⇒ Object
-
#finalize ⇒ Object
Finalize logger.
-
#handler ⇒ Object
Extended handler for fatal errors.
- #old_add ⇒ Object
- #old_fatal ⇒ Object
-
#sound_alarm(beeps = @config[:beeps]) ⇒ Object
Alarm sound on Logger instance.
-
#trace(error) ⇒ Object
Trace errors in json format.
-
#with_timeout(out = 5) ⇒ Object
Execute with timeout and protection from exceptions.
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
16 17 18 |
# File 'lib/releaseable/extensions.rb', line 16 def config @config end |
#logdev ⇒ Object
Returns the value of attribute logdev.
16 17 18 |
# File 'lib/releaseable/extensions.rb', line 16 def logdev @logdev end |
Class Method Details
.sound_alarm(beeps = 3) ⇒ Object
Alarm sound to get my attention
19 20 21 22 23 24 |
# File 'lib/releaseable/extensions.rb', line 19 def self.sound_alarm beeps = 3 beeps.times do java.awt.Toolkit.getDefaultToolkit.beep sleep 0.5 end end |
Instance Method Details
#add(*args, &block) ⇒ Object
152 153 154 155 |
# File 'lib/releaseable/extensions.rb', line 152 def add *args, &block @file_logger.add *args if @file_logger old_add *args, &block end |
#configure(config = {}, &formatter) ⇒ Object
Extended configuration for Logger
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/releaseable/extensions.rb', line 110 def configure config={}, &formatter @config = (config[:log] || config).dup @config[:beeps] ||= 0 @config[:time_format] ||= @config[:time] || '%H:%M:%S.%N' @handle_fatal_errors = @config[:ignore] ? false : true # Configure fatal error handler: if @handle_fatal_errors @config[:tag] = config[:tag] || config[:name] || !config[:connection] ? "Tag#{rand(10000)}" : "#{config[:env]}-#{config[:connection][:client_id]}-#{config[:connection][:account]}" @config[:domain] ||= 'https://logs.loggly.com' @config[:failsafe] ||= StringIO.new("") # $stderr @config[:input] = case @config[:input] when Symbol INPUTS[@config[:input]] when nil INPUTS[:opts] else @config[:input] end end self.level = @config[:level] || Logger::INFO self.formatter = formatter || proc do |level, time, prog, msg| "#{time.strftime(@config[:time_format])} #{msg}\n" end # Configure detailed backup file logger: if @config[:file] @file = Pathname.new(@config[:file]).realpath.to_s @file_logger = Logger.new(@file).tap do |logger| logger.formatter = proc do |level, time, prog, msg| "#{time.strftime('%H:%M:%S.%N')} #{level[0..0]} #{msg}\n" end logger.level = Logger::INFO #DEBUG end end end |
#fatal(*args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/releaseable/extensions.rb', line 69 def fatal *args if handler with_timeout do entry = args.first case entry when Exception trace :severity => Logger::FATAL, :error => entry.class, :msg => entry., :backtrace => (entry.backtrace || []).join(', ') else trace :severity => Logger::INFO, :msg => "#{args.join(', ')}" old_fatal *args end end else old_fatal *args end end |
#finalize ⇒ Object
Finalize logger
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/releaseable/extensions.rb', line 53 def finalize if handler with_timeout do if @config[:failsafe].respond_to?(:string) && @config[:failsafe].string != '' trace :failsafe => @config[:failsafe].string end sleep 2 @handler.logdev.dev.deliverer.kill end end @file_logger.close if @file_logger close end |
#handler ⇒ Object
Extended handler for fatal errors
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/releaseable/extensions.rb', line 90 def handler return @handler if @handler && @handler.logdev.dev.deliverer.alive? @handle_init_timeout ||= Time.now if @handle_fatal_errors && @handle_init_timeout <= Time.now @handle_init_timeout = Time.now + 5 * 60 # Retry init in 5 minutes with_timeout do @handler = Logglier.new "#{@config[:domain]}/inputs/#{@config[:input]}", :failsafe => @config[:failsafe], :read_timeout => 10, # defaults to 120 :open_timeout => 10, # defaults to 120 :threaded => true, :format => :json @handler.progname = @config[:progname] end end end |
#old_add ⇒ Object
150 |
# File 'lib/releaseable/extensions.rb', line 150 alias old_add add |
#old_fatal ⇒ Object
67 |
# File 'lib/releaseable/extensions.rb', line 67 alias old_fatal fatal |
#sound_alarm(beeps = @config[:beeps]) ⇒ Object
Alarm sound on Logger instance
27 28 29 |
# File 'lib/releaseable/extensions.rb', line 27 def sound_alarm beeps = @config[:beeps] self.class.sound_alarm beeps end |
#trace(error) ⇒ Object
Trace errors in json format
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/releaseable/extensions.rb', line 41 def trace error if handler with_timeout do hash = error.is_a?(Hash) ? error : {:msg => error} handler.add(hash[:severity] || Logger::UNKNOWN) do hash.merge :tag => @config[:tag] end end end end |
#with_timeout(out = 5) ⇒ Object
Execute with timeout and protection from exceptions
32 33 34 35 36 37 38 |
# File 'lib/releaseable/extensions.rb', line 32 def with_timeout out=5 # Something that should be interrupted if it takes too much time... Timeout::timeout(out) { yield } rescue Timeout::Error, Exception => e @config[:failsafe].puts e nil end |