Class: Commons::Logging::Impl::SimpleLog

Inherits:
NoOpLog
  • Object
show all
Includes:
Log
Defined in:
lib/commons/logging/impl/simple_log.rb

Constant Summary collapse

SYSTEM_PREFIX =
'commons.logging.simplelog.'
DEFAULT_DATE_TIME_FORMAT =
'%Y/%m/%d %H:%M:%S %Z'
LOG_LEVEL_ALL =
0
LOG_LEVEL_TRACE =
1
LOG_LEVEL_DEBUG =
2
LOG_LEVEL_INFO =
3
LOG_LEVEL_WARN =
4
LOG_LEVEL_ERROR =
5
LOG_LEVEL_FATAL =
6
LOG_LEVEL_OFF =
7
@@simple_log_props =
nil
@@show_log_name =
false
@@show_short_name =
true
@@show_date_time =
false
@@date_time_format =
DEFAULT_DATE_TIME_FORMAT

Constants included from Log

Log::DEBUG, Log::ERROR, Log::FATAL, Log::INFO, Log::TRACE, Log::WARN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#debug, #debug?, #error, #error?, #fatal, #fatal?, #info, #info?, #trace, #trace?, #warn, #warn?

Constructor Details

#initialize(name = nil) ⇒ SimpleLog

Returns a new instance of SimpleLog.



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
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/commons/logging/impl/simple_log.rb', line 123

def initialize(name = nil)
  super(name)
  
  @stderr = false
    
  @log_name = name
  
  @current_log_level
  
  @short_log_name = nil
  
  # Set log level from properties
  lvl = self.class.get_string_property(SYSTEM_PREFIX + 'log.' + @log_name)
  i = name == nil ? -1 : name.rindex('::') 
  while lvl == nil && i > -1
    name = name[0 ... i]
    lvl = self.class.get_string_property(SYSTEM_PREFIX + 'log.' + name)
    i = name.rindex('::')
  end
  
  if lvl == nil
    lvl = self.class.get_string_property(SYSTEM_PREFIX + 'default_log')
  end
  
  lvl = lvl.upcase
  case lvl
    when 'ALL'   then set_level(LOG_LEVEL_ALL)
    when 'TRACE' then set_level(LOG_LEVEL_TRACE)
    when 'DEBUG' then set_level(LOG_LEVEL_DEBUG)
    when 'INFO'  then set_level(LOG_LEVEL_INFO)
    when 'WARN'  then set_level(LOG_LEVEL_WARN)
    when 'ERROR' then set_level(LOG_LEVEL_ERROR)
    when 'FATAL' then set_level(LOG_LEVEL_FATAL)
    when 'OFF'   then set_level(LOG_LEVEL_OFF)
    else              set_level(LOG_LEVEL_INFO)
  end
end

Instance Attribute Details

#current_log_level=(value) ⇒ Object

Sets the attribute current_log_level

Parameters:

  • value

    the value to set the attribute current_log_level to.



118
119
120
# File 'lib/commons/logging/impl/simple_log.rb', line 118

def current_log_level=(value)
  @current_log_level = value
end

#log_name=(value) ⇒ Object

Sets the attribute log_name

Parameters:

  • value

    the value to set the attribute log_name to.



118
119
120
# File 'lib/commons/logging/impl/simple_log.rb', line 118

def log_name=(value)
  @log_name = value
end

#stderr=(value) ⇒ Object

Sets the attribute stderr

Parameters:

  • value

    the value to set the attribute stderr to.



118
119
120
# File 'lib/commons/logging/impl/simple_log.rb', line 118

def stderr=(value)
  @stderr = value
end

Class Method Details

.get_boolean_property(name, default) ⇒ Object



86
87
88
89
90
# File 'lib/commons/logging/impl/simple_log.rb', line 86

def self.get_boolean_property(name, default)
  prop = get_string_property(name)
  prop.upcase! unless prop == nil
  return prop == nil ? default : prop == 'TRUE'
end

.get_string_property(name, default = nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/commons/logging/impl/simple_log.rb', line 78

def self.get_string_property(name, default = nil)
  prop = nil;
  prop = ENV[name]
  prop = prop == nil ? @@simple_log_props.get_property(name) : prop
  return prop == nil ? default : prop
end

.static_initializeObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/commons/logging/impl/simple_log.rb', line 96

def self.static_initialize
  @@simple_log_props = Commons::Util::Properties.new
  
  # Add props from the resource simplelog.properties
  props_path = Commons::Lang::ClassLoader.get_resource('simplelog.properties')
  File.open(props_path, 'r') {|file|
    @@simple_log_props.load(file)
  }
  
  @@show_log_name = get_boolean_property(
    SYSTEM_PREFIX + 'show_log_name', @@show_log_name)
  @@show_short_name = get_boolean_property(
    SYSTEM_PREFIX + 'show_short_name', @@show_short_name)
  @@show_date_time = get_boolean_property(
    SYSTEM_PREFIX + 'show_date_time', @@show_date_time)
end

Instance Method Details

#enabled_for?(level) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/commons/logging/impl/simple_log.rb', line 227

def enabled_for?(level)
  case level
    when TRACE
      level = LOG_LEVEL_TRACE
    when DEBUG
      level = LOG_LEVEL_DEBUG
    when INFO
      level = LOG_LEVEL_INFO
    when WARN
      level = LOG_LEVEL_WARN
    when ERROR
      level = LOG_LEVEL_ERROR
    when FATAL
      level = LOG_LEVEL_FATAL
    else
      level = LOG_LEVEL_OFF
  end
  
  return (level >= @current_log_level)
end

#get_levelObject



167
168
169
# File 'lib/commons/logging/impl/simple_log.rb', line 167

def get_level
  return @current_log_level
end

#log(level, message, exception = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/commons/logging/impl/simple_log.rb', line 172

def log(level, message, exception = nil)
  if message == nil
    message = '<null>'
  end
  
  buf = ''
  
  if @@show_date_time
    now = Time.now
    buf.concat(
      now.strftime(@@date_time_format) + ' +' + now.usec.to_s)
    buf.concat(' ')
  end
  
  case level
    when TRACE then buf.concat('[TRACE] ')
    when DEBUG then buf.concat('[DEBUG] ')
    when INFO  then buf.concat('[INFO]  ')
    when WARN  then buf.concat('[WARN]  ')
    when ERROR then buf.concat('[ERROR] ')
    when FATAL then buf.concat('[FATAL] ')
  end
  
  if @@show_short_name
    if @short_log_name == nil
      # Cut all but the last component of the name
      delim_rindex = @log_name.rindex('::')
      if delim_rindex != nil
        @short_log_name = @log_name[delim_rindex + 2 .. -1]
      end
    end
    buf.concat(@short_log_name).concat(' - ')
  elsif @@show_log_name
    buf.concat(@log_name).concat(' - ')
  end
  
  buf.concat(message.to_s)
  
  # Append stack trace if not null
  if exception != nil
    buf.concat(
      ' <' + exception.class.name + ': ' + exception.message.to_s + '; ' \
      + exception.stack_trace_string + '> ')
  end
  
  write(buf);   
end

#set_level(current_log_level) ⇒ Object



162
163
164
# File 'lib/commons/logging/impl/simple_log.rb', line 162

def set_level(current_log_level)
  @current_log_level = current_log_level
end