How to direct logs to multiple log files (destinations) – log4cpp
log4cpp can not only be used to log the events to a log file or standard output, but also helps you to log events to multiple destinations. For this purpose, either you can add multiple appenders to a single category or make use of multiple Categories. We will explore the first options. With this option, you can direct the same log messages to multiple files
A Category class has the following functions
void log4cpp::Category::addAppender ( Appender & appender) void log4cpp::Category::setAdditivity ( bool additivity )
These functions can be used to add multiple appenders to a category.
addAppender is used to add an Appender to a Category. setAdditivity is used to decide whether you want to support multiple log files by the same category. If the value is set to true, a category can support multiple log files, but if the value is set to false, the value of the appender by the latest addAppender is used as the appender.
For supporting multiple log files, you must have
- Multiple Appender or log destinations
- Each Appender must have its own layout. Multiple Appenders can have the same layout, but they must initialize them separately.
- The additivity flag must be true. Once set to true, the appenders can be added.
Let’s check the following program
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <log4cpp/category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
using namespace std;
#define LOGFILE1 "/home/user/test1.log"
#define LOGFILE2 "/home/user/test2.log"
int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender1 = new log4cpp::FileAppender("FileAppender1",LOGFILE1);
log4cpp::Appender *appender2 = new log4cpp::FileAppender("FileAppender2",LOGFILE2);
log4cpp::Layout *layout1 = new log4cpp::SimpleLayout();
log4cpp::Layout *layout2 = new log4cpp::SimpleLayout();
log4cpp::Category& category = log4cpp::Category::getInstance("Category");
category.setAdditivity(true);
category.setPriority(log4cpp::Priority::INFO);
appender1->setLayout(layout1);
appender2->setLayout(layout2);
category.addAppender(appender1);
category.addAppender(appender2);
category.info("This is for tracing the flow");
category.notice("This is to notify certain events");
category.warn("This is to generate certain warnings");
log4cpp::Category::shutdown();
return 0;
}
Check the output of logs in test1.log and test2.log. You will find both the files to be similar
l$ cat ~/test1.log INFO : This is for tracing the flow NOTICE : This is to notify certain events WARN : This is to generate certain warnings
$ cat ~/test2.log INFO : This is for tracing the flow NOTICE : This is to notify certain events WARN : This is to generate certain warnings
Now let’s explore the second option. Here we make use of multiple categories. To make use of multiple categories,
Each category must be assigned an appender
Each category must be assigned an Layout
Each category must be assigned an priority
Check the following program
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <log4cpp/category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
using namespace std;
<pre>#define LOGFILE1 "/home/user/test1.log"
#define LOGFILE2 "/home/user/test2.log"
</pre>
int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender1 = new log4cpp::FileAppender("FileAppender1",LOGFILE1);
log4cpp::Appender *appender2 = new log4cpp::FileAppender("FileAppender2",LOGFILE2);
log4cpp::Layout *layout1 = new log4cpp::SimpleLayout();
log4cpp::Layout *layout2 = new log4cpp::BasicLayout();
log4cpp::Category& category1 = log4cpp::Category::getInstance("Category1");
log4cpp::Category& category2 = log4cpp::Category::getInstance("Category2");
category1.setPriority(log4cpp::Priority::INFO);
category2.setPriority(log4cpp::Priority::DEBUG);
appender1->setLayout(layout1);
appender2->setLayout(layout2);
category1.addAppender(appender1);
category2.addAppender(appender2);
category1.info("This is for tracing the flow");
category2.notice("This is to notify certain events");
category1.warn("This is to generate certain warnings");
log4cpp::Category::shutdown();
return 0;
}
The output of this program can be seen in multiple files
l$ cat ~/test1.log INFO : This is for tracing the flow WARN : This is to generate certain warnings
$ cat ~/test2.log 1263564981 NOTICE Category2 : This is to notify certain events
As you can see each file shows different log messages with different log formats
Comments: