Module: OpenHAB::Log

Defined in:
lib/openhab/log.rb,
lib/openhab/log.rb

Overview

Provides access to the openHAB logging facilities using Ruby logging methods

Logging is available everywhere through the #logger object.

The logging prefix is ‘org.openhab.automation.jrubyscripting`. Logging within file-based rules will have the name of the file appended to the logger name. Logging inside of a rule will have the id of the rule appended to the logger name. Any classes will have the full class anem appended to the logger name.

Examples:

The following entries are in a file named ‘log_test.rb’

logger.trace('Test logging at trace') # 2020-12-03 18:05:20.903 [TRACE] [org.openhab.automation.jrubyscripting.log_test] - Test logging at trace
logger.debug('Test logging at debug') # 2020-12-03 18:05:32.020 [DEBUG] [org.openhab.automation.jrubyscripting.log_test] - Test logging at debug
logger.warn('Test logging at warn')   # 2020-12-03 18:05:41.817 [WARN ] [org.openhab.automation.jrubyscripting.log_test] - Test logging at warn
logger.info('Test logging at info')   # 2020-12-03 18:05:41.817 [INFO ] [org.openhab.automation.jrubyscripting.log_test] - Test logging at info
logger.error('Test logging at error') # 2020-12-03 18:06:02.021 [ERROR] [org.openhab.automation.jrubyscripting.log_test] - Test logging at error

The following entries are in a file named ‘log_test.rb’

rule 'foo' do
  run { logger.trace('Test logging at trace') } # 2020-12-03 18:05:20.903 [TRACE] [org.openhab.automation.jrubyscripting.foo] - Test logging at trace
  on_load
end

A log entry from inside a class

class MyClass
  def initialize
    logger.trace("hi!") # 2020-12-03 18:05:20.903 [TRACE] [org.openhab.automation.jrubyscripting.MyClass] - hi!
  end
end

Class Method Summary collapse

Class Method Details

.logger(object) ⇒ Logger

Retrieve a OpenHAB::Logger for a particular object.

Parameters:

  • object (Module, String)

    Object the logger is for, or explicit name of the logger.

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/openhab/log.rb', line 79

def logger(object)
  case object
  when Module
    name = Logger::PREFIX
    klass = java_klass(object)
    name += ".#{klass.name.gsub("::", ".")}" if klass.name
  when String
    name = object
  when :main
    name = "#{Logger::PREFIX}.#{rules_file.tr_s(":", "_").gsub(/[^A-Za-z0-9_.-]/, "")}"
    name = "#{name}.#{$ctx["ruleUID"]}" if $ctx&.key?("ruleUID")
    return @loggers[name] ||= BiLogger.new(Logger.new(name))
  end

  @loggers[name] ||= Logger.new(name)
end