Logging and reporting in Sikuli scripts

Created by RaiMan
Keywords:
Last updated by:
RaiMan

This from comment # 4 (Harry Riedinger https://launchpad.net/~temporary22)
of question https://answers.launchpad.net/sikuli/+question/165999
------------------------------

The python logging class is very easy to set up and I recommend using that for logging.

information on python logging is here: http://docs.python.org/library/logging.html

here is a quick, basic example of using logging:

import logging

# when run in IDE with reruns to avoid errors use
import logging; reload(logging)

#create a new logging instance
logger = logging.getLogger("MyLogger")

#set the file to log to
logger.fileHandler(r"C:\path\to\your\log\file.log")

#set the logging level, DEBUG is the most verbose level
logger.setLevel(logging.DEBUG)

logger.info("We are starting now")

def functionInMyScript():
     logger.debug("entering functionInMyScript()")
     # do something that might fail
     logger.debug("leaving functionInMyScript()")

try:
    functionInMyScript()
    logger.debug("Function call succeeded")
except
    logger.error("Function call failed")

___________________________________________________

You can also set up filters and log to different files depending on the log level.
Because logger uses handlers, you can do all this with a single logging instance.

It is a very flexible logging solution.