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 "Donetti, John G." <Do...@llnl.gov> on 2011/08/05 00:51:10 UTC

Custom Appender Issue

I am attempting to add a simple custom appender to my code. I have been able to compile the appender, however, I cannot get the logger to find the new appender.

I get the error:
log4cxx: Could not create an Appender. Reported error follows.
log4cxx: Class not found: bgsyslogging::AppWindowAppender
log4cxx: No appender named [AppWindowAppender] could be found.

My log4cxx config file looks like:


<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <!-- Output the log message to system console.
    -->

  <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%-5p - %m (%F:%L) %n"/>
    </layout>
  </appender>


  <appender name="AppWindowAppender" class="bgsyslogging::AppWindowAppender">
    <!-- <param name="Threshold" value="INFO"/> -->
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{MMM dd yyyy HH:mm:ss,SSS} [%-5p] - %m %n"/> -->
    </layout>
  </appender>

  <!-- Setup the root category, add the appenders and set the default level
            5 level of logging,  ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
           The root level is set with INFO, which mean any message greater or same
           as INFO will be log down, in this case, DEBUG is not logged.
            To log all regardless of logging level, set <priority value="ALL">
       To shut off the logging of all messages, set <priority value="OFF">
        -->

  <root>
    <priority value="ALL" />
    <appender-ref ref="ConsoleAppender"/>
    <appender-ref ref="AppWindowAppender"/>
  </root>

</log4j:configuration>

The .h file looks like:

#ifndef APPWINDOWAPPENDER_H
#define APPWINDOWAPPENDER_H



#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/propertyconfigurator.h"
#include "log4cxx/helpers/exception.h"
#include "Log4cxx/xml/domconfigurator.h"
#include "log4cxx/appenderskeleton.h"
#include "log4cxx/spi/loggingevent.h"
#include "log4cxx/spi/errorhandler.h"
#include <vector>

using namespace log4cxx;

namespace bgsyslogging
{
       /**
        An appender that appends logging events to a vector.
        */
        class AppWindowAppender : public AppenderSkeleton
        {
                    public:

                BEGIN_LOG4CXX_CAST_MAP()
                        LOG4CXX_CAST_ENTRY(AppWindowAppender)
                        LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
                END_LOG4CXX_CAST_MAP()

                           AppWindowAppender();
                           ~AppWindowAppender();

                /**
                This method is called by the AppenderSkeleton#doAppend
                method.
                */
                void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p);


                void close();

                bool isClosed() const
                        { return closed; }

                bool requiresLayout() const
                        { return true;   }

        };

}
#endif // APPWINDOWAPPENDER_H

And the .cpp file looks like:

#include "AppWindowAppender.h"
#include "log4cxx/helpers/thread.h"
#include "log4cxx/helpers/transcoder.h"
#include <QString>
using namespace log4cxx;
using namespace log4cxx::helpers;


bgsyslogging::AppWindowAppender::AppWindowAppender() {
}

bgsyslogging::AppWindowAppender::~AppWindowAppender() {}

void bgsyslogging::AppWindowAppender::append(const spi::LoggingEventPtr& event, Pool& p)
{
       if ( this->layout == NULL ) {
             LOG4CXX_ENCODE_CHAR(nameStr, name);
             std::string msg("No Layout set for the appender named [ ");
             msg.append(nameStr);
             msg.append(" ].");

             LOG4CXX_DECODE_CHAR(msgL, msg);
             errorHandler->error(msgL);
           return;
       }

       log4cxx::LogString fMsg;
       this->layout->format(fMsg, event, p);
       LOG4CXX_ENCODE_CHAR(fMsgStr, fMsg);
       printf("AppWindowAppender: %s", fMsgStr.c_str());

}

void bgsyslogging::AppWindowAppender::close()
{
        if (this->closed)
        {
                return;
        }

        this->closed = true;
}


What have I forgot to do? What have I done wrong? Any help would be greatly appreciated.



John G. Donetti
Lawrence Livermore National Laboratory
Phone: 925 423-6009
Email: donetti1@llnl.gov


RE: Custom Appender Issue

Posted by Greg Powell <GP...@deltad.com>.
I think you are missing the DECLARE macro call in the following...

 

namespace bgsyslogging

{

       /**

        An appender that appends logging events to a vector.

        */

        class AppWindowAppender : public AppenderSkeleton

        {

                    public:

                DECLARE_LOG4CXX_OBJECT(AppWindowAppender)

                BEGIN_LOG4CXX_CAST_MAP()

                        LOG4CXX_CAST_ENTRY(AppWindowAppender)

                        LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)

