log4cpp::FileAppender – Logging into Files
log4cpp::FileAppender is used to output the logs to a file. The following program will show how an object of log4cpp::FileAppender is used.
#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
#define LOGFILE "/home/user/test.log"
int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
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");
}
Let’s take a look at the following code snippet
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
The first parameter of the constructor is the name of the appender and the second parameter is the destination of the log file.
The constructor of log4cpp::FileAppender is as follows
log4cpp::FileAppender::FileAppender ( const std::string & name, const std::string & fileName, bool append = true, mode_t mode = 00644 )
We already discussed first and second parameters. We did not use the third and fourth parameters
append: This decides whether to append logs. The best option is to set this value true so as to not to lose any critical log information of the software run some days back. But there is a danger of log file taking up lot of space or crossing the file size supported by your system. The best approach is to save and truncate the logfile manually or through some automated process.
mode: this is the mode in which a file should be created, the rwx permissions pertaining to owner, group and others. The default value is 0644
We have one more constructor with us
log4cpp::FileAppender::FileAppender ( const std::string & name, int fd )
Instead of making the (default) supported values of the previous constructor, we can open the file in a way of our choice. We need to supply the file descriptor as the parameter. We get a file descriptor when we make use of the open() system call to open a file
So we replace the following code snippet
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
with the following one
int fd;
fd = open (LOGFILE,O_CREAT|O_APPEND|O_WRONLY );
if (fd < 0) {
perror("File Open: Error");
exit(0);
}
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",fd);
By making use of the constructor which requires the file descriptor as a parameter, we are able to add certain error messages in order to find out the failure in case of opening of files. This was not feasible in the first case. Overall, this change will not have any impact in the execution of the program. The program will behave in the same way.
Check the complete code after adding the header files
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <log4cpp/category.hh>
#include <log4cpp/fileappender.hh>
#include <log4cpp/simplelayout.hh>
#define LOGFILE "/home/user/test.log"
int main()
{
/*Setting up Appender, layout and Category*/
int fd;
fd = open (LOGFILE,O_CREAT|O_APPEND|O_WRONLY );
if (fd < 0) {
perror("File Open: Error");
exit(0);
}
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",fd);
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");
}
Comments: