issues when using Python logging module in sikuli

Asked by linmiaoling

Hi all,
i have issues when using Python logging module in sikuli

run following script in sikuli ide :

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.addHandler(r"D:\\TestingReports\\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
     print("run function success!!")
     logger.debug("leaving functionInMyScript()")

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

but i get error:
[error] script [ log_1 ] stopped with error in line 15
[error] AttributeError ( 'str' object has no attribute 'level' )
[error] --- Traceback --- error source first line: module ( function ) statement 1319: __init__ ( callHandlers ) if record.levelno >= hdlr.level:
1280: __init__ ( handle ) self.callHandlers(record)
1270: __init__ ( _log ) self.handle(record)
1151: __init__ ( info ) self._log(INFO, msg, args, **kwargs)
[error] --- Traceback --- end --------------

What should i do ?
thanks for the help!

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
TestMechanic (ndinev) said :
#1

Try to change

logger.setLevel(logging.DEBUG)

to

logger.setLevel(10)

See here - https://docs.python.org/2/library/logging.html#logging-levels

Revision history for this message
Manfred Hampl (m-hampl) said :
#2

I assume that your usage of the logging module is not correct.

As far as I know logger.addHandler() needs a handler as its parameter, and not a file name.

Maybe you can use https://docs.python.org/2.3/lib/node304.html as a template.

Can you help with this problem?

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

To post a message you must log in.