ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Accessing Log Level in roscpp

asked 2012-12-13 05:53:23 -0500

sebastianh gravatar image

updated 2012-12-14 07:13:01 -0500

Eric Perko gravatar image

Hi,

I am wondering how to access the current log level of a certain logger in ROS during runtime. I would like to write something like this:

if (Logger.getLogger("log4j.logger.ros.my_package").getLevel() == Level::DEBUG)
{
  // some debugging code
}

I know that this is possible in log4j in Java, but I was unable to find out how to access the loggers in roscpp. The reason behind this is that I would like to perform certain calculations only when being in DEBUG mode.

Thanks!

Sebastian

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
3

answered 2012-12-14 05:14:05 -0500

clemens gravatar image

updated 2012-12-14 05:19:58 -0500

Hi Sebastian!

You can access the logger like this:

log4cxx::LoggerPtr my_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
my_logger->getLevel();
my_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);

You then also have to link against the log4cxx library:

find_library(LOG4CXX_LIBRARY log4cxx)
target_link_libraries(some_target ${LOG4CXX_LIBRARY})
edit flag offensive delete link more

Comments

You should not use `g_level_lookup` to map the ROS debug level to the log4cxx log level. Instead use the log4cxx level directly. Besides the package must specify a dependency on log4cxx in its package manifest and the cpp file must include the necessary log4cxx header(s).

Dirk Thomas gravatar image Dirk Thomas  ( 2014-01-14 13:45:25 -0500 )edit
1

answered 2019-08-12 21:51:09 -0500

xinwf gravatar image

updated 2019-08-12 21:53:06 -0500

I have tested the code segments about the two existed answers on my ubuntu 16.04 with ros kinetic version, it failed. After my exploration, now the right way to access logger level is in the following:

std::map< std::string, ros::console::levels::Level> logger;
ros::console::get_loggers(logger);
std::cout << "current logger: " << logger[ROSCONSOLE_DEFAULT_NAME] << "\n";

And a tip to all of you, the object logger can't be reused, it means if you want to get logger level after you have changed it, you must create a new object with std::map< std::string, ros::console::levels::Level>, hope this can help you.

edit flag offensive delete link more
0

answered 2012-12-14 05:26:20 -0500

sebastianh gravatar image

updated 2012-12-14 05:27:04 -0500

Thanks Clemens, that works!

Just one addition to Clemens' post: If you want to access a custom logger, you have to remove the "log4j.logger" part. The following is doing exactly what I want:

log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("ros.my_package");
if (logger && logger->getLevel() == log4cxx::Level::getDebug()) {
   // debugging code
}
edit flag offensive delete link more

Comments

If your question is answered, please mark the correct answer as correct and close the question. It is also advised that you do not add another answer if you are following up on someone's answer, but rather post a comment. I realize, however, you might not be able to do that due to karma limits...

georgebrindeiro gravatar image georgebrindeiro  ( 2012-12-14 05:59:56 -0500 )edit

Question Tools

Stats

Asked: 2012-12-13 05:53:23 -0500

Seen: 953 times

Last updated: Aug 12 '19