I'm newbie working with a logging system, but it's something very useful to add to your programs.
Summarizing my problem. I use the logging library log4cplus
I doing and easy example with two simple classes and the main program.
That's my error log:
- log4cplus:ERROR No appenders could be found for logger (main).
- log4cplus:ERROR Please initialize the log4cplus system properly.
- Segmentation fault: 11
The problem is i don't know how to solve the problem with the appender.
That's basic code of my example. Class1.h
#include <iostream>
#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>
using namespace std;
// Use the log4cplus namespace
using namespace log4cplus;
class one
{
private:
// Create the logger
Logger logger;
public:
bool flag;
int valor;
one();
int multiplica(int a);
};
Class1.cpp
one::one()
{
logger.getInstance(LOG4CPLUS_TEXT("Clase One - constructor."));
}
int one::multiplica(int a)
{
int sol = 0;
sol = valor * a;
// Imprimo un mesaje en el log.
LOG4CPLUS_INFO(logger, "El resultado de la multiplicación es: xx");
return sol;
}
Class2.h
#include <iostream>
#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>
using namespace std;
// Use the log4cplus namespace
using namespace log4cplus;
class two
{
private:
// Create the logger
Logger logger;
public:
bool flag;
int valor;
two();
int suma(int a);
};
Class.cpp
two::two()
{
logger.getInstance(LOG4CPLUS_TEXT("Clase Two - DEconstructor."));
}
int two::suma(int a)
{
int sol = 0;
sol = valor + a;
// Imprimo un mesaje en el log.
LOG4CPLUS_INFO(logger, "El resultado de la suma es: YY ");
return sol;
}
main.cpp
int main(int argc, char** argv)
{
// Load the properties
PropertyConfigurator::doConfigure("logClase.properties");
Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application startup");
}
one uno;
two dos;
uno.valor = dos.valor = 4;
uno.multiplica(7);
dos.suma(7);
// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application shutdown");
}
return 0;
}
What i'm doing wrong ??? That's the correct way to work with the logging system ??
I use a simple properties file to save all the log messages in a file.
That's my logClase.properties file to configure de logger.
log4cplus.rootLogger=INFO, STDOUT, FILEAPPENDER
log4cplus.logger.main=INFO
log4cplus.logger.utils=FILEAPPENDER
log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n
log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender
log4cplus.appender.FILEAPPENDER.File=KlasseEx.log
log4cplus.appender.FILEAPPENDER.MaxFileSize=5MB
#log4cplus.appender.FILEAPPENDER.MaxFileSize=500KB
log4cplus.appender.FILEAPPENDER.MaxBackupIndex=1
log4cplus.appender.FILEAPPENDER.layout=log4cplus::PatternLayout
log4cplus.appender.FILEAPPENDER.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n
I want to use FILEAPPENDER and the Console appender to throw messages in a file and into the console. I think that's not must be so difficult to do, it must be easy to do it.