默认开启Unicode编码
#include <log4cplus/logger.h>
#include <log4cplus/initializer.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/stringhelper.h>
#include <log4cplus/loggingmacros.h>
#include <clocale>
#include <windows.h>
//通用版将wstring转化为string
__inline static
std::string WCToMB(const std::wstring& ws, unsigned long cp = CP_ACP)
{
if (ws.empty() == true)return ("");
std::string s(WideCharToMultiByte(cp, 0, ws.data(), ws.size(), NULL, 0, NULL, NULL), ('\0'));
return s.substr(0, WideCharToMultiByte(cp, 0, ws.c_str(), ws.size(), (LPSTR)s.data(), (int)s.size(), NULL, NULL));
}
//通用版将string转化为wstring
__inline static
std::wstring MBToWC(const std::string& s, unsigned long cp = CP_ACP)
{
if (s.empty() == true)return (L"");
std::wstring ws(MultiByteToWideChar(cp, 0, s.data(), s.size(), NULL, 0), (L'\0'));
return ws.substr(0, MultiByteToWideChar(cp, 0, s.data(), s.size(), (LPWSTR)ws.data(), (int)ws.size()));
}
#define ANSIToWIDE(A) MBToWC(A,CP_ACP)
#define WIDEToANSI(W) WCToMB(W,CP_ACP)
#define UTF8ToWIDE(U) MBToWC(U,CP_UTF8)
#define WIDEToUTF8(W) WCToMB(W,CP_UTF8)
#define ANSIToUTF8(A) WIDEToUTF8(ANSIToWIDE(A))
#define UTF8ToANSI(U) WIDEToANSI(UTF8ToWIDE(U))
int
main() {
//setlocale(LC_ALL, "chs");
//std::setlocale(std::locale::none, ("chs"));
log4cplus::Initializer initializer;
PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log.properties"));
Logger logger = Logger::getRoot();
// trace
LOG4CPLUS_TRACE(logger, LOG4CPLUS_TEXT("中文trace and get the fingerprint: "));
// debug
LOG4CPLUS_DEBUG(logger, LOG4CPLUS_TEXT("中文this is debug log: ") << "random integer: ");
// info
LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("中文the information centry....") << "[ 1 + 1 = " << 1 + 1 << "]");
// warn
LOG4CPLUS_WARN(logger, LOG4CPLUS_TEXT("中文Writing warning messages to log...."));
// error
LOG4CPLUS_ERROR(logger, LOG4CPLUS_TEXT("中文ooooooh, there is an error...."));
//fatal
LOG4CPLUS_FATAL(logger, LOG4CPLUS_TEXT("中文oh, my god! the fatal error occur!!!!!!!!!"));
//
std::string utf8 = ("你好hello");
Logger loggerTest = Logger::getInstance(LOG4CPLUS_TEXT("test"));
LOG4CPLUS_ERROR(loggerTest, L"utf8中文");
LOG4CPLUS_ERROR(loggerTest, LOG4CPLUS_TEXT("utf8中文"));
#ifdef _MSC_VER
std::wstring wMsg = UTF8ToWIDE(utf8);
LOG4CPLUS_ERROR(loggerTest, wMsg.c_str());
#endif // _MSC_VER
LOG4CPLUS_ERROR(loggerTest, "anther logger,error ");
return 0;
}
log.properties
log4cplus.rootLogger=ERROR, R
log4cplus.appender.R=log4cplus::DailyRollingFileAppender
log4cplus.appender.R.File=./log/error.log
log4cplus.appender.R.Schedule=HOURLY
log4cplus.appender.R.Append=true
log4cplus.appender.R.layout=log4cplus::PatternLayout
log4cplus.appender.R.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S,%Q}] [%t] %-5p - %m%n[%p
log4cplus.logger.test=TRACE, RR
log4cplus.appender.RR=log4cplus::DailyRollingFileAppender
log4cplus.appender.RR.File=./log/test.log
log4cplus.appender.RR.MaxBackupIndex=192 #8 * 24
log4cplus.appender.RR.Schedule=HOURLY
log4cplus.appender.RR.Append=true
log4cplus.appender.RR.layout=log4cplus::PatternLayout
log4cplus.appender.RR.layout.ConversionPattern=%p %D{%Y-%m-%d %H:%M:%S.%q}:%t %F:%L "%m"%