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



253
254
255
# File 'lib/openhab/log.rb', line 253

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

Class Method Details

.eventsLogger

The events logger (events.log)

Returns:



169
170
171
# File 'lib/openhab/log.rb', line 169

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

.gem_rootLogger

The root logger for this gem

Returns:



163
164
165
# File 'lib/openhab/log.rb', line 163

def gem_root
  Log.logger(PREFIX)
end

.rootLogger

The root logger (all of openHAB)

Returns:



157
158
159
# File 'lib/openhab/log.rb', line 157

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



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

def_level_method(:debug)

#debug?true, false

If the logger is enabled at debug level.

Returns:

  • (true, false)


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

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



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

def_level_method(:error)

#error?true, false

If the logger is enabled at error level.

Returns:

  • (true, false)


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

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



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

def_level_method(:info)

#info?true, false

If the logger is enabled at info level.

Returns:

  • (true, false)


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

def_level_predicate(:info)

#inspectString Also known as: to_s

Returns:

  • (String)


234
235
236
# File 'lib/openhab/log.rb', line 234

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



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

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)


229
230
231
# File 'lib/openhab/log.rb', line 229

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



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

def_level_method(:trace)

#trace?true, false

If the logger is enabled at trace level.

Returns:

  • (true, false)


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

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



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

def_level_method(:warn)

#warn?true, false

If the logger is enabled at warn level.

Returns:

  • (true, false)


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

def_level_predicate(:warn)