3

New to log4cxx. I have a configuration file where I define the log4cxx configuration. That works fine. What I want to do now is access an object defined in that config file and extend it's functionality, e.g.

say I have a Log4Cxx implementation that has a Rolling File Appender. What I ultimately want to to do is be able to add triggers to that Appender in the cpp file, to be able to trigger a rollover.
********* log4cxx.xml *********************

..
..
<appender name="rfa" class="org.apachelog4j.rolling.RollingFileAppender">
   <rollingPolicy class="org.apache.log4j.rolling.rollingPolicy">
     <param name="file" value="logfiles/logfile%d{yyyy-MM-dd}.txt" /> 
   </rollingPolicy>
   <triggeringPolicy
      class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
      <param name="MaxFileSize" value="10MB"
   </triggeringPolicy>
</appender>

My question is

  1. How can I modify/add to this appender in the code file. What I've been trying is something like

*********LogImplementation.cpp ******* '''

#include "LogImplementation.h"
.. 
#include "log4cxx/rolling/rollingpolicy.h"
#include "log4cxx/rolling/rollingfileappender.h"
#include "log4cxx/rolling/filterbasedtriggeringpolicy.h"
#include "log4cxx/helpers/pool.h"
#include "log4cxx/spi/filter.h"
#include "log4cxx/rolling/rollingpolicy.h"
#include "log4cxx/logger.h"

using namespace log4cxx;
using namespace log4cxx::rolling;
using namespace log4cxx::helpers; 
 ..

void LogImplemetation::addFilter()
{
    FilterBasedTriggeringPolicyPtr fbtp = FilterBasedTriggeringPolicyPtr(new FilterBasedTriggeringPolicy());
 
    log4cxx::filter::StringMatchFilter tf;
    tf.setStringToMatch("filterText");
    tf.setAcceptOnMatch(true);

    fbtp->addFilter(&tf);

    Pool p;
    fbtp->activateOptions(p); 
    rfa->setTriggeringPolicy(fbtp);

}

'''

but I'm getting a compilation error because it doesn't recognize "rfa" as an rolling file appender object. I also tried

log4cxx::Appender.rfs->setTriggeringPolicy(fbtp); but I get an "unqualified id before '.' token" compilation error in that case.

Rob_BG
  • 31
  • 3
  • You have to get the appender with [Logger::getAppender](https://logging.staged.apache.org/log4cxx/latest_stable/classlog4cxx_1_1Logger.html#a2657145dd77494712264685b2a6da5cb) before you can modify it. If you're looking to extend it, take a look at the documentation for [extending log4cxx](https://logging.staged.apache.org/log4cxx/latest_stable/extending-log4cxx.html) – rm5248 Nov 08 '21 at 22:27

0 Answers0