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

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2011-07-26
Last reply:
2014-07-29
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: http://docs.python.org/library/logging.html

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
logger.fileHandler("C:\path\to\your\log\file.log")
#set the logging level, DEBUG is the most verbose level
logger.setLevel(logging.DEBUG)

<script you want to run>

try:
    functionInMyScript()
    logger.debug("This is a debug log message")
except
    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
reload(logging)
logger = logging.getLogger("MyLogger")
logging.FileHandler("C:\Amutha\sikuli_logs\file.log","w")
logger.setLevel(logging.DEBUG)
logger.info("We are starting now")
import sample
reload (sample)
import HTMLTestRunner
suite = unittest.TestLoader().loadTestsFromTestCase(sample.UnitTestA)
unittest.TextTestRunner(verbosity=2).run(suite)
outfile = open("C:\Report.html", "a")
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile, title='Test Report', description='This is demo' )
runner.run(suite)
outfile.close()
But it gives an error as

[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\asreeniv\LOCALS~1\Temp\sikuli-tmp4440333128282933731.py", line 6, in
 logging.FileHandler("C:\Amutha\sikuli_logs\file.log","w")
 File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\logging\__init__.py", 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')
    logger.setLevel(logging.DEBUG)
    fh = logging.FileHandler('test.log')
    fh.setLevel(logging.DEBUG)
     ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    logger.addHandler(fh)
    logger.addHandler(ch)
    logger.info(String)
if __name__ == "__main__":
    TT('yes')

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.