Howto: Configure the logger from a program¶
The following example shows how to configure the logger from a program (without the need for a log config file):
#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/oflog.h"
int main(int argc, char *argv[])
{
/* enable verbose mode (info log level) */
OFLog::configure(OFLogger::INFO_LOG_LEVEL);
/* ... */
}
Log Levels¶
The following log levels are available. Every log level in this list includes the details of the above level (e.g. the WARN_LOG_LEVEL also outputs all errors and thus also fatal errors).- FATAL_LOG_LEVEL: Only fatal error messages are printed.
- ERROR_LOG_LEVEL: All errors are printed.
- WARN_LOG_LEVEL: Warnings are printed.
- INFO_LOG_LEVEL: The most important processing information is printed.
- DEBUG_LOG_LEVEL: This is a pretty verbose logging level that is a good starting point for getting more information what actually happens in the code and to identify corresponding problems.
- TRACE_LOG_LEVEL: This is the most verbose logging level and outputs many processing details.
Redirecting log output to a file¶
If the log output should be directed to a file or another log pattern should be used, we need some more lines of code:
#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/fileap.h"
int main(int argc, char *argv[])
{
/* specify log pattern */
OFunique_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout("%D{%Y-%m-%d %H:%M:%S.%q} %5p: %m%n"));
/* Denote that a log file should be used that is appended to. The file is re-created every
time the code gets to this point.
*/
log4cplus::SharedAppenderPtr logfile(new log4cplus::FileAppender("example.log"));
logfile->setLayout(OFmove(layout));
/* make sure that only the file logger is used */
log4cplus::Logger log = log4cplus::Logger::getRoot();
log.removeAllAppenders();
log.addAppender(logfile);
log.setLogLevel(OFLogger::INFO_LOG_LEVEL);
/* ...*/
}
The above code re-creates the example.log file every time the code is encountered, i.e. if the complete program is restarted. If you want to append a file without deleting it, use the following line instead
log4cplus::SharedAppenderPtr logfile(new log4cplus::FileAppender("example.log", STD_NAMESPACE ios::app));
No output at all (quiet mode)¶
If all log output should be discarded:
#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/nullap.h"
int main(int argc, char *argv[])
{
log4cplus::SharedAppenderPtr nullapp(new log4cplus::NullAppender());
/* make sure that only the null logger is used */
log4cplus::Logger log = log4cplus::Logger::getRoot();
log.removeAllAppenders();
log.addAppender(nullapp);
/* ...*/
}
Note¶
Please note that the above sample code requires DCMTK 3.5.5 (20091222) or newer.
Please also note that the namespace has changed after the DCMTK 3.6.0 release from log4cplus
to dcmtk::log4cplus
with this commit.
Please also note that older versions of DCMTK (prior to commit 3a54504640c35a) required the use of the no longer available OFauto_ptr instead of OFunique_ptr.