欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報邀請碼778899分享:log4j Log4cpp

柚子快報邀請碼778899分享:log4j Log4cpp

http://yzkb.51969.com/

Log4cpp

1.Log4cpp是什么?2.Log4cpp的安裝3.Log4cpp的使用

1.Log4cpp是什么?

log4cpp是一個C++語言編寫的開源日志庫,它提供了一套簡單易用的API,支持多線程和遠程日志記錄。log4cpp的設(shè)計靈感來源于Java中的log4j,它能夠幫助開發(fā)人員在應(yīng)用程序中記錄日志,幫助開發(fā)人員進行調(diào)試和故障排除。

log4cpp支持輸出到文件、控制臺、syslog和網(wǎng)絡(luò)套接字等多種日志輸出方式,可以按照不同的級別(如debug、info、warning、error和fatal)對日志進行分類管理。它還提供了靈活的配置選項,可以通過配置文件或程序代碼來配置日志記錄方式和級別。

log4cpp已經(jīng)被廣泛地應(yīng)用于各種C++應(yīng)用程序中,包括網(wǎng)絡(luò)應(yīng)用、桌面應(yīng)用、嵌入式系統(tǒng)和游戲開發(fā)等領(lǐng)域。

2.Log4cpp的安裝

Log4cpp的官網(wǎng):

http://log4cpp.sourceforge.net/

安裝步驟:

wget https://nchc.dl.sourceforge.net/project/log4cpp/log4cpp-1.1.x%20%28new%29/log4cpp-1.1/log4cpp-1.1.3.tar.gz

tar xzvf log4cpp-1.1.3.tar.gz

cd log4cpp

./configure --prefix=/home/wxncom/shared_bike/third/

make

make install

3.Log4cpp的使用

step 1 :包含頭文件

#include

#include

#include

#include

step 2 :初始化日志輸出的目的地(appenders)

// 輸出到std::cout

log4cpp::Appender *appender = new log4cpp::OstreamAppender("root", &std::cout);

// 輸出到log文件

//log4cpp::Appender *appender = new log4cpp::FileAppender("root", "test.log");

appender有以下這些: log4cpp::FileAppender // 輸出到文件 log4cpp::RollingFileAppender // 輸出到回卷文件,即當(dāng)文件到達某個大小后回卷 log4cpp::OstreamAppender // 輸出到一個ostream類 log4cpp::RemoteSyslogAppender // 輸出到遠程syslog服務(wù)器 log4cpp::StringQueueAppender // 內(nèi)存隊列 log4cpp::SyslogAppender // 本地syslog log4cpp::Win32DebugAppender // 發(fā)送到缺省系統(tǒng)調(diào)試器 log4cpp::NTEventLogAppender // 發(fā)送到win 事件日志 ??上文,我們說過日志輸出到終端或者文件中實際上是很慢的,會引起IO中斷,所以我們可以輸出到內(nèi)存里

S

t

r

i

n

g

Q

u

e

u

e

A

p

p

e

n

d

e

r

StringQueueAppender

StringQueueAppender,然后從

S

t

r

i

n

g

Q

u

e

u

e

A

p

p

e

n

d

e

r

StringQueueAppender

StringQueueAppender輸出到其它地方,這樣我們的線程執(zhí)行是比較高效的。

step 3 :設(shè)置日志輸出的格式

log4cpp::PatternLayout *patternLayout = new log4cpp::PatternLayout();

patternLayout->setConversionPattern("%d [%p] - %m%n");

appender->setLayout(patternLayout);

日志輸出格式控制有: PatternLayout supports following set of format characters: %% - a single percent sign %c - the category %d - the date\n Date format: The date format character may be followed by a date format specifier enclosed between braces. For example, %d{%\H:%M:%S,%l} or %d{%\d %m %Y %H:%\M:%S,%l}. If no date format specifier is given then the following format is used: “Wed Jan 02 02:03:55 1980”. The date format specifier admits the same syntax as the ANSI C function strftime, with 1 addition. The addition is the specifier %l for milliseconds, padded with zeros to make 3 digits. %m - the message %n - the platform specific line separator %p - the priority %r - milliseconds since this layout was created. %R - seconds since Jan 1, 1970 %u - clock ticks since process start %x - the NDC %t - thread name By default, ConversionPattern for PatternLayout is set to “%m%n”.

