Handling Logging in Custom Library when Running inside and outside RobotFramework

2,426 views
Skip to first unread message

Joseph Lorenzini

unread,
Nov 19, 2015, 12:57:35 PM11/19/15
to robotframework-users
Hi all,

In my custom libraries, I am taking advantage of the public logging api:

from robot.api import logger

This works great and does exactly what I want it to do when the library is used in RobotFramework. However, when the script is run outside of the framework. I get this:

No handlers could be found for logger "RobotFramework"

I understand why this is happening but i am not sure what the best way to handle it is. When a function is called that uses RF logger and RF is not running, then messages are redirected to the standard Python logging module using the logger named RobotFramework. So obviously I need to create one or more handlers for the RF logger in that case. 

Here's the question. I only want to run the code to create those handlers if and only if the script is not running inside robot framework. Is there any way to introspect or figure that out? In other words, I want to write a function that says something like:

if not running in robotframework:
    create logging handlers.

Thanks,
Joe 


Tatu Aalto

unread,
Nov 21, 2015, 6:13:25 AM11/21/15
to jal...@gmail.com, robotframework-users
Ugh

I did wonder similar type of problem, not so long time ago. I did end up writing my own logging class, which did know is robot running or nor. If it was not running, it would perform logging normally but if robor was running, it would use robot logger to perform logging.

-Tatu
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Joseph Lorenzini

unread,
Nov 22, 2015, 9:00:26 AM11/22/15
to Tatu Aalto, robotframework-users
Hi Tatu,

I would be curious to see the code that figured if you were running in robot framework or not. Would you mind sharing?

Thanks,
Joe 

Tatu Aalto

unread,
Nov 22, 2015, 9:24:49 AM11/22/15
to Joseph Lorenzini, robotframework-users

Ugh

I can share that part of the code. I am away from my PC but later today or Monday

-Tatu
Send from my mobile

Tatu Aalto

unread,
Nov 23, 2015, 1:47:51 PM11/23/15
to robotframework-users, jal...@gmail.com
Ugh

In simplified way, I did like this:
from robot.libraries.BuiltIn import BuiltIn
from robot.libraries.BuiltIn import RobotNotRunningError

rf_running = False
try:
    BuiltIn().get_variables()
    rf_running = True
except RobotNotRunningError:
    rf_running = False

And based on the rf_running, it was relatively easy to decide where the logging should go. Then the only problem was to map the RF log/error level to correct ones in Python side, but because my need was to log to stdout if RF was not running, I did do: 
if not rf_running:
    print message
else:
    logger.write(message, level)

I hope this helps.

-Tatu
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.

Joseph Lorenzini

unread,
Nov 23, 2015, 3:55:01 PM11/23/15
to Tatu Aalto, robotframework-users
That's perfect. It's exactly what i was looking for.

Thanks,
Joe

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.

Joseph Lorenzini

unread,
Dec 1, 2015, 3:07:48 PM12/1/15
to Tatu Aalto, robotframework-users
Hi Tatu,

I actually have run into an interesting problem. This code works fine if I do not have a listener that imports the BuiltIn class. However, if I do have a listener that imports the BuiltIn class, then the code always raises RobotNotRunningError exception when I am testing to see if I am in the robot framework or not.

Any idea why that might be? Seems like I might be hitting some kinda of esoteric import bug in the framework.

Joe 

Tatu Aalto

unread,
Dec 1, 2015, 3:24:15 PM12/1/15
to Joseph Lorenzini, robotframework-users

Ugh

Sorry, I haven't seen that problem. I use listeners but I don't use BuiltIn from the listener. Would you have an example which one could investigate?

-Tatu
Send from my mobile

Joseph Lorenzini

unread,
Dec 1, 2015, 4:45:32 PM12/1/15
to Tatu Aalto, robotframework-users
Here is an example listener:


Here's how its invoked.

pybot -V global_vars.py --exitonerror --randomize all --listener listeners.TestCaseStatus

Please let me know if you need additional details.

Tatu Aalto

unread,
Dec 2, 2015, 1:23:33 AM12/2/15
to Joseph Lorenzini, robotframework-users

Ugh

What you are trying to do with the listener, is not possible. Listeners can only listen the test execution status but, currently, listeners cannot modify the test execution status in any way. There has been multiple requests to change this design (there should be issue describing the planned change in the GitHub) but that has not been implemented.

To do, what your listener is trying to do, you need to write a keyword and call that keyword in every test case teardown.

-Tatu
Send from my mobile

Joseph Lorenzini

unread,
Dec 2, 2015, 12:48:16 PM12/2/15
to Tatu Aalto, robotframework-users
Hi Tatu,

I think there might be misunderstanding here. I am not modifying the test case execution status. Using the builtin library, I am only tagging a test case based on whatever the test case status happens to me. Whether this should work is an open question. However, it has in fact worked for me. Now if this is an unsupported mode that can cause other problems .....then fair enough but I didn't see any documentation that indicated this would result in undesirable behavior.

Joe 

Tatu Aalto

unread,
Dec 3, 2015, 1:28:55 AM12/3/15
to Joseph Lorenzini, robotframework-users

Ugh

Listener only listens and one should not be able do have any interaction from listener to Robot Framework. If tagging happens to work, you have stumbled to an undocumented feature, somebody could considered it a bug. Will the feature remain and is there planned changes in this area in the future, I don't know. That is more a question to the RF development team.

-Tatu
Send from my mobile

Joseph Lorenzini

unread,
Dec 3, 2015, 8:15:57 AM12/3/15
to Tatu Aalto, robotframework-users
Hmmm, seems like this PR is what you are talking about which may get merged into RF 3.0.


Thanks for the info. I was unaware of this restriction on listeners.

Joe 

Jyrki Lindroos

unread,
May 13, 2016, 3:19:08 AM5/13/16
to robotframework-users, jal...@gmail.com

Hi guys,

I have done similar custom logger for libraries that enabled us to log correctly from libraries no matter whether the Robot is installed or not, nor if the library is executed from Robot or not.


Now I have realized that there is no real need for this custom library as using the Python logging API in libraries the logging will be added also to the Robot log due to a Robothandler that is added to the logging system.


Now I want to create a configuration where libraries will add logging to the console and to a file when they are executed without Robot and when executed from Robot the logging would go to Robot log and to the file. Logging to the file is mainly there because of the inability to log from the threads to the Robot Log.


This all worked fine while I was initializing the loggers from code but once I started using json config files with “config.dictConfig” the logging to the Robot Log stopped working. This is because the ‘dictConfig’ will clear the RobotHandler from the root logger, even when 'disable_existing_loggers' is set to false.


Any ideas what would be the best way to fix this?


The problem is that I’m initializing my loggers in the top suite setup which is too late.


I should do it before Robot starts. Best way to do this? Are there any hooks for this kind of purpose?


or  should I just try to save the root logger handlers before calling ‘dictConfig’ and then restore them afterwards?


Thanks,

Jyrki

Reply all
Reply to author
Forward
0 new messages