Central logging module from openalea.

Simple Tutorial

Just to see how easy it is to log something:

from openalea.core import logger
a = 1234
logger.debug("This is an evil value %d"%a)

This examples uses the defaults of the openalea.core.logger module: There is one base logger name "openalea" that logs to a default stream handler (prints out the logs to stderr).

There are more default loggers available:

logger.default_init(level=logger.DEBUG, handlers=["file"])

This line will make logs go to rotating files in ~/.openalea/.

If you're running PyQt4:

logger.default_init(level=logger.DEBUG, handlers=["qt"])

Will make your logs go to a QStandardItemModel that you can get this way:

itemModel = logger.get_handler("qt")

You can directly use it in a QListView.

Per-package loggers

The previous example used the central OpenAlea logger. However, we recommend you use a specific logger for your package, eg. Openalea.MTG:

from openalea.core import logger
mylogger = logger.get_logger("Openalea.MTG")
[...]
mylogger.debug("Execution reached this place...")

This will print nothing as mylogger is attached to no handler. You can attach it to Openalea's handlers if they are any available:

logger.connect_loggers_to_handlers(mylogger, logger.get_handler_names())
mylogger.debug("Execution reached this other place...")

This will make every handler registered to OpenAlea receive the log.

Or you can connect it to your own handlers (In this case you can completely bypass Openalea.)

import logging
mylogger.addHandler(logging.FileHandler("path_to_file", mode="w"))

Indeed, mylogger is a logger.PatchedPyLogger slightly derived from standard logging.Logger.

You can disconnect loggers and handlers::
logger.disconnect_loggers_from_handlers(mylogger, logger.get_handler_names())

This will disconnect all handlers know by openalea from mylogger.

Logging levels

Logging typically happens at different levels, from the less important to the worst case. You have access to 5 default logging levels: DEBUG, INFO, WARNING, ERROR and CRITICAL.

Through the openalea.core.logger module:

logger.debug(str)
logger.info(str)
logger.warning(str)
logger.error(str)
logger.critical(str)

Or a custom logger:

mylogger = logger.get_logger("Openalea.MTG")
mylogger.debug(str)
mylogger.info(str)
mylogger.warning(str)
mylogger.error(str)
mylogger.critical(str)

You can also send logs of arbitrary levels using the log(level (int), msg (str)) module function (or mylogger.log(int, str)).

Logger objects can be set to ignore logs that happen at levels lower than a chosen one. The same goes for handlers.

In openalea.core.logger, you can set all loggers and handlers to have the same level:

logger.set_global_logger_level(logger.INFO) #DEBUG logs will not be sent to handlers
logger.set_global_handler_level(logger.ERROR) #DEBUG and ERROR logs will be ignored by handlers.

Of course you can do this more selectively:

lg = logger.get_logger("Openalea.MTG")
# lg will not send logs below the CRITICAL level:
lg.setLevel(logger.CRITICAL)

hd = logger.get_handler("qt")
# hd will not process logs below INFO level.
hd.setLevel(logger.INFO)
class core.logger.LoggerOffice(level=10)[source]

Bases: object

This class behaves as the central registry of loggers and handlers for Openalea. This way, the application can query information about them.

add_handler(name, handler)[source]
add_logger(name)[source]
connect_loggers_to_handlers(loggers, handlers, noDisconnect=False)[source]
disconnect_loggers_from_handlers(loggers, handlers)[source]
get_date_format()[source]
get_default_logger()[source]
get_format()[source]
get_handler(name)[source]
get_handler_names()[source]
get_logger(name)[source]
get_logger_names()[source]
iter_handlers()[source]
iter_loggers()[source]
make_default_logger(handlers=None)[source]
set_default_logger(logger)[source]
set_defaults(level=40, handlers=None)[source]
set_global_handler_level(level)[source]
set_global_logger_level(level)[source]
class core.logger.PatchedPyLogger(name, level=0)[source]

Bases: logging.Logger

Patched Logger that identifies correctly the origin of the logger relative to this module

findCaller()[source]

Find the stack frame of the caller so that we can note the source file name, line number and function name.

core.logger.connect_loggers_to_handlers(loggers, handlers)[source]

Connects loggers to handlers. Each argument can be a single item or a list of them. Each item can be the name of the logger/handler or an instance of that.

core.logger.critical(msg)[source]

Send a critical level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.debug(msg)[source]

Send a debug level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.default_init(level=40, handlers=['file', 'stream'])[source]

Configure the LoggerOffice with a default openalea logger and handlers named in handlers. The latter is a list of strings from "qt", "file", "stream".

  • "qt" is only available if PyQt4 is installed. Logs will go to a QStandardItemModel.
  • "file" creates a rotating file handler. Logs are stored in "~/.openalea/log.log.X" files X get incremented every day. Beyond 20 days olds files get deleted.
  • "stream" logs to stderr.
core.logger.disconnect_loggers_from_handlers(loggers, handlers)[source]

Disconnect loggers from handlers. Each argument can be a single item or a list of them. Each item can be the name of the logger/handler or an instance of that.

core.logger.error(msg)[source]

Send a error level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.get_handler(name)[source]

Returns the handler called name. It will always return the same handler for the same name.

core.logger.get_handler_names()[source]

Returns a list of handler names known by OpenAlea's LoggerOffice

core.logger.get_logger(name)[source]

Returns the logger called name. It will always return the same logger for the same name.

core.logger.get_logger_names()[source]

Returns a list of logger names known by OpenAlea's LoggerOffice

core.logger.info(msg)[source]

Send a info level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.log(level, msg)[source]

Send an arbitrary level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.set_global_handler_level(level)[source]

Set level of all known handlers to level.

core.logger.set_global_logger_level(level)[source]

Set level of all known loggers to level.

core.logger.warning(msg)[source]

Send a warning level msg to openalea's default logger. Handlers may or may not be connected yet.

core.logger.defaultHandlerNames = ['file', 'stream']

List of default handler names:

Table Of Contents

Previous topic

<no title>

Next topic

<no title>

This Page