output FILE test_logconf.log level INFO styles DATETIME|LOGLEVEL|PID|TID|SOURCE|FORMAT|NEWLINE options CHANGE_TEST rotate_mode SIZE rotate_size 10MB log_bufsize 1MB 5MB在代码里这样写就实现了装载配置文件来配置日志句柄
#include "LOGCONF.h" LOG *g = NULL ; g = CreateLogHandleFromConfig( "test_logconf.conf" , NULL ) ; ...函数CreateLogHandleFromConfig内部逻辑是读取简单日志配置文件,依次调用创建、设置函数构建日志句柄,也就是等价于:
SetLogOutput( g , LOG_OUTPUT_FILE , "test_logconf.log" , LOG_NO_OUTPUTFUNC ); SetLogLevel( g , LOG_LEVEL_INFO ); SetLogStyles( g , LOG_STYLE_DATETIME|LOG_STYLE_LOGLEVEL|LOG_STYLE_PID|LOG_STYLE_TID|LOG_STYLE_SOURCE|LOG_STYLE_FORMAT|LOG_STYLE_NEWLINE , LOG_NO_STYLEFUNC ); SetLogOptions( g , LOG_OPTION_CHANGE_TEST ); SetLogRotateMode( g , LOG_ROTATEMODE_SIZE ); SetLogRotateSize( g , 10*1024*1024 ); SetLogBufferSize( g , 1*1024*1024 , -1 );应用输出日志和关闭日志句柄照常。
#include "LOGCONF.h" int main( int argc , char *argv[] ) { LOG *g = NULL ; g = CreateLogHandleFromConfig( argv[0] , ".conf" ) ; ... }这样就能实现可执行程序my_program和所在目录下有一个主文件名一样的对应日志配置文件my_program.conf协同工作。
id hello output FILE test_logconf.log level INFO styles DATETIME|LOGLEVEL|PID|TID|SOURCE|FORMAT|NEWLINE options CHANGE_TEST rotate_mode SIZE rotate_size 10MB log_bufsize 1MB 5MB id stdout output STDOUT level INFO styles DATETIME|LOGLEVEL|PID|TID|SOURCE|FORMAT|NEWLINE代码中这样调用
#include "LOGSCONF.h" LOGS *gs = NULL ; gs = CreateLogsHandleFromConfig( "test_logsconf.conf" , NULL ) ; ...Log变成Logs即可,很好记,不是吗? ^_^
<log id="access"> <output>FILE</output> <filename>$HOME$/log/access.log"</filename> <level>DEBUG</level> <styles>DATETIME|LOGLEVEL|PID|TID|FORMAT|NEWLINE</styles> <rotate_mode>SIZE</rotate_mode> ; <rotate_size>10MB</rotate_size> </log>或者是我原创的SML标记语言
log ( id = access ) { output = FILE ; filename = "$HOME$/log/access.log" ; level = DEBUG ; styles = "DATETIME|LOGLEVEL|PID|TID|FORMAT|NEWLINE" ; rotate_mode = SIZE ; rotate_size = 10MB ; }
access.DEBUG "$HOME$/log/access.log",10MB,"DATETIME|LOGLEVEL|PID|TID|FORMAT|NEWLINE"再或者是你所在项目要求的特定的统一格式。
_WINDLL_FUNC int ConvertLogOutput_atoi( char *output_desc , int *p_log_output ); _WINDLL_FUNC int ConvertLogLevel_atoi( char *log_level_desc , int *p_log_level ); _WINDLL_FUNC int ConvertLogStyle_atol( char *line_style_desc , long *p_line_style ); _WINDLL_FUNC int ConvertLogOption_atol( char *log_option_desc , long *p_log_option ); _WINDLL_FUNC int ConvertLogRotateMode_atoi( char *rotate_mode_desc , int *p_rotate_mode ); _WINDLL_FUNC int ConvertBufferSize_atol( char *bufsize_desc , long *p_bufsize );最后调用日志句柄创建、设置函数构造日志句柄即可。
hetao { logs { log ( id = access ) { output = FILE ; filename = "$HOME$/log/access.log" ; level = DEBUG ; styles = "DATETIME|LOGLEVEL|PID|TID|FORMAT|NEWLINE" ; rotate_mode = SIZE ; rotate_size = 10MB ; } log ( id = error ) { output = FILE ; filename = "$HOME$/log/error.log" ; level = ERROR ; styles = "DATETIME|LOGLEVEL|SOURCE|FORMAT|NEWLINE" ; rotate_mode = SIZE ; rotate_size = 10MB ; } } module { connection ( filename = "$HOME$/so/hetao_module_connection_epoll.so" ) { } protocol ( filename = "$HOME$/so/hetao_module_protocol_http.so" ) { max_header_size = 10KB ; root = "$HOME$/www" ; index_page = "index.html" ; error_pages { error_page { error = 403 ; page = "$HOME$/www/403.html" } ; error_page { error = 404 ; page = "$HOME$/www/404.html" } ; error_page { error = 500 ; page = "$HOME$/www/500.html" } ; error_page { error = 505 ; page = "$HOME$/www/505.html" } ; } } } server { listen_ip = "127.0.0.1" ; listen_port = 8080 ; max_connections = 1024 ; } }在代码中这样写即可迅速自动构建出日志句柄集合
gs = CreateLogsHandleFromConfig_SML( "hetao.conf" , "/hetao/logs" , NULL ) ;CreateLogsHandleFromConfig_SML是我另外编写的iLOG3配置SML格式函数库iLOG3CONF_SML函数