edwardcmh

人氣不過肥皂泡

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

参考了李嘉昱的这篇博文,在此表示感谢。

1. log4c的使用

log4c_init();   // 初始化log4c
logcat = log4c_category_get(LOG_CATEGORY_NAME);   // 获取一个已有的category
log4c_category_log(logcat, LOG4C_PRIORITY_DEBUG, "Hello World!");  //用该category进行日志输出,优先级为DEBUG,输出信息为 "Hello World!"
log4c_fini();   // 销毁log4c

使用宏定义简化输出过程:

#ifndef LOGGER_H
#define LOGGER_H

#include "log4c.h"

#define LOG_ERROR(category, msg, args...) \
{ \
    const log4c_location_info_t locinfo = LOG4C_LOCATION_INFO_INITIALIZER(NULL); \
    log4c_category_log_locinfo(category, &locinfo, LOG4C_PRIORITY_ERROR, msg, ##args); \
}

#define LOG_WARN(category, msg, args...) \
{ \
    const log4c_location_info_t locinfo = LOG4C_LOCATION_INFO_INITIALIZER(NULL); \
    log4c_category_log_locinfo(category, &locinfo, LOG4C_PRIORITY_WARN, msg, ##args); \
}

#define LOG_INFO(category, msg, args...) \
{ \
    const log4c_location_info_t locinfo = LOG4C_LOCATION_INFO_INITIALIZER(NULL); \
    log4c_category_log_locinfo(category, &locinfo, LOG4C_PRIORITY_INFO, msg, ##args); \
}

#define LOG_DEBUG(category, msg, args...) \
{ \
    const log4c_location_info_t locinfo = LOG4C_LOCATION_INFO_INITIALIZER(NULL); \
    log4c_category_log_locinfo(category, &locinfo, LOG4C_PRIORITY_DEBUG, msg, ##args); \
}

#define LOG_TRACE(category, msg, args...) \
{ \
    const log4c_location_info_t locinfo = LOG4C_LOCATION_INFO_INITIALIZER(NULL); \
    log4c_category_log_locinfo(category, &locinfo, LOG4C_PRIORITY_TRACE, msg, ##args); \
}

#endif // LOGGER_H

2. log4c的配置

log4c的配置文件名为:log4crc,是一个XML文件,存放在工程目录中即可。

常用的配置文件例子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE log4c SYSTEM "">
<log4c version="1.2.1">
	<config>
		<bufsize>0</bufsize>
		<debug level="2"/>
		<nocleanup>0</nocleanup>
		<reread>1</reread>
	</config>
	<category name="root" priority="notice"/>
	<category name="log4c.examples.helloworld" priority="debug" appender="myrollingfileappender"/>

	<appender name="myrollingfileappender" type="rollingfile" logdir="./" prefix="mylogfile" layout="basic" rollingpolicy="myrollingpolicy" />
	<appender name="stdout" type="stream" layout="basic"/>

	<layout name="basic" type="basic"/>
	<layout name="dated" type="dated"/>
	
	<rollingpolicy name="myrollingpolicy" type="sizewin" maxsize="10400" maxnum="10" />
</log4c>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE log4c SYSTEM "">
<log4c version="1.2.1">
    <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"/>

    <!--输出到控制台-->
    <category name="WLAN_Console" priority="trace" appender="stdout"/>

    <!--保存日志到文件-->
    <category name="WLAN_File" priority="trace" appender="myrollingfileappender"/>

    <!--logdir为日志输出路径  prefix为文件名  layout为输出格式--> 
    <appender name="myrollingfileappender" type="rollingfile" logdir="." prefix="wlan_log" layout="dated" rollingpolicy="myrollingpolicy"/>
  
    <!--sizewin表示达到最大值后新建日志文件  值由maxsize设定,单位Bytes  maxnum为最大文件数目-->
    <rollingpolicy name="myrollingpolicy" type="sizewin" maxsize="102400" maxnum="10"/>
    
    <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"/>
</log4c>
posted on 2012-03-09 18:37  edwardcmh  阅读(2052)  评论(0编辑  收藏  举报