step 4 :設(shè)置類別輸出的(category)和日志優(yōu)先級(priority)

log4cpp::Category &root = log4cpp::Category::getRoot();

root.setPriority(log4cpp::Priority::NOTICE);

root.addAppender(appender);

日志的級別總共有:NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG。日志級別的意思是低于該級別的日志不會被記錄。

step 5 :定義一個宏

#define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << " " << __LINE__ << ": "

當(dāng)然也可以使用Category定義的函數(shù):

/**

* Log a message with the specified priority.

* @param priority The priority of this log message.

* @param stringFormat Format specifier for the string to write

* in the log file.

* @param ... The arguments for stringFormat

**/

virtual void log(Priority::Value priority, const char* stringFormat,

...) throw();

/**

* Log a message with the specified priority.

* @param priority The priority of this log message.

* @param message string to write in the log file

**/

virtual void log(Priority::Value priority,

const std::string& message) throw();

void debug(const char* stringFormat, ...) throw();

void debug(const std::string& message) throw();

void info(const char* stringFormat, ...) throw();

...

step 6 : 使用宏定義記錄日志

LOG(DEBUG) << "i am happy.";

LOG(INFO) << "oh, you happy, we happy.";

LOG(NOTICE)<< "please do not contact me. ";

LOG(WARN) << "i am very busy now.";

LOG(ERROR) << "oh, what happed?";

當(dāng)然我們在使用過程中,可以封裝一個單例,oh,單例是什么?! 記得復(fù)習(xí)一下!

在實際工程上應(yīng)用,我們是使用日志配置文件去控制日志記錄的。接下來讓我們先配置一個日志配置文件:

#定義Root category的屬性

log4cpp.rootCategory=DEBUG, RootLog

#定義RootLog屬性

log4cpp.appender.RootLog=RollingFileAppender

log4cpp.appender.RootLog.layout=PatternLayout

#log4cpp.appender.RootLog.layout.ConversionPattern=%d{% m-%d %H:%M:%S %l} [%t][%p]%m%n

log4cpp.appender.RootLog.layout.ConversionPattern=%d{%m-%d %H:%M:%S %l} [%t][%p]%m%n

log4cpp.appender.RootLog.fileName=/var/log/shared_bike.log

log4cpp.appender.RootLog.maxFileSize=268435456 #256MB

log4cpp.appender.RootLog.fileNamePattern=shared_bike_%i.log

log4cpp.appender.RootLog.maxBackupIndex=256

那如何使用配置文件定義log呢?

//Logger.h

#ifndef DISTRIBUTED_LOGGER_H_

#define DISTRIBUTED_LOGGER_H_

#include

#include

#include

#include

#include

class Logger

{

public:

bool init(const std::string &log_conf_file);

static Logger *instance()

{

return &instance_;

}

log4cpp::Category *GetHandle()

{

return category_;

}

protected:

static Logger instance_;

log4cpp::Category *category_;

};

#define LOG_INFO Logger::instance()->GetHandle()->info

#define LOG_DEBUG Logger::instance()->GetHandle()->debug

#define LOG_ERROR Logger::instance()->GetHandle()->error

#define LOG_WARN Logger::instance()->GetHandle()->warn

#endif

// Logger.cpp

#include "Logger.h"

#include

#include

#include

#include

#include

#include

// 外部的static成員必須聲明一下,不聲明就會報錯!!!

Logger Logger::instance_;

bool Logger::init(const std::string &log_conf_file)

{

try

{

log4cpp::PropertyConfigurator::configure(log_conf_file);

}

catch (log4cpp::ConfigureFailure &f)

{

std::cerr << " load log config file " << log_conf_file.c_str() << " failed with result : " << f.what() << std::endl;

return false;

}

category_ = &log4cpp::Category::getRoot(); // 輸出日志category_

return true;

}

柚子快報邀請碼778899分享:log4j Log4cpp

http://yzkb.51969.com/

文章鏈接

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19090261.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機訪問

文章目錄