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

Instance Methods =====================================================

Yields:

  • (_self)

Yield Parameters:



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, options = nil)
  @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])

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

Returns:

  • (Boolean)


98
99
100
# File 'lib/birling/logger.rb', line 98

def can_rotate?
  !!@path
end

#closeObject



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

Returns:

  • (Boolean)


167
168
169
# File 'lib/birling/logger.rb', line 167

def closed?
  !@log
end

#create_timeObject



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, 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:

  • (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

#sizeObject



102
103
104
# File 'lib/birling/logger.rb', line 102

def size
  @log and @log.respond_to
end