log4cplus的三个例程
http://log4cplus.sourceforge.net/codeexamples.html里面自带的三个例程
Hello World Example
#include
<
log4cplus
/
logger.h
>
#include < log4cplus / configurator.h >
#include < iomanip >
using namespace log4cplus;
int
main()
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance( " main " );
LOG4CPLUS_WARN(logger, " Hello, World! " );
return 0 ;
}
#include < log4cplus / configurator.h >
#include < iomanip >
using namespace log4cplus;
int
main()
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance( " main " );
LOG4CPLUS_WARN(logger, " Hello, World! " );
return 0 ;
}
ostream Example (Show how to write logging messages.)
#include
<
log4cplus
/
logger.h
>
#include < log4cplus / configurator.h >
#include < iomanip >
using namespace std;
using namespace log4cplus;
int
main()
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance("logger");
LOG4CPLUS_WARN(logger, "This is"
<< " a reall"
<< "y long message." << endl
<< "Just testing it out" << endl
<< "What do you think?")
LOG4CPLUS_WARN(logger, "This is a bool: " << true)
LOG4CPLUS_WARN(logger, "This is a char: " << 'x')
LOG4CPLUS_WARN(logger, "This is a short: " << (short)-100)
LOG4CPLUS_WARN(logger, "This is a unsigned short: " << (unsigned short)100)
LOG4CPLUS_WARN(logger, "This is a int: " << (int)1000)
LOG4CPLUS_WARN(logger, "This is a unsigned int: " << (unsigned int)1000)
LOG4CPLUS_WARN(logger, "This is a long(hex): " << hex << (long)100000000)
LOG4CPLUS_WARN(logger, "This is a unsigned long: "
<< (unsigned long)100000000)
LOG4CPLUS_WARN(logger, "This is a float: " << (float)1.2345)
LOG4CPLUS_WARN(logger, "This is a double: "
<< setprecision(15)
<< (double)1.2345234234)
LOG4CPLUS_WARN(logger, "This is a long double: "
<< setprecision(15)
<< (long double)123452342342.342)
return 0;
}
#include < log4cplus / configurator.h >
#include < iomanip >
using namespace std;
using namespace log4cplus;
int
main()
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance("logger");
LOG4CPLUS_WARN(logger, "This is"
<< " a reall"
<< "y long message." << endl
<< "Just testing it out" << endl
<< "What do you think?")
LOG4CPLUS_WARN(logger, "This is a bool: " << true)
LOG4CPLUS_WARN(logger, "This is a char: " << 'x')
LOG4CPLUS_WARN(logger, "This is a short: " << (short)-100)
LOG4CPLUS_WARN(logger, "This is a unsigned short: " << (unsigned short)100)
LOG4CPLUS_WARN(logger, "This is a int: " << (int)1000)
LOG4CPLUS_WARN(logger, "This is a unsigned int: " << (unsigned int)1000)
LOG4CPLUS_WARN(logger, "This is a long(hex): " << hex << (long)100000000)
LOG4CPLUS_WARN(logger, "This is a unsigned long: "
<< (unsigned long)100000000)
LOG4CPLUS_WARN(logger, "This is a float: " << (float)1.2345)
LOG4CPLUS_WARN(logger, "This is a double: "
<< setprecision(15)
<< (double)1.2345234234)
LOG4CPLUS_WARN(logger, "This is a long double: "
<< setprecision(15)
<< (long double)123452342342.342)
return 0;
}
LogLevel Example (Shows how log messages can be filtered at runtime by adjusting the LogLevel.)
#include
<
log4cplus
/
logger.h
>
#include < log4cplus / configurator.h >
#include < iostream >
using namespace std;
using namespace log4cplus;
Logger logger = Logger::getInstance( " main " );
void printMessages()
{
LOG4CPLUS_TRACE(logger, "printMessages()");
LOG4CPLUS_DEBUG(logger, "This is a DEBUG message");
LOG4CPLUS_INFO(logger, "This is a INFO message");
LOG4CPLUS_WARN(logger, "This is a WARN message");
LOG4CPLUS_ERROR(logger, "This is a ERROR message");
LOG4CPLUS_FATAL(logger, "This is a FATAL message");
}
int
main()
{
BasicConfigurator config;
config.configure();
logger.setLogLevel(TRACE_LOG_LEVEL);
cout << "*** calling printMessages() with TRACE set: ***" << endl;
printMessages();
logger.setLogLevel(DEBUG_LOG_LEVEL);
cout << "\n*** calling printMessages() with DEBUG set: ***" << endl;
printMessages();
logger.setLogLevel(INFO_LOG_LEVEL);
cout << "\n*** calling printMessages() with INFO set: ***" << endl;
printMessages();
logger.setLogLevel(WARN_LOG_LEVEL);
cout << "\n*** calling printMessages() with WARN set: ***" << endl;
printMessages();
logger.setLogLevel(ERROR_LOG_LEVEL);
cout << "\n*** calling printMessages() with ERROR set: ***" << endl;
printMessages();
logger.setLogLevel(FATAL_LOG_LEVEL);
cout << "\n*** calling printMessages() with FATAL set: ***" << endl;
printMessages();
return 0;
}
#include < log4cplus / configurator.h >
#include < iostream >
using namespace std;
using namespace log4cplus;
Logger logger = Logger::getInstance( " main " );
void printMessages()
{
LOG4CPLUS_TRACE(logger, "printMessages()");
LOG4CPLUS_DEBUG(logger, "This is a DEBUG message");
LOG4CPLUS_INFO(logger, "This is a INFO message");
LOG4CPLUS_WARN(logger, "This is a WARN message");
LOG4CPLUS_ERROR(logger, "This is a ERROR message");
LOG4CPLUS_FATAL(logger, "This is a FATAL message");
}
int
main()
{
BasicConfigurator config;
config.configure();
logger.setLogLevel(TRACE_LOG_LEVEL);
cout << "*** calling printMessages() with TRACE set: ***" << endl;
printMessages();
logger.setLogLevel(DEBUG_LOG_LEVEL);
cout << "\n*** calling printMessages() with DEBUG set: ***" << endl;
printMessages();
logger.setLogLevel(INFO_LOG_LEVEL);
cout << "\n*** calling printMessages() with INFO set: ***" << endl;
printMessages();
logger.setLogLevel(WARN_LOG_LEVEL);
cout << "\n*** calling printMessages() with WARN set: ***" << endl;
printMessages();
logger.setLogLevel(ERROR_LOG_LEVEL);
cout << "\n*** calling printMessages() with ERROR set: ***" << endl;
printMessages();
logger.setLogLevel(FATAL_LOG_LEVEL);
cout << "\n*** calling printMessages() with FATAL set: ***" << endl;
printMessages();
return 0;
}