Class: OpenHAB::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/log.rb

Overview

Ruby Logger that forwards messages at appropriate levels to openHAB Logger

Constant Summary collapse

PREFIX =

The base prefix for all loggers from this gem.

"org.openhab.automation.jrubyscripting"
LEVELS =

Returns Supported logging levels.

Returns:

  • (Array<symbol>)

    Supported logging levels

%i[trace debug warn info error].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#level:error, ...

Note:

When a logger's level is modified, the logging infrastructure has to reload, and logging may be completely unavailable for a short time.

Returns The current log level.

Examples:

Retrieve the current log level

level = OpenHAB::Log.logger("openhab.event.ItemStateChangedEvent").level
logger.info "The log level for 'openhab.event.ItemStateChangedEvent' is #{level}"

Set the log level

OpenHAB::Log.logger("openhab.event.ItemStateChangedEvent").level = :warn

Returns:

  • (:error, :warn, :info, :debug, :trace)

    The current log level



283
284
285
# File 'lib/openhab/log.rb', line 283

def level
  Logger.log_service.get_level(name)[name]&.downcase&.to_sym
end

Class Method Details

.eventsLogger

The events logger (events.log)

Returns:



199
200
201
# File 'lib/openhab/log.rb', line 199

def events
  Log.logger("openhab.event")
end

.gem_rootLogger

The root logger for this gem

Returns:



193
194
195
# File 'lib/openhab/log.rb', line 193

def gem_root
  Log.logger(PREFIX)
end

.rootLogger

The root logger (all of openHAB)

Returns:



187
188
189
# File 'lib/openhab/log.rb', line 187

def root
  Log.logger(org.slf4j.Logger::ROOT_LOGGER_NAME)
end

Instance Method Details

#debug(msg = nil) { ... } ⇒ void

This method returns an undefined value.

Log a message at debug level.

Examples:

logger.debug do
  total = Item1.state + Item2.state
  average = total / 2
"Total: #{total}, Average: #{average}"
end

Parameters:

  • msg (Object, nil) (defaults to: nil)

    The log message

Yields:

  • Pass a block to delay generating the log message until it's confirmed that logging is enabled at debug level.

Yield Returns:

  • (Object, nil)

    The log message



299
# File 'lib/openhab/log.rb', line 299

def_level_method(:debug)

#debug?true, false

If the logger is enabled at debug level.

Returns:

  • (true, false)


300
# File 'lib/openhab/log.rb', line 300

def_level_predicate(:debug)

#error(msg = nil) { ... } ⇒ void

This method returns an undefined value.

Log a message at error level.

Examples:

logger.error do
  total = Item1.state + Item2.state
  average = total / 2
"Total: #{total}, Average: #{average}"
end

Parameters:

  • msg (Object, nil) (defaults to: nil)

    The log message

Yields:

  • Pass a block to delay generating the log message until it's confirmed that logging is enabled at error level.

Yield Returns:

  • (Object, nil)

    The log message



293
# File 'lib/openhab/log.rb', line 293

def_level_method(:error)

#error?true, false

If the logger is enabled at error level.

Returns:

  • (true, false)


294
# File 'lib/openhab/log.rb', line 294

def_level_predicate(:error)

#info(msg = nil) { ... } ⇒ void

This method returns an undefined value.

Log a message at info level.

Examples:

logger.info do
  total = Item1.state + Item2.state
  average = total / 2
"Total: #{total}, Average: #{average}"
end

Parameters:

  • msg (Object, nil) (defaults to: nil)

    The log message

Yields:

  • Pass a block to delay generating the log message until it's confirmed that logging is enabled at info level.

Yield Returns:

  • (Object, nil)

    The log message



297
# File 'lib/openhab/log.rb', line 297

def_level_method(:info)

#info?true, false

If the logger is enabled at info level.

Returns:

  • (true, false)


298
# File 'lib/openhab/log.rb', line 298

def_level_predicate(:info)

#inspectString Also known as: to_s

Returns:

  • (String)


264
265
266
# File 'lib/openhab/log.rb', line 264

def inspect
  "#<OpenHAB::Logger #{name}>"
end

#log_exception(exception) ⇒ void

This method returns an undefined value.

Print error and stack trace without calls to internal classes

Parameters:

  • exception (Exception)

    A rescued error



310
311
312
313
314
315
# File 'lib/openhab/log.rb', line 310

def log_exception(exception)
  exception = clean_backtrace(exception)
  error do
    "#{exception.message} (#{exception.class})\n#{exception.backtrace&.join("\n")}"
  end
end

#nameString

The logger name

Returns:

  • (String)


259
260
261
# File 'lib/openhab/log.rb', line 259

def name
  @slf4j_logger.name
end

#trace(msg = nil) { ... } ⇒ void

This method returns an undefined value.

Log a message at trace level.

Examples:

logger.trace do
  total = Item1.state + Item2.state
  average = total / 2
"Total: #{total}, Average: #{average}"
end

Parameters:

  • msg (Object, nil) (defaults to: nil)

    The log message

Yields:

  • Pass a block to delay generating the log message until it's confirmed that logging is enabled at trace level.

Yield Returns:

  • (Object, nil)

    The log message



301
# File 'lib/openhab/log.rb', line 301

def_level_method(:trace)

#trace?true, false

If the logger is enabled at trace level.

Returns:

  • (true, false)


302
# File 'lib/openhab/log.rb', line 302

def_level_predicate(:trace)

#warn(msg = nil) { ... } ⇒ void

This method returns an undefined value.

Log a message at warn level.

Examples:

logger.warn do
  total = Item1.state + Item2.state
  average = total / 2
"Total: #{total}, Average: #{average}"
end

Parameters:

  • msg (Object, nil) (defaults to: nil)

    The log message

Yields:

  • Pass a block to delay generating the log message until it's confirmed that logging is enabled at warn level.

Yield Returns:

  • (Object, nil)

    The log message



295
# File 'lib/openhab/log.rb', line 295

def_level_method(:warn)

#warn?true, false

If the logger is enabled at warn level.

Returns:

  • (true, false)


296
# File 'lib/openhab/log.rb', line 296

def_level_predicate(:warn)