Menu

How can I close the stdout?

2001-11-01
2003-04-08
  • Yan Shaohui

    Yan Shaohui - 2001-11-01

    I want to log all information into a file. The FileAppender class display the content in stdout when it writes to the specified file. How can I mask the stdout?

    Yan Shaohui

     
    • Bastiaan Bakker

      Bastiaan Bakker - 2001-11-01

      Hi,

      I cannot duplicate the behaviour you're describing. Please submit a small example + platform information in the bug tracker and I'll have a look.

      Bastiaan Bakker

       
    • Yan Shaohui

      Yan Shaohui - 2001-11-01

      Here is the src code,

      /* sample.cpp */
      #include "log4cpp/Portability.hh"
      #include <iostream>
      #include <log4cpp/Category.hh>
      #include <log4cpp/Appender.hh>
      #include <log4cpp/FileAppender.hh>
      #include <log4cpp/OstreamAppender.hh>
      #ifdef LOG4CPP_HAVE_SYSLOG
      #include <log4cpp/SyslogAppender.hh>
      #endif
      #include <log4cpp/Layout.hh>
      #include <log4cpp/BasicLayout.hh>
      #include <log4cpp/Priority.hh>
      #include <log4cpp/NDC.hh>

      int main() {
          log4cpp::Appender *appender;
          appender = new log4cpp::FileAppender("sample", "log");
          appender->setLayout(new log4cpp::SimpleLayout());
          log4cpp::Category &logfile = log4cpp::Category::getInstance(std::string("Title1"));

          logfile.setAppender(appender);
          logfile.info("Log service starts up...");
          return 0;
      }

      The compiler command,
      gcc -g -c -Wall -O5 -g  -I./include/log4cpp -o sample.o sample.cpp
      gcc -g -O2 -s -o sample sample.o -L.  ./liblog4cpp.a /usr/lib/libstdc++.so.2.9 -lpthread

      The result displays in screen:
      bash$ ./sample
      INFO - Log service starts up...
      bash$

      And the "log" file's content:
      INFO - Log service starts up...
      INFO - Log service starts up...

      I want all the content written in the "log" file, and at the same the mask the stdout in console.

      Thanks.

       
    • Bastiaan Bakker

      Bastiaan Bakker - 2001-11-01

      Ah, I see now. The logging to stdout is performed by the root Category. This is a consequence of the way log4cpp works: if you log something to a Category (say "Title1") the LoggingEvent also passes through its parents Category and eventually the root Category. This is called 'additivity' and may be turned of with the 'setAdditivity' method.
      Now, by default the root Category has an Appender which logs to stderr.
      To solve your problem you could replace the line logfile.setAppender(appender); 

      with
      log4cpp::Category::getRoot().setAppender(appender);

      Alternatively you could disable the upward passing of LoggingEvents by doing:
      logfile.setAdditivity(false);

      If you update to the CVS HEAD revision you can also disable any logging by the root Category by setting its Appender to NULL, but this is not recommended because it simply discards all LoggingEvents without warning.

      Good luck,

      Bastiaan

       
    • Marc Pound

      Marc Pound - 2003-02-12

      This also works:

      logfile.getRoot().removeAllAppenders();

      I dislike this stderr behavior of the root category.  Log4cpp
      gives you all this wonderful control of how and where to log,
      then by default takes it away by forcing messages to stderr.

      Marc

       
    • Lloyd Rahimisa

      Lloyd Rahimisa - 2003-04-08

      Hi,
      Suppose I create two loggers:
      log4cpp::Category &logger1 = log4cpp::Category::getInstance(std::string("logger1"));
      log4cpp::Category &logger2 = log4cpp::Category::getInstance(std::string("logger1.logger2"));

      If the additivity flag is set to true, then I noticed that log statements from logger2 are logged twice.

      Is it possible to disable this?

      lloyd

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.