Central logging module from openalea.
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.
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.
This will disconnect all handlers know by openalea from mylogger.
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)
Bases: object
This class behaves as the central registry of loggers and handlers for Openalea. This way, the application can query information about them.
Bases: logging.Logger
Patched Logger that identifies correctly the origin of the logger relative to this module
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.
Send a critical level msg to openalea's default logger. Handlers may or may not be connected yet.
Send a debug level msg to openalea's default logger. Handlers may or may not be connected yet.
Configure the LoggerOffice with a default openalea logger and handlers named in handlers. The latter is a list of strings from "qt", "file", "stream".
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.
Send a error level msg to openalea's default logger. Handlers may or may not be connected yet.
Returns the handler called name. It will always return the same handler for the same name.
Returns a list of handler names known by OpenAlea's LoggerOffice
Returns the logger called name. It will always return the same logger for the same name.
Returns a list of logger names known by OpenAlea's LoggerOffice
Send a info level msg to openalea's default logger. Handlers may or may not be connected yet.
Send an arbitrary level msg to openalea's default logger. Handlers may or may not be connected yet.
Send a warning level msg to openalea's default logger. Handlers may or may not be connected yet.
List of default handler names: