Class: Birling::Logger

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, options = nil) {|_self| ... } ⇒ Logger

Use Birling.open(…) to create new instances.

Yields:

  • (_self)

Yield Parameters:



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, options = nil)
  @encoding = (options and options[:encoding])
  @period = (options and options[:period])
  @severity = self.class.severity(options && options[:severity])
  @retain_count = (options and options[:retain_count])
  @retain_period = (options and options[:retain_period])
  @formatter = (options and options[:formatter] or Birling::Formatter)
  @program = (options and options[:program] or nil)
  @time_source = (options and options[:time_source] or Time)
  @path_format = (options and options[: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_pathObject (readonly)

Returns the value of attribute current_path.



36
37
38
# File 'lib/birling/logger.rb', line 36

def current_path
  @current_path
end

#formatterObject

Returns the value of attribute formatter.



30
31
32
# File 'lib/birling/logger.rb', line 30

def formatter
  @formatter
end

#pathObject (readonly)

Returns the value of attribute path.



33
34
35
# File 'lib/birling/logger.rb', line 33

def path
  @path
end

#path_formatObject (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_formatObject (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

#periodObject (readonly)

Returns the value of attribute period.



39
40
41
# File 'lib/birling/logger.rb', line 39

def period
  @period
end

#programObject

Returns the value of attribute program.



31
32
33
# File 'lib/birling/logger.rb', line 31

def program
  @program
end

#retain_countObject (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_periodObject (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_timeObject (readonly)

Returns the value of attribute rotation_time.



40
41
42
# File 'lib/birling/logger.rb', line 40

def rotation_time
  @rotation_time
end

#severityObject

Properties ===========================================================



29
30
31
# File 'lib/birling/logger.rb', line 29

def severity
  @severity
end

#time_sourceObject

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 <<(message)
  return unless (@log)
  
  self.check_log_rotation!
  
  @log.write(message)
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.

Returns:

  • (Boolean)


114
115
116
# File 'lib/birling/logger.rb', line 114

def can_rotate?
  !!@path
end

#closeObject

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.

Returns:

  • (Boolean)


209
210
211
# File 'lib/birling/logger.rb', line 209

def closed?
  !@log
end

#create_timeObject

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

#flushObject



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, message = 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, message))
end

#opened?Boolean

Returns true if the log is opened, false otherwise.

Returns:

  • (Boolean)


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

#sizeObject

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(message)
  return unless (message.match(/\S/))

  self.log(:debug, message.chomp)
end