0

I have a properties-based log4cxx called Log4cxxConfig.cfg. In it I added a named logger called log4j.daemon. I also created an appender called QTermDaemonAppender. Here is a portion of the cfg file:

log4j.rootLogger=info, rootFileAppender
log4j.StationControllertest=trace, rootConsoleAppender, rootFileAppender
log4j.log4test=trace, rootConsoleAppender
log4j.daemon=trace, QTermDaemonAppender

# QTermDaemonAppender
log4j.appender.QTermDaemonAppender.File=qterm_daemon.log
log4j.appender.QTermDaemonAppender.Append=true            # set whether to overwrite or append to the file
log4j.appender.QTermDaemonAppender.MaxFileSize=10000KB
log4j.appender.QTermDaemonAppender.MaxBackupIndex=10
log4j.appender.QTermDaemonAppender=org.apache.log4j.RollingFileAppender
log4j.appender.QTermDaemonAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QTermDaemonAppender.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

In my C++ code I have tried everything I can think of to get my output to write to qterm_daemon.log. The file is not created in addition to having nothing written to it.

LoggerPtr logger(Logger::getLogger("daemon"));

int main(int argc, char* argv[]) 
{
    // Initialise logger. Use the configuration file named 'log4cxx.properties'.
    PropertyConfigurator::configureAndWatch("Log4cxxConfig.cfg");

I've tried the above. I've tried qualifying the name of the cfg file like "./Log4cxxConfig.cfg". I've searched my path for duplicates of this file. I've tried using the logger name "log4j.daemon" instead of "daemon".

I've tried the following:

int main(int argc, char* argv[])
{
    // Initialise logger. Use the configuration file named 'log4cxx.properties'.
    PropertyConfigurator::configureAndWatch("Log4cxxConfig.cfg");
    LoggerPtr logger(Logger::getLogger("log4j.daemon"));

Again, with various combinations of qualifying the path and changing the name of the logger name. No joy! What in the heck am I doing wrong? I can get this to work like a champ with Java and log4j1.4 on an AS400. BTW, this is with Raspbian and the new Pi 2.

Kelly Beard
  • 684
  • 1
  • 8
  • 20

1 Answers1

0

OK, I got this setup to work.

The code:

LoggerPtr logger(Logger::getLogger("daemon"));

int main(int argc, char* argv[])
{
    // Initialise logger. Use the configuration file named 'Log4cxxConfig.cfg'.
    PropertyConfigurator::configureAndWatch("Log4cxxConfig.cfg");

and the config file:

log4j.logger.daemon=trace, QTermDaemonAppender

# QTermDaemonAppender
log4j.appender.QTermDaemonAppender.File=qterm_daemon3.log
log4j.appender.QTermDaemonAppender.Append=true            # set whether to overwrite or append to the file
log4j.appender.QTermDaemonAppender.MaxFileSize=10000KB
log4j.appender.QTermDaemonAppender.MaxBackupIndex=10
log4j.appender.QTermDaemonAppender=org.apache.log4j.RollingFileAppender
log4j.appender.QTermDaemonAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QTermDaemonAppender.layout.ConversionPattern=%5p %d [%t] (%F:%L) - %m%n

The key was getting the logger name right and specifying it correctly in the code. I'm sure there is some namespace thing going on since you have to add the ".logger" portion to your logger name. I'll try and research that to see if it's true. Using a string like "logger.daemon" or "log4j.logger.daemon" didn't work.

In any case, my log file, qterm_daemon3.log is created and output written to it. Apparently the "log4j" is the correct prefix to use even for log4cxx. I had found an example online that used "log4cplus" and that just caused a "please initialize the log4cxx system properly".

Kelly Beard
  • 684
  • 1
  • 8
  • 20