Let log4cplus a log file per CPP

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.