Failed to use log4crc configuration file

1.7k views Asked by At

I am trying to use log4c for my C application logging. For implementing rolling policy, I used log4c rolling file APIs. I understood from Log4C developers guide that the rolling policy could be achieved by editing the configuration file (log4crc). I tried editing the appender name to a log file path in the configuration file. But the application run without logging. Can anyone please tell me how to implement rolling log mechanism using log4c?

Please see my helloworld application sample and the configuration file below:

#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;
}

The configuration file looks as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE log4c SYSTEM "">

<log4c version="1.2.3">

        <config>
               <bufsize>0</bufsize>
               <debug level="2"/>
               <nocleanup>0</nocleanup>
               <reread>1</reread>
        </config>

        <category name="root" priority="notice"/>
        <category name="six13log.log" priority="error" appender="stdout" />

        <rollingpolicy name="myrollingpolicy" type="sizewin" maxsize="1024" maxnum="10" />

        <appender name="myrollingfileappender" type="rollingfile" logdir="." prefix="myprefix" layout="dated" rollingpolicy="myrollingpolicy" />

        <appender name="stdout" type="stream" layout="basic"/>
        <appender name="stderr" type="stream" layout="dated"/>
        <appender name="syslog" type="syslog" layout="basic"/>


        <appender name="s13file" type="s13_file" layout="basic"/>
        <appender name="plain_stderr" type="s13_stderr" layout="none"/>
        <appender name="cat_stderr" type="s13_stderr" layout="catlayout"/>
        <appender name="xml_stderr" type="s13_stderr" layout="xmllayout"/>
        <appender name="user_stderr" type="s13_stderr" layout="userlayout"/>

        <layout name="basic" type="basic"/>
        <layout name="dated" type="dated"/>

        <layout name="catlayout" type="s13_cat"/>
        <layout name="xmllayout" type="s13_xml"/>
        <layout name="none" type="s13_none"/>
        <layout name="userlayout" type="s13_userloc"/>

        <category name="six13log.log.app.application2" priority="debug" appender="cat_stderr" />
        <category name="six13log.log.app.application3" priority="debug" appender="user_stderr" />
        <category name="six13log.log.app" priority="debug" appender="myrollingfileappender" />

        <category name="log4c.examples.helloworld" priority="debug" appender="stdout"/>
        <category name="log4c.examples.helloworld" priority="debug" appender="/home .. /..filename.txt"/>

 </log4c>

I edited the last two lines in the configuration file for logging. Please let me know what is wrong with my log4crc configuration file and how I can use configuration file to implement logging without using log4c rolling policy apis?

3

There are 3 answers

0
Sandeep On

I am also new to log4c. But what I understand from your log4c configuration file you have category name "log4c.examples.helloworld" twice. So when you write log4c_category_get("log4c.examples.helloworld") it is going to check from CRC file and will get appender name. Corresponding to log4c.examples.helloworld the appender names are "stdout" and "/home .. /..filename.txt". First one is proper as it will give output to screen. The last one is improper. Do not specify file path there. Put name/ string that will correspoond to appender name. And then in appender name you have logdir value where you can specify the directory where your file will get created.

0
Asghar H On

Removing the version number completely works for me:

Change 
    <log4c version="1.2.3">
to
    <log4c>
0
GShaik On

Check whether your log4crc file is included in the helloworld directory