Main BLOGGER
Google
WWW THIS BLOG
Wednesday, August 17, 2005
 
A trivial log4cxx tutorial
1. Create a new win32 VC++ project
2. "Property Pages" -- "C/C++" -- "General" --
"Additional Include Directories"="..\log4cxx-0.9.7\include;"
3. "Property Pages" -- "Linker" -- "General" --
"Additional Library Directories"="..\log4cxx-0.9.7\msvc\lib\debug;"
3. "Property Pages" -- "Linker" -- "Input" --
"Additional Dependencies"="log4cxx.lib"


-----------------------------------------------
Change log4j properties on the fly

#include <stdlib.h>
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/ndc.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/patternlayout.h>

using namespace log4cxx;
using namespace log4cxx::helpers;
int main()
{
int result = EXIT_SUCCESS;
try
{
PatternLayoutPtr myLayoutPtr = new PatternLayout("%r [%t] %-5p %c- %m%n %x");
//%x means to print context info stored in NDC
ConsoleAppenderPtr myAppenderPtr = new ConsoleAppender(myLayoutPtr);
BasicConfigurator::configure(myAppenderPtr);
LoggerPtr rootLogger = Logger::getRootLogger();

NDC::push(_T("context\n"));
LOG4CXX_DEBUG(rootLogger,_T("debug "));
LOG4CXX_INFO(rootLogger,_T("info "));
LOG4CXX_WARN(rootLogger,_T("warn "));
LOG4CXX_ERROR(rootLogger,_T("error "));
LOG4CXX_FATAL(rootLogger,_T("fatal "));
NDC::pop();

myLayoutPtr->setConversionPattern("%-5p [%t]: %m %n");
//change the pattern
rootLogger->setLevel(log4cxx::Level::ERROR);
//change the log level
LOG4CXX_DEBUG(rootLogger,_T("debug "));
LOG4CXX_INFO(rootLogger,_T("info "));
LOG4CXX_WARN(rootLogger,_T("warn "));
LOG4CXX_ERROR(rootLogger,_T("error "));
LOG4CXX_FATAL(rootLogger,_T("fatal "));

}
catch(Exception&)
{
result = EXIT_FAILURE;
}

return result;
}

Sample output:

50 [804] DEBUG root- debug
context
50 [804] INFO root- info
context
50 [804] WARN root- warn
context
50 [804] ERROR root- error
context
50 [804] FATAL root- fatal
context
ERROR [804]: error
FATAL [804]: fatal


-----------------------------------------------


#include < stdlib.h>

#include <log4cxx/logger.h>

#include <log4cxx/basicconfigurator.h>

#include <log4cxx/helpers/exception.h>

#include <log4cxx/helpers/exception.h>

#include <log4cxx/helpers/stringhelper.h>

#include <log4cxx/propertyconfigurator.h>

#include <log4cxx/helpers/exception.h>

#include <log4cxx/ndc.h>

#include <log4cxx/level.h>



using namespace log4cxx;
using namespace log4cxx::helpers;

int main()
{
int result = EXIT_SUCCESS;
enum {LOG4CXX_POLL_INTERVAL = 60, };
const char* LOG4CXX_PROPERTY_FILE =
"C://3rd//log4cxx-0.9.7//msvc//examples//sd.log4j.properties";
try
{
// if we do not specify the log4j properties file, we must use
// BasicConfigurator::configure();
// else we pass in the file name with path and watch time

//
log4cxx::PropertyConfigurator::configureAndWatch(
LOG4CXX_PROPERTY_FILE,
LOG4CXX_POLL_INTERVAL);
// a thread will be created that will periodically
// check if the LOG4CXX_PROPERTY_FILE has been created or
// modified. The period is determined by the LOG4CXX_POLL_INTERVAL
// argument in seconds. If a change or file creation is detected, then
// LOG4CXX_PROPERTY_FILE is read to configure log4j.

LoggerPtr rootLogger = Logger::getLogger("sd");

// get the logger setting by name "sd"

NDC::push(_T("trivial context"));
// NDC (nested diagnostic context) for multithread logging purpose

while(1==1)
{
rootLogger->debug(_T("debug message"));
rootLogger->info(_T("info message"));
rootLogger->warn(_T("warn message"));
rootLogger->error(_T("error message"));
rootLogger->fatal(_T("fatal message"));
}
}
catch(Exception&)
{
result = EXIT_FAILURE;
}

return result;
}
-------------sample sd.log4j.properties file ---------------

log4j.rootLogger=INFO, stdout, SD
^^^^
change here to [ALL| OFF|DEBUG|INFO|WARN|ERROR|FATAL]

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] (%F:%L) - %m%n

log4j.appender.SD=org.apache.log4j.RollingFileAppender

#specify log file
log4j.appender.SD.File=../logs/sd.log
log4j.appender.SD.MaxFileSize=100000KB

# Keep one backup file
log4j.appender.SD.MaxBackupIndex=1
log4j.appender.SD.layout=org.apache.log4j.PatternLayout
log4j.appender.SD.layout.ConversionPattern=%d %p [%t] %c - %m%n



<< Home

Powered by Blogger

Google
WWW THIS BLOG