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 name appended to the logger name.

Logging within UI-based rules will have the rule UID appended to the logger.

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

From a UI-based rule with UID ‘rule_uid’

logger.info('Test logging at info')   # 2020-12-03 18:05:41.817 [INFO ] [org.openhab.automation.jrubyscripting.script.rule_uid] - Test logging at info

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

# Enable trace log level for all the rules in this file
logger.level = :trace

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

rule 'foo with id', id: "foo_id" do
  run { logger.trace('Test logging at trace') } # 2020-12-03 18:05:20.903 [TRACE] [org.openhab.automation.jrubyscripting.log_test.rule.foo_id] - 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:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/openhab/log.rb', line 92

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}.#{current_file}"
    name = "#{name}.#{$ctx["ruleUID"]}" if $ctx&.key?("ruleUID")
    return @loggers[name] ||= BiLogger.new(Logger.new(name))
  end

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