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
Writes to the log file regardless of log level.
-
#age(relative_to = nil) ⇒ Object
Returns the age of the log file in seconds if opened, nil otherwise.
-
#can_rotate? ⇒ Boolean
Returns true if the log can be rotated, false otherwise.
-
#close ⇒ Object
Closes the log.
-
#closed? ⇒ Boolean
Returns true if the log is closed, false otherwise.
-
#create_time ⇒ Object
Returns the creation time of the log if opened, nil otherwise.
- #flush ⇒ Object
-
#initialize(log, options = nil) {|_self| ... } ⇒ Logger
constructor
Use Birling.open(…) to create new instances.
-
#log(level, message = nil, program = nil) ⇒ Object
(also: #add)
Log the message for the (optional) program at the given log level.
-
#opened? ⇒ Boolean
Returns true if the log is opened, false otherwise.
-
#retain=(value) ⇒ Object
Sets the retention interval for log files.
-
#size ⇒ Object
Returns size of the log if opened, nil otherwise.
- #sync=(sync) ⇒ Object
-
#write(message) ⇒ Object
An IO compatible method for writing a message to the file.
Constructor Details
#initialize(log, options = nil) {|_self| ... } ⇒ Logger
Use Birling.open(…) to create new instances.
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 |
# File 'lib/birling/logger.rb', line 60 def initialize(log, = nil) @encoding = ( and [:encoding]) @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]) @file_open_options = { } @rotation_time = nil @path = nil @log = nil if (@encoding) @file_open_options[:encoding] = @encoding end 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 Integer SEVERITY_LABEL[value] and value or DEFAULT_SEVERITY else DEFAULT_SEVERITY end end |
Instance Method Details
#<<(message) ⇒ Object
Writes to the log file regardless of log level.
162 163 164 165 166 167 168 |
# File 'lib/birling/logger.rb', line 162 def <<() return unless (@log) self.check_log_rotation! @log.write() end |
#age(relative_to = nil) ⇒ Object
Returns the age of the log file in seconds if opened, nil otherwise.
224 225 226 |
# File 'lib/birling/logger.rb', line 224 def age(relative_to = nil) @log and (relative_to || @time_source.now) - @log.ctime end |
#can_rotate? ⇒ Boolean
Returns true if the log can be rotated, false otherwise.
114 115 116 |
# File 'lib/birling/logger.rb', line 114 def can_rotate? !!@path end |
#close ⇒ Object
Closes the log.
196 197 198 199 200 201 |
# File 'lib/birling/logger.rb', line 196 def close return unless (@log) @log.close @log = nil end |
#closed? ⇒ Boolean
Returns true if the log is closed, false otherwise.
209 210 211 |
# File 'lib/birling/logger.rb', line 209 def closed? !@log end |
#create_time ⇒ Object
Returns the creation time of the log if opened, nil otherwise.
214 215 216 |
# File 'lib/birling/logger.rb', line 214 def create_time @log and @log.ctime end |
#flush ⇒ Object
143 144 145 |
# File 'lib/birling/logger.rb', line 143 def flush # Auto-sync is always turned on, so this operation is ignored. end |
#log(level, message = nil, program = nil) ⇒ Object Also known as: add
Log the message for the (optional) program at the given log level. No data will be written if the current log level is not sufficiently high.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/birling/logger.rb', line 149 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
Returns true if the log is opened, false otherwise.
204 205 206 |
# File 'lib/birling/logger.rb', line 204 def opened? !!@log end |
#retain=(value) ⇒ Object
Sets the retention interval for log files. Value should respond to to_i and yield an integer value that’s a positive number of seconds between rotation operations.
121 122 123 124 125 126 127 128 129 |
# File 'lib/birling/logger.rb', line 121 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
Returns size of the log if opened, nil otherwise.
219 220 221 |
# File 'lib/birling/logger.rb', line 219 def size @log and @log.size end |
#sync=(sync) ⇒ Object
139 140 141 |
# File 'lib/birling/logger.rb', line 139 def sync=(sync) # Auto-sync is always turned on, so this operation is ignored. end |
#write(message) ⇒ Object
An IO compatible method for writing a message to the file. Only non-empty messages are actually logged.
133 134 135 136 137 |
# File 'lib/birling/logger.rb', line 133 def write() return unless (.match(/\S/)) self.log(:debug, .chomp) end |