Class: Birling::Logger
- Inherits:
-
Object
- Object
- Birling::Logger
- Defined in:
- lib/birling/logger.rb
Constant Summary collapse
- SEVERITY =
These level constants are the same as the syslog system utility
{ :emergency => EMERGENCY = 0, :alert => ALERT = 1, :critical => CRITICAL = 2, :error => ERROR = 3, :warning => WARNING = 4, :notice => NOTICE = 5, :info => INFO = 6, :debug => DEBUG = 7, :unknown => UNKNOWN = 999 }.freeze
- DEFAULT_SEVERITY =
UNKNOWN
- SEVERITY_LABEL =
SEVERITY.invert.freeze
- PATH_TIME_DEFAULT =
{ :hourly => '%Y%m%d%H'.freeze, :daily => '%Y%m%d'.freeze, :default => '%s'.freeze }.freeze
Instance Attribute Summary collapse
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_format ⇒ Object
readonly
Returns the value of attribute path_format.
-
#path_time_format ⇒ Object
readonly
Returns the value of attribute path_time_format.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#program ⇒ Object
Returns the value of attribute program.
-
#retain_count ⇒ Object
readonly
Returns the value of attribute retain_count.
-
#retain_period ⇒ Object
readonly
Returns the value of attribute retain_period.
-
#rotation_time ⇒ Object
readonly
Returns the value of attribute rotation_time.
-
#severity ⇒ Object
Properties ===========================================================.
-
#time_source ⇒ Object
Returns the value of attribute time_source.
Class Method Summary collapse
-
.severity(value) ⇒ Object
Class Methods ========================================================.
Instance Method Summary collapse
- #<<(message) ⇒ Object
- #age(relative_to = nil) ⇒ Object
- #can_rotate? ⇒ Boolean
- #close ⇒ Object
- #closed? ⇒ Boolean
- #create_time ⇒ Object
-
#initialize(log, options = nil) {|_self| ... } ⇒ Logger
constructor
Instance Methods =====================================================.
- #log(level, message = nil, program = nil) ⇒ Object (also: #add)
- #opened? ⇒ Boolean
- #retain=(value) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(log, options = nil) {|_self| ... } ⇒ Logger
Instance Methods =====================================================
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 |
# File 'lib/birling/logger.rb', line 59 def initialize(log, = nil) @period = ( and [:period]) @severity = self.class.severity( && [:severity]) @retain_count = ( and [:retain_count]) @retain_period = ( and [:retain_period]) @formatter = ( and [:formatter] or Birling::Formatter) @program = ( and [:program] or nil) @time_source = ( and [:time_source] or Time) @path_format = ( and [:path_format]) case (log) when IO, StringIO @log = log when String @path = log end if (@path and @period) @rotation_time = self.next_rotation_time @path_time_format = (PATH_TIME_DEFAULT[@period] or PATH_TIME_DEFAULT[:default]) @path_format ||= @path.sub(/\.(\w+)$/) do |s| '.' + @path_time_format + '.' + $1 end end if (@path and !@log) self.log_open! end yield(self) if (block_given?) end |
Instance Attribute Details
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
36 37 38 |
# File 'lib/birling/logger.rb', line 36 def current_path @current_path end |
#formatter ⇒ Object
Returns the value of attribute formatter.
30 31 32 |
# File 'lib/birling/logger.rb', line 30 def formatter @formatter end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
33 34 35 |
# File 'lib/birling/logger.rb', line 33 def path @path end |
#path_format ⇒ Object (readonly)
Returns the value of attribute path_format.
34 35 36 |
# File 'lib/birling/logger.rb', line 34 def path_format @path_format end |
#path_time_format ⇒ Object (readonly)
Returns the value of attribute path_time_format.
35 36 37 |
# File 'lib/birling/logger.rb', line 35 def path_time_format @path_time_format end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
39 40 41 |
# File 'lib/birling/logger.rb', line 39 def period @period end |
#program ⇒ Object
Returns the value of attribute program.
31 32 33 |
# File 'lib/birling/logger.rb', line 31 def program @program end |
#retain_count ⇒ Object (readonly)
Returns the value of attribute retain_count.
37 38 39 |
# File 'lib/birling/logger.rb', line 37 def retain_count @retain_count end |
#retain_period ⇒ Object (readonly)
Returns the value of attribute retain_period.
38 39 40 |
# File 'lib/birling/logger.rb', line 38 def retain_period @retain_period end |
#rotation_time ⇒ Object (readonly)
Returns the value of attribute rotation_time.
40 41 42 |
# File 'lib/birling/logger.rb', line 40 def rotation_time @rotation_time end |
#severity ⇒ Object
Properties ===========================================================
29 30 31 |
# File 'lib/birling/logger.rb', line 29 def severity @severity end |
#time_source ⇒ Object
Returns the value of attribute time_source.
32 33 34 |
# File 'lib/birling/logger.rb', line 32 def time_source @time_source end |
Class Method Details
.severity(value) ⇒ Object
Class Methods ========================================================
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/birling/logger.rb', line 44 def self.severity(value) case (value) when Symbol SEVERITY[value] or DEFAULT_SEVERITY when String SEVERITY[value.to_sym] or DEFAULT_SEVERITY when Fixnum SEVERITY_LABEL[value] and value or DEFAULT_SEVERITY else DEFAULT_SEVERITY end end |
Instance Method Details
#<<(message) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/birling/logger.rb', line 128 def <<() return unless (@log) self.check_log_rotation! @log.write() end |
#age(relative_to = nil) ⇒ Object
179 180 181 |
# File 'lib/birling/logger.rb', line 179 def age(relative_to = nil) (relative_to || @time_source.now) - @log.ctime end |
#can_rotate? ⇒ Boolean
98 99 100 |
# File 'lib/birling/logger.rb', line 98 def can_rotate? !!@path end |
#close ⇒ Object
156 157 158 159 160 161 |
# File 'lib/birling/logger.rb', line 156 def close return unless (@log) @log.close @log = nil end |
#closed? ⇒ Boolean
167 168 169 |
# File 'lib/birling/logger.rb', line 167 def closed? !@log end |
#create_time ⇒ Object
171 172 173 |
# File 'lib/birling/logger.rb', line 171 def create_time @log and @log.ctime end |
#log(level, message = nil, program = nil) ⇒ Object Also known as: add
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/birling/logger.rb', line 116 def log(level, = nil, program = nil) return unless (@log) level = self.class.severity(level) program ||= @program self.check_log_rotation! @log.write(@formatter.call(level, @time_source.now, program, )) end |
#opened? ⇒ Boolean
163 164 165 |
# File 'lib/birling/logger.rb', line 163 def opened? !!@log end |
#retain=(value) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/birling/logger.rb', line 106 def retain=(value) @retain = value ? value.to_i : nil if (@retain_period and @retain_period <= 0) @retain_period = nil end @retain_period end |
#size ⇒ Object
102 103 104 |
# File 'lib/birling/logger.rb', line 102 def size @log and @log.respond_to end |