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函數