                END_LOG4CXX_CAST_MAP()

...

 

As well as the implement macro call in the cpp file.

 

#include "AppWindowAppender.h"

#include "log4cxx/helpers/thread.h"

#include "log4cxx/helpers/transcoder.h"

#include <QString>

using namespace log4cxx;

using namespace log4cxx::helpers;

 

IMPLEMENT_LOG4CXX_OBJECT(PostMessageAppender)

 

bgsyslogging::AppWindowAppender::AppWindowAppender() {

}

 

bgsyslogging::AppWindowAppender::~AppWindowAppender() {}

...

 

 

Regards,

Greg P.

 

From: Donetti, John G. [mailto:Donetti1@llnl.gov] 
Sent: Thursday, August 04, 2011 3:51 PM
To: log4cxx-user@logging.apache.org
Subject: Custom Appender Issue

 

I am attempting to add a simple custom appender to my code. I have been
able to compile the appender, however, I cannot get the logger to find
the new appender.

 

I get the error: 

log4cxx: Could not create an Appender. Reported error follows.

log4cxx: Class not found: bgsyslogging::AppWindowAppender

log4cxx: No appender named [AppWindowAppender] could be found.

 

My log4cxx config file looks like:

 

 

<?xml version="1.0" encoding="UTF-8" ?>

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <!-- Output the log message to system console. 

    -->

 

  <appender name="ConsoleAppender"
class="org.apache.log4j.ConsoleAppender">

    <param name="Target" value="System.out"/>

    <param name="Threshold" value="DEBUG"/>

    <layout class="org.apache.log4j.PatternLayout">

      <param name="ConversionPattern" value="%-5p - %m (%F:%L) %n"/>

    </layout>

  </appender>

 

 

  <appender name="AppWindowAppender"
class="bgsyslogging::AppWindowAppender">

    <!-- <param name="Threshold" value="INFO"/> -->

    <layout class="org.apache.log4j.PatternLayout">

      <param name="ConversionPattern" value="%d{MMM dd yyyy
HH:mm:ss,SSS} [%-5p] - %m %n"/> -->

    </layout>    

  </appender>

 

  <!-- Setup the root category, add the appenders and set the default
level 

            5 level of logging,  ALL < DEBUG < INFO < WARN < ERROR <
FATAL < OFF

           The root level is set with INFO, which mean any message
greater or same

           as INFO will be log down, in this case, DEBUG is not logged. 

            To log all regardless of logging level, set <priority
value="ALL">

       To shut off the logging of all messages, set <priority
value="OFF">

        -->

 

  <root>

    <priority value="ALL" />

    <appender-ref ref="ConsoleAppender"/>

    <appender-ref ref="AppWindowAppender"/>

  </root>

 

</log4j:configuration>

 

The .h file looks like:

 

#ifndef APPWINDOWAPPENDER_H

#define APPWINDOWAPPENDER_H

 

 

 

#include "log4cxx/logger.h"

#include "log4cxx/basicconfigurator.h"

#include "log4cxx/propertyconfigurator.h"

#include "log4cxx/helpers/exception.h"

#include "Log4cxx/xml/domconfigurator.h"

#include "log4cxx/appenderskeleton.h"

#include "log4cxx/spi/loggingevent.h"

#include "log4cxx/spi/errorhandler.h"

#include <vector>

 

using namespace log4cxx;

 

namespace bgsyslogging

{

       /**

        An appender that appends logging events to a vector.

        */

        class AppWindowAppender : public AppenderSkeleton

        {

                    public:

                DECLARE_LOG4CXX_OBJECT(AppWindowAppender)

                BEGIN_LOG4CXX_CAST_MAP()

                        LOG4CXX_CAST_ENTRY(AppWindowAppender)

                        LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)

                END_LOG4CXX_CAST_MAP()

 

                           AppWindowAppender();

                           ~AppWindowAppender();

 

                /**

                This method is called by the AppenderSkeleton#doAppend

                method.

                */

                void append(const spi::LoggingEventPtr& event,
log4cxx::helpers::Pool& p);

 

 

                void close();

 

                bool isClosed() const

                        { return closed; }

 

                bool requiresLayout() const

                        { return true;   }

 

        };

 

}

#endif // APPWINDOWAPPENDER_H

 

And the .cpp file looks like:

 

#include "AppWindowAppender.h"

#include "log4cxx/helpers/thread.h"

#include "log4cxx/helpers/transcoder.h"

#include <QString>

using namespace log4cxx;

using namespace log4cxx::helpers;

 

 

bgsyslogging::AppWindowAppender::AppWindowAppender() {

}

 

bgsyslogging::AppWindowAppender::~AppWindowAppender() {}

 

void bgsyslogging::AppWindowAppender::append(const spi::LoggingEventPtr&
event, Pool& p)

{

       if ( this->layout == NULL ) {

             LOG4CXX_ENCODE_CHAR(nameStr, name);

             std::string msg("No Layout set for the appender named [ ");

             msg.append(nameStr);

             msg.append(" ].");

 

             LOG4CXX_DECODE_CHAR(msgL, msg);

             errorHandler->error(msgL);

           return;

       }

 

       log4cxx::LogString fMsg;

       this->layout->format(fMsg, event, p);

       LOG4CXX_ENCODE_CHAR(fMsgStr, fMsg);

       printf("AppWindowAppender: %s", fMsgStr.c_str());

 

}

 

void bgsyslogging::AppWindowAppender::close()

{

        if (this->closed)

        {

                return;

        }

 

        this->closed = true;

}

 

 

What have I forgot to do? What have I done wrong? Any help would be
greatly appreciated.

 

 

 

John G. Donetti

Lawrence Livermore National Laboratory

Phone: 925 423-6009

Email: donetti1@llnl.gov