【问题标题】:Failed to use log4crc configuration file使用 log4crc 配置文件失败
【发布时间】:2013-08-20 14:39:43
【问题描述】:

我正在尝试将 log4c 用于我的 C 应用程序日志记录。为了实现滚动策略,我使用了 log4c 滚动文件 API。我从 Log4C 开发人员指南中了解到,滚动策略可以通过编辑配置文件(log4crc)来实现。我尝试将附加程序名称编辑到配置文件中的日志文件路径。但是应用程序在没有记录的情况下运行。谁能告诉我如何使用 log4c 实现滚动日志机制?

请看我的 helloworld 应用示例和下面的配置文件:

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

配置文件如下:

<?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>

我编辑了配置文件中的最后两行以进行日志记录。 请让我知道我的 log4crc 配置文件有什么问题,以及如何在不使用 log4c 滚动策略 API 的情况下使用配置文件来实现日志记录?

【问题讨论】:

    标签: policy log4c


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        删除版本号完全适合我:

        Change 
            <log4c version="1.2.3">
        to
            <log4c>
        

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 2020-04-27
          • 2021-07-10
          • 1970-01-01
          • 1970-01-01
          • 2013-11-27
          • 2013-12-28
          • 1970-01-01
          • 2015-04-08
          相关资源
          最近更新 更多