Log4cpp::StringQueueAppender – Manipulating the log4cpp logs
log4cpp logs can be directed to files, standard output, back up files or even to the System Log. But before moving to discussing about log4cpp::StringQueueAppender, I would like to present certain scenarios or use cases of StringQueueAppender.
- You do not want to actually print the log files, but simply want to count certain occurrences of certain lines in the log, may be for statistics or analytics. (Yes you can do this even after post logging using some shell scripts. But what if you don’t waste the resources)
- You want to perform certain manipulations in the log before actually printing it into some file.
log4cpp::StringQueueAppender does not direct the logs to any file or standard output. Instead it directs the log into a queue. As you know that queue works in the FIFO manner. So you can be sure that the order of the logs is preserved. So you now have the queue of logs and you can manipulate it any way you want.
The following program depicts how the logs are stored in a queue by StringQueueAppender and later manipulated. The program makes use of the following public methods of StringQueueAppender
StringQueueAppender (const std::string & name) : Constructor to initialize. Unlike other Appender, this only requires the name you want to give to this appender.
std::queue<std::string>getQueue(): This returns the queue used by the StringQueueAppender
size_t queueSize(): returns the size of the queue
popMessage(): pop the oldest log message
Check how these functions are used
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <log4cpp/category.hh>
#include <log4cpp/StringQueueAppender.hh>
#include <log4cpp/SimpleLayout.hh>
using namespace std;
int changeLogQueue (queue<string>& q) {
queue<string> temp;
int size = q.size();
for (int i = size; i > size/2;i--) {
temp.push(q.front());
q.pop();
}
q.push("StringQueueAppender: Inserting some Random lines into the log\n");
q.push("StringQueueAppender: Inserting extra lines into the log\n");
for (int i = size; i > size/2;i--) {
q.push(temp.front());
temp.pop();
}
}
int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::StringQueueAppender *appender = new log4cpp::StringQueueAppender("StringQueueAppender");
log4cpp::Layout *layout = new log4cpp::SimpleLayout();
log4cpp::Category& category = log4cpp::Category::getInstance("Category");
appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);
category.info("This is for tracing the flow");
category.notice("This is to notify certain events");
category.warn("This is to generate certain warnings");
changeLogQueue(appender->getQueue());
for (int i = appender->queueSize(); i>0; i--)
cout<<appender->popMessage();
}
The output of the program is displayed on the standard output
WARN : This is to generate certain warnings StringQueueAppender: Inserting some Random lines into the log StringQueueAppender: Inserting extra lines into the log INFO : This is for tracing the flow NOTICE : This is to notify certain events
Note that the order of log messages is preserved. You can write similar program to come out with statistics
Comments: