linux log4cplus安装和实例

tar –xvf  log4cplus-1.1.3-rc5.tar.gz
cd log4cplus-1.1.3-rc5
configure --prefix=/usr/local/log4cplus
make
make install

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/log4cplus/lib/

g++ -g  test_log4cplus.cpp -o process  -I/usr/local/log4cplus/include  -L/usr/local/log4cplus/lib -llog4cplus -lrt -lpthread

#include <iostream>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <iomanip>
#include <log4cplus/logger.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
using namespace log4cplus;
int main()
{
        /* step 1: Instantiate an appender object */
        SharedAppenderPtr  _append(new ConsoleAppender());
        _append->setName(LOG4CPLUS_TEXT("ConsoleAppender"));
        /* step 2: Instantiate a layout object */
        //std::string pattern = ;   
        std::auto_ptr<Layout> _layout(new PatternLayout(LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S}  - %m [%l]%n")));
        /* step 3: Attach the layout object to the appender */
        _append->setLayout(_layout);
        /* step 4: Instantiate a logger object */
        Logger _logger = Logger::getInstance(LOG4CPLUS_TEXT("test"));
        /* step 5: Attach the appender object to the logger  */
        _logger.addAppender(_append);
        /* step 6: Set a priority for the logger  */
        _logger.setLogLevel(WARN_LOG_LEVEL);
        /* log activity */
        LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message");
        LOG4CPLUS_WARN(_logger, "This is the SECOND log message");
        LOG4CPLUS_TRACE(_logger, "This is" << " just a t" << "est." << std::endl);
        LOG4CPLUS_DEBUG(_logger, "This is a bool: " << true);
        LOG4CPLUS_INFO(_logger, "This is a char: " << 'x');
        LOG4CPLUS_WARN(_logger, "This is a int: " << 1000);
        LOG4CPLUS_ERROR(_logger, "This is a long(hex): " << std::hex << 100000000);
        LOG4CPLUS_FATAL(_logger, "This is a double: " << std::setprecision(15) << 1.2345234234);
        return 0;
}

 

posted @ 2017-07-17 15:55  略加思索的河马  阅读(314)  评论(0编辑  收藏  举报