0
$\begingroup$

Rosanswers logo

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


Originally posted by sebastianh on ROS Answers with karma: 70 on 2012-12-13

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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})

Originally posted by clemens with karma: 71 on 2012-12-14

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Dirk Thomas on 2014-01-14:
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).

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by xinwf with karma: 11 on 2019-08-12

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.