Copyright Notice
Please respect the original works. Reprint please keep the article integrity, and in the form of hyperlinks to the original author "Tingsking18" and the main site address, convenient for other friends to ask and correct.
1. Write a header file, "Mylog.h", as follows:
#pragma once
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/logger.h>
#include <log4cplus/ndc.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/threads.h>
#include <log4cplus/streams.h>
using namespace Std;
using namespace Log4cplus;
struct Glog
{
Glog (const char * _fname)
{
string n = _fname;
Logger _l = (logger::getinstance (n));
n+= ". Log";
_l.addappender (New Rollingfileappender (N.c_str (), 1024*10, 10));
_l.addappender (New Consoleappender ());
}
};
#define USE_SEPARATE_LOG (fname) static Glog _g (fname);
#define FLOG Logger::getinstance (__file__)
2. Include "Mylog.h" in all the CPP files to log.
And in the front of the file add a sentence: Use_separate_log (__file__)
3. All log places are used in the following sentences:
Log4cplus_info (Flog, "testtest" <<endl);
Like what:
#include "stdafx.h"
#include <iostream>
#include "Mylog.h"
Use_separate_log (__file__)
int main (int argc, _tchar* argv[])
{
Log4cplus_info (Flog, "Hello World" <<endl);
return 0;
}
BTW: It's not a good idea to record a log file per file.