Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rospy] Write log for class method with class name for rospy #877

Closed

Conversation

wkentaro
Copy link
Contributor

@wkentaro wkentaro commented Aug 25, 2016

For #875

$ export ROSCONSOLE_FORMAT='[${severity}] [${time}]: [${node}] [${function}] ${message}'
$ python spam.py
[INFO] [1472155704.567361]: [/a] [<module>] a
[WARN] [1472155704.568193]: [/a] [<module>] a
[FATAL] [1472155704.568894]: [/a] [<module>] a
[ERROR] [1472155704.569581]: [/a] [<module>] a
[INFO] [1472155704.570351]: [/a] [main] a
[WARN] [1472155704.571100]: [/a] [main] a
[FATAL] [1472155704.571878]: [/a] [main] a
[ERROR] [1472155704.572626]: [/a] [main] a
[INFO] [1472155704.573439]: [/a] [Test::__init__] a
[WARN] [1472155704.574176]: [/a] [Test::__init__] a
[FATAL] [1472155704.574937]: [/a] [Test::__init__] a
[ERROR] [1472155704.575672]: [/a] [Test::__init__] a
$ cat spam.py

import rospy

rospy.init_node('a')

rospy.loginfo('a')
rospy.logwarn('a')
rospy.logfatal('a')
rospy.logerr('a')

def main():
    rospy.loginfo('a')
    rospy.logwarn('a')
    rospy.logfatal('a')
    rospy.logerr('a')
main()

class Test(object):
    def __init__(self):
        rospy.loginfo('a')
        rospy.logwarn('a')
        rospy.logfatal('a')
        rospy.logerr('a')
Test()

```
$ export ROSCONSOLE_FORMAT='[${severity}] [${time}]: [${node}] [${function}] ${message}'
```

```
$ python spam.py
[INFO] [1472155704.567361]: [/a] [<module>] a
[WARN] [1472155704.568193]: [/a] [<module>] a
[FATAL] [1472155704.568894]: [/a] [<module>] a
[ERROR] [1472155704.569581]: [/a] [<module>] a
[INFO] [1472155704.570351]: [/a] [main] a
[WARN] [1472155704.571100]: [/a] [main] a
[FATAL] [1472155704.571878]: [/a] [main] a
[ERROR] [1472155704.572626]: [/a] [main] a
[INFO] [1472155704.573439]: [/a] [Test::__init__] a
[WARN] [1472155704.574176]: [/a] [Test::__init__] a
[FATAL] [1472155704.574937]: [/a] [Test::__init__] a
[ERROR] [1472155704.575672]: [/a] [Test::__init__] a
```

```
$ cat spam.py

import rospy

rospy.init_node('a')

rospy.loginfo('a')
rospy.logwarn('a')
rospy.logfatal('a')
rospy.logerr('a')

def main():
    rospy.loginfo('a')
    rospy.logwarn('a')
    rospy.logfatal('a')
    rospy.logerr('a')
main()

class Test(object):
    def __init__(self):
        rospy.loginfo('a')
        rospy.logwarn('a')
        rospy.logfatal('a')
        rospy.logerr('a')
Test()
```
@dirk-thomas
Copy link
Member

dirk-thomas commented Aug 30, 2016

This patch seems to not resolve the line number. Doesn't this breaks existing behavior when ${line} is being used.

Please also add a test for the new behavior.

func_name = frame.f_code.co_name
try:
class_name = frame.f_locals['self'].__class__.__name__
func_name = '%s::%s' % (class_name, func_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%s::%s is not the Python way to reference a method of a class. Please use %s.%s instead.

@wkentaro
Copy link
Contributor Author

Fixed.

pass
return file_name, lineno, func_name

logging.setLoggerClass(RospyLogger)
Copy link
Member

@dirk-thomas dirk-thomas Aug 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ensure not to overwrite custom logger classes already being set by user code. See https://docs.python.org/2/library/logging.html#logging.getLoggerClass on how to achieve this.

Could you please also cover that case in the test.

@wkentaro
Copy link
Contributor Author

wkentaro commented Sep 1, 2016

Fixed.

@dirk-thomas
Copy link
Member

Please use the approach described in the link from #877 (comment) since it also allows to customize the logger if a user defined class has been set before.

@wkentaro
Copy link
Contributor Author

wkentaro commented Sep 1, 2016

This should ensure not to overwrite custom logger classes already being set by user code. See https://docs.python.org/2/library/logging.html#logging.getLoggerClass on how to achieve this.

But below code overwrites the custom logger class. Is that ok?

class MyLogger(logging.getLoggerClass()):
    # ... override behaviour here

@dirk-thomas
Copy link
Member

Yes, but it creates the subclass not of the original logger but of the currently set logger class which could be set by a user before. So it will maintain all other changes from the custom class (except the overridden functionality).

@wkentaro wkentaro force-pushed the rosconsole-format-func-with-class branch from 484af79 to 901290a Compare September 1, 2016 19:50
@wkentaro wkentaro force-pushed the rosconsole-format-func-with-class branch from 901290a to 76835b0 Compare September 1, 2016 19:57
@wkentaro
Copy link
Contributor Author

wkentaro commented Sep 1, 2016

Ok, fixed.

@dirk-thomas
Copy link
Member

The patch currently fails one of the new tests.

@wkentaro
Copy link
Contributor Author

wkentaro commented Sep 3, 2016

Fixed.

@wkentaro wkentaro closed this Sep 4, 2016
@wkentaro wkentaro reopened this Sep 4, 2016
@@ -0,0 +1,133 @@
# Software License Agreement (BSD License)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file in a subfolder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shouldn't repeat all the tests from the other file but only check the UserCustomLogger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shouldn't repeat all the tests from the other file but only check the UserCustomLogger.

Ok I will fix it.

@wkentaro
Copy link
Contributor Author

wkentaro commented Oct 28, 2016

Well, .. it still fails

% nosetests test/    
........ERROR: Invalid remapping argument ':=:='
ERROR: Invalid remapping argument 'foo:=bar:=baz'
..........invalid ROS_IP (must be a valid IPv4 or IPv6 address)
invalid ROS_IP (must be a valid IPv4 or IPv6 address)
.invalid ROS_IP (must be a valid IPv4 or IPv6 address)
.invalid ROS_IP (must be a valid IPv4 or IPv6 address)
..............................F..
======================================================================
FAIL: test.test_roslogging_user_logger.test_roslogging_user_logger
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/wkentaro/ros/ros_comm_ws/src/ros_comm/tools/rosgraph/test/test_roslogging_user_logger.py", line 112, in test_roslogging_user_logger
    assert_regexp_matches(log_actual, log_expected)
AssertionError: Regexp didn't match: 'INFO 127.0.0.1 Hello world. [0-9]*\\.[0-9]* [0-9]* rosout <filename> <lineno> <func_name> /unnamed [0-9]*\\.[0-9]*' not found in 'INFO Hello world. 1477682768.908821 140199866681152 rosout /home/wkentaro/ros/ros_comm_ws/src/ros_comm/tools/rosgraph/test/test_roslogging_user_logger.py 93 test_roslogging_user_logger /unnamed 1477682768.908852'

----------------------------------------------------------------------
Ran 53 tests in 0.531s

FAILED (failures=1)

% nosetests test/test_roslogging_user_logger.py
.
----------------------------------------------------------------------
Ran 1 test in 0.079s

OK

@dirk-thomas
Copy link
Member

Addressed by #1043.

@dirk-thomas dirk-thomas closed this Jul 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants