You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Yusuf Cinar <ci...@yahoo.com> on 2011/03/23 00:37:47 UTC
Custom Appender
Hi
I am trying to write a custom appender following VectorAppender,
here http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/test/cpp/vectorappender.h?view=co, as
an example.
However, I am getting an error when calling my custom Appenders constructor:
undefined reference to `log4cxx::VirtualAppender::VirtualAppender()' -- see
below in red.
I am not sure what is going on but guessing that I am not linking a library
although I am linking log4cxx.a, apr-1.a, aprutil-1.a, xml2.a, expat.a. Is there
another library to link when creating a Custom appender inherited
from AppenderSkeleton?
Below is the code snippet.
Thanks in advance for any help.
#ifndef VIRTUALAPPENDER_H_
#define VIRTUALAPPENDER_H_
#include <log4cxx/appenderskeleton.h>
#include <log4cxx/spi/loggingevent.h>
namespace log4cxx
{
class VirtualAppender : public AppenderSkeleton
{
public:
DECLARE_LOG4CXX_OBJECT(VirtualAppender)
BEGIN_LOG4CXX_CAST_MAP()
LOG4CXX_CAST_ENTRY(VirtualAppender)
LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
END_LOG4CXX_CAST_MAP()
VirtualAppender();
~VirtualAppender();
/**
This method is called by the AppenderSkeleton#doAppend
method.
*/
void append(const spi::LoggingEventPtr& event,
log4cxx::helpers::Pool& p)
{
LogString test = event->getMessage();
}
void close();
bool isClosed() const
{ return closed; }
bool requiresLayout() const
{ return false; }
};
}
#endif /* VIRTUALAPPENDER_H_ */
My app:
#include <iostream>
using namespace std;
// include log4cxx header files.
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
#include "VirtualAppender.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr logger(Logger::getLogger("MyApp"));
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
VirtualAppender * vAppender = new VirtualAppender(); // Error happens here
logger->addAppender(vAppender);
LOG4CXX_INFO(logger, "Entering application.");
LOG4CXX_INFO(logger, "Exiting application.");
return 0;
}
Re: Custom Appender
Posted by Curt Arnold <ca...@apache.org>.
First. it would be best for you to define your custom appenders in a namespace other than log4cxx.
Second, you declare a constructor, destructor and close method for VirtualAppender, but you did not provide an implementation. Unless you compiled and linked in a file that looked like:
#include "VirtualAppender.h"
log4cxx::VirtualAppender::VirtualAppender() {
}
log4cxx::VirtualAppender::~VirtualAppender() {
}
void log4cxx::VirtualAppender::close() {
}
Then the linker would be expected to complain that there was no implementation provided.