Ask Your Question

Accessing Log Level in roscpp

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

sebastianh gravatar image

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

Eric Perko gravatar image


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.



edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2012-12-14 05:14:05 -0600

clemens gravatar image

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

Hi Sebastian!

You can access the logger like this:

log4cxx::LoggerPtr my_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);

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


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 -0600 )edit

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

xinwf gravatar image

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

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;
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

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

sebastianh gravatar image

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

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


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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


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

Seen: 512 times

Last updated: Aug 12 '19