1

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?

4

3 回答 3

0

我也是 log4c 的新手。但是我从您的 log4c 配置文件中了解到,您有"log4c.examples.helloworld"两次类别名称。因此,当您编写时,log4c_category_get("log4c.examples.helloworld")它将从 CRC 文件中检查并获取附加程序名称。对应于log4c.examples.helloworldappender 名称的是"stdout""/home .. /..filename.txt"。第一个是正确的,因为它将输出到屏幕。最后一个是不合适的。不要在那里指定文件路径。放置与附加程序名称相对应的名称/字符串。然后在appender namelogdir value可以指定创建文件的目录的位置。

于 2014-12-20T09:42:09.613 回答
0

检查你的 log4crc 文件是否包含在 helloworld 目录中

于 2015-01-22T08:45:24.110 回答
0

删除版本号完全适合我:

Change 
    <log4c version="1.2.3">
to
    <log4c>
于 2017-01-27T18:28:02.050 回答