NOTE: This information applies to the log4cpp version in the Ubitrack library, which is slightly modified compared to the original version. If you somehow googled this page and want to find out how to use log4cpp in general, you are wrong here.
Before you can write a message to the logger, you have to get a reference to a logger first:
#include <log4cpp/Category.hh> static log4cpp::Category& logger( log4cpp::Category::getInstance( "<category name>" ) );
Try to create a logger once and then keep the reference. Creating a new logger for every log message will cause performance problems!
By convention, the name of the catogory contains the namespace/class name, with components separated by ".", e.g.
"Ubitrack.Dataflow.Component"If you want to log operations that happen in a running dataflow, please use the "Ubitrack.Events" prefix which has a lower default log level of NOTICE, e.g.
"Ubitrack.Events.Dataflow.PushConsumer"
log4cpp knows the log levels FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG and TRACE. To write a message, please use the macro LOG4CPP_XXX (replace XXX by log level):
LOG4CPP_INFO( logger, "I have something to say" );
If you want to concatenate multiple variables or output exotic types, you can use the standard streaming operator:
int errorBadness = 8523; LOG4CPP_ERROR( logger, "There was an error of badness " << errorBadness );
Inside a program, you can use the normal log4cpp methods to add layouts and appenders to different loggers. If you are calling Util::initLogging
(which is called by initDataflow
, do the configuration after that.
If you are using an application that uses Ubitrack, such as trackman or the UT console, you can configure logging at runtime by creating a file called log4cpp.conf
in the applications's working directory. There you can specify different appenders (logging destination), log formats and log levels for different categories. An examplary configuration file for most purposes is given below:
#------------------------------------------------------------------------------------- # Logging levels: FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG and TRACE #------------------------------------------------------------------------------------- log4j.rootCategory=ERROR, rolling, console ###win32debug #------------------------------------------------------------------------------------- # Configuration for other categories #------------------------------------------------------------------------------------- log4j.category.Ubitrack.Events.Dataflow=INFO, win32debug #------------------------------------------------------------------------------------- # Configure appender "Console" #------------------------------------------------------------------------------------- log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n #------------------------------------------------------------------------------------- # Configure appender "Win32 Debug" #------------------------------------------------------------------------------------- log4j.appender.win32debug=org.apache.log4j.Win32DebugAppender log4j.appender.win32debug.layout=org.apache.log4j.PatternLayout log4j.appender.win32debug.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n #------------------------------------------------------------------------------------- # Configure appender "Rolling log file" #------------------------------------------------------------------------------------- log4j.appender.rolling=org.apache.log4j.RollingFileAppender log4j.appender.rolling.fileName=messages.log log4j.appender.rolling.append=false log4j.appender.rolling.maxFileSize=10000000 # size in byte log4j.appender.rolling.maxBackupIndex=1 # keep one backup file log4j.appender.rolling.layout=org.apache.log4j.PatternLayout log4j.appender.rolling.layout.ConversionPattern=%d{%H:%M:%S.%l} %6p %20f:%-3l %m %c %n
A very useful appender when developping on Windows is the Win32DebugAppender. It allows to obtain the log inside the Visual Studio console, instead of the ugly Windows shell. To enable it, replace console
by win32debug
in the first line of code in above's configration file.
For a list of available appenders and their parameters, have a look at 3rd\log4cpp\src\PropertyConfiguratorImpl.cpp
.