9

Can someone tell me how I can use the rolling log feature of log4c API library?

There is only documentation on the functions it provides and there are so many.

If anyone has used rolling log with log4c, would be great to see how to configure it and use it.

2 Answers 2

11

Add something like this to your .log4crc file:

<rollingpolicy name="myrollingpolicy" 
               type="sizewin"
               maxsize="1024"
               maxnum="10"
               />
<appender name="myrollingfileappender"
          type="rollingfile"
          logdir="."
          prefix="myprefix"
          layout="dated"
          rollingpolicy="myrollingpolicy"
          />

Then you do logging like normal with:

#include <stdio.h>
#include "log4c.h"

int main(int argc, char** argv) {
  int rc = 0;
  log4c_category_t* mycat = NULL;

  if (log4c_init()) {
    printf("log4c_init() failed");
    rc = 1;  
  } 
  else{
      mycat = log4c_category_get("log4c.examples.helloworld");

      log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Hello World!");

    /* Explicitly call the log4c cleanup routine */
    if ( log4c_fini()){
      printf("log4c_fini() failed");
    }
  }
  return 0;
}

This is all available in the examples from the log4c source code

2
  • 1
    Helpful, thanks, and good luck for your "neuromancer" badge ;-)
    – philant
    Nov 20, 2009 at 7:20
  • Can the file name contain the pid of the running process? Jun 9, 2022 at 12:44
-1

Since this is a 3 month old question, just wondering if the Wikipedia page was tried -- http://en.wikipedia.org/wiki/Log4c#Development_with_Log4C.

1
  • 1
    Wikipeia redirects to Log4J. They are similar, but not the same. Therefore, there are minimal differences. Apr 24, 2012 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy