problems using Python logging module

Asked by Truptimayee Rath on 2011-07-26

What is the logging and Reporting facilities provided by Project Sikuli automation tool.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

Sikuli prints some messages about found objects (matches) and actions (typing and clicking) to stdout. The message level can be adjusted.

There is no configurable logging or reporting. You have to set it up on your own using the standard features of Python or Java.
(E,g, no feature to save screenshots together with valuable status information.)

We are planning to use the tool 'Project Sikuli' automation tool .But Logging and reporting are definitely a important feature of an automation tool.Can you please elaborate how we can achieve this using Java or Python.

RaiMan (raimund-hocke) said : #3

Both Java and Python provide modules/classes that allow to implement logging.

In Sikuli, you set up some workflows of "finding and clicking" (roughly spoken). So you have to integrate this logging feature into your scripted workflow accordingly.

Depending on the scope of usage, this can be hard coded in every script or set up as private modules/classes, that are imported and used in the scripts.

The reporting is something different. It usually is some post processing based on the logged information. This is totally up to you, wether you use existing frameworks like Hudson or program it on your own.

E.g. you might set up all your automation using the unit test frameworks available in Python and Java. Based on this there are many other possibilities available out in the net to achieve what you want.

Harry Readinger (temporary22) said : #4

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

information on python logging is here:

here is a quick, basic example of using logging:

import logging

#create a new logging instance
logger = logging.getLogger("MyLogger")
#set the file to log to
#set the logging level, DEBUG is the most verbose level

<script you want to run>

    logger.debug("This is a debug log message")
    logger.error("this is an error message in your log")


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

Hi Raiman,

         I a using HTMLTestrunner for reporting.
In my situation , I have to rerun the test case several times. Everytime, when I rerun, In the report , it always updates the 1st table (Report file is opened in "append" mode) . In this case, the report is not correct (i.e), it is not adding the failures to the correct row. Could you please give me the solution for this?

For logs, where to add above given lines for logging?

I added under, maintest.sikuli

import unittest
import logging
logger = logging.getLogger("MyLogger")
logger.setLevel(logging.DEBUG)"We are starting now")
import sample
reload (sample)
import HTMLTestRunner
suite = unittest.TestLoader().loadTestsFromTestCase(sample.UnitTestA)
outfile = open("C:\Report.html", "a")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report', description='This is demo' )
But it gives an error as

[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\asreeniv\LOCALS~1\Temp\", line 6, in
 File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\logging\", line 770, in __init__
IOError: (13, 'Permission denied', 'C:\\Amutha\\sikuli_logs\x0cile.log')

RaiMan (raimund-hocke) said : #6

this is a known problem with the logging module on rerun in the IDE.

Has nothing to do with the HTMLTestRunner.

at the end of your script you have to shutdown the logger correctly.

BTW: is it by intention, that you run your suite twice each run: first with the TextTestRunner and then again with the HTMLTestRunner?

Thanks for the reply Raiman.

While running ,I Commented the "TextTestRunner".

Tried with XMLTestRunner. If I ran it for 2 times (same script), it generates the report.

But , if ran it for 5 times. The report is blank. Why so?

Is this also due to logging problem?

Do you know any other reporting tool that can integrate with sikuli, to generate correct report for several runs?

RaiMan (raimund-hocke) said : #8

Such complex constructions should generally be run from command line, to have a clean initialization of the internally used Jython interpreter. This at least is true for production runs and repetitive runs.

You can still use the IDE to mange your scripts and make test runs, to assure that the Sikuli features are working.

The IDE internally uses the same interpreter on subsequent script runs. This helps to avoid the 3 to 5 seconds startup time for a new interpreter instance, but brings in some oddities caused by problems in the current Jython or in the used modules.

Huang Jie (jane19900203) said : #9

Hi Raiman,

I have a question about import logging under Sikuli environment. But this issue cannot be reproduced under python environment. please my program as below, the run result will repeat several times but I just run one time. I think the expected result is one log.
Could you please give me the solution for this?

import logging

def TT(String):
    logger = logging.getLogger('mylogger')
    fh = logging.FileHandler('test.log')
     ch = logging.StreamHandler()
     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
if __name__ == "__main__":

Run result:
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes
2014-07-29 11:11:51,969 - mylogger - INFO - yes

Can you help with this problem?

Provide an answer of your own, or ask Truptimayee Rath for more information if necessary.

To post a message you must log in.