開源日誌系統log4cplus 三

2021-09-08 16:10:01 字數 4794 閱讀 3828

本文介紹了三種控制輸出格式的布局管理器的概念和使用情況,通過掌握這些知識,可以更有效地控制log系統輸出盡可能貼近你需求的資訊來。

### 如何控制輸出訊息的格式 ###

前面已經講過,log4cplus通過布局器(layouts)來控制輸出的格式,log4cplus提供了三種型別的layouts,

分別是******layout、patternlayout、和ttcclayout。其中:

1. ******layout

是一種簡單格式的布局器,在輸出的原始資訊之前加上loglevel和乙個"-"。

比如以下**片段:
... ...
/* step 2: instantiate a layout object */

std::auto_ptr  _layout(new log4cplus::******layout());

/* step 4: instantiate a logger object */

logger _logger = logger::getinstance("test");

/* log activity */

log4cplus_debug(_logger, "this is the ****** formatted log message...")

... ...

將列印結果:

debug - this is the ****** formatted log message...

2. patternlayout

是一種有詞法分析功能的模式布局器,一提起模式就會想起正規表示式,這裡的模式和正規表示式類似,但是

遠比後者簡單,能夠對預定義的識別符號(稱為conversion specifiers)進行解析,轉換成特定格式輸出。以下

**片段演示了如何使用patternlayout:

... ...
/* step 4: instantiate a logger object */

logger _logger = logger::getinstance("test_logger.subtest");

/* log activity */

log4cplus_debug(_logger, "teststr")

... ...

輸出結果:

10/16/04 18:51:25  - teststr [main.cpp:51]

可以看出通過填寫特定格式的模式字串"pattern",原始資訊被包含到一堆有格式的資訊當中了,這就使得

使用者可以根據自身需要來定製顯示內容。"pattern"可以包含普通字串和預定義的識別符號,其中:

(1)普通字串,能夠被直接顯示的資訊。

(2)預定義識別符號,通過"%"與乙個或多個字元共同構成預定義的識別符號,能夠產生出特定格式資訊。

關於預定義識別符號,log4cplus文件中提供了詳細的格式說明,我每種都試了一下,以上述**為例,根據不同

的pattern,各種訊息格式使用情況列舉如下:

(1)"%%",轉義為%, 即,std::string pattern = "%%" 時輸出: "%"

(2)"%c",輸出logger名稱,比如std::string pattern ="%c" 時輸出: "test_logger.subtest",

也可以控制logger名稱的顯示層次,比如"%c"時輸出"test_logger",其中數字表示層次。

(3)"%d",顯示本地時間,當std::string pattern ="%d" 時輸出:"2004-10-16 18:55:45",%d顯示標準時間,

所以當std::string pattern ="%d" 時輸出 "2004-10-16 10:55:45" (因為我們是東8區,差8個小時啊)。

可以通過%d定義更詳細的顯示格式,比如%d表示要顯示小時:分鐘:秒。大括號中可顯示的

預定義識別符號如下:

%a -- 表示禮拜幾,英文縮寫形式,比如"fri"

%a -- 表示禮拜幾,比如"friday"

%b -- 表示幾月份,英文縮寫形式,比如"oct"

%b -- 表示幾月份,"october"

%c -- 標準的日期+時間格式,如 "sat oct 16 18:56:19 2004"

%d -- 表示今天是這個月的幾號(1-31)"16"

%h -- 表示當前時刻是幾時(0-23),如 "18"

%i -- 表示當前時刻是幾時(1-12),如 "6"

%j -- 表示今天是哪一天(1-366),如 "290"

%m -- 表示本月是哪一月(1-12),如 "10"

%m -- 表示當前時刻是哪一分鐘(0-59),如 "59"

%p -- 表示現在是上午還是下午, am or pm

%q -- 表示當前時刻中毫秒部分(0-999),如 "237"

%q -- 表示當前時刻中帶小數的毫秒部分(0-999.999),如 "430.732"

%s -- 表示當前時刻的多少秒(0-59),如 "32"

%u -- 表示本週是今年的第幾個禮拜,以週日為第一天開始計算(0-53),如 "41"

%w -- 表示禮拜幾,(0-6, 禮拜天為0),如 "6"

%w -- 表示本週是今年的第幾個禮拜,以周一為第一天開始計算(0-53),如 "41"

%x -- 標準的日期格式,如 "10/16/04"

%x -- 標準的時間格式,如 "19:02:34"

%y -- 兩位數的年份(0-99),如 "04"

%y -- 四位數的年份,如 "2004"

%z -- 時區名,比如 "gmt"

(4)"%f",輸出當前記錄器所在的檔名稱,比如std::string pattern ="%f" 時輸出: "main.cpp"

(5)"%l",輸出當前記錄器所在的檔案行號,比如std::string pattern ="%l" 時輸出: "51"

(6)"%l",輸出當前記錄器所在的檔名稱和行號,比如std::string pattern ="%l" 時輸出:

"main.cpp:51"

(7)"%m",輸出原始資訊,比如std::string pattern ="%m" 時輸出: "teststr",即上述**中

log4cplus_debug的第二個引數,這種實現機制可以確保原始資訊被嵌入到帶格式的資訊中。

(8)"%n",換行符,沒什麼好解釋的

(9)"%p",輸出loglevel,比如std::string pattern ="%p" 時輸出: "debug"

(10)"%t",輸出記錄器所在的執行緒id,比如std::string pattern ="%t" 時輸出: "1075298944"

(11)"%x",巢狀診斷上下文ndc (nested diagnostic context) 輸出,從堆疊中彈出上下文資訊,ndc可以用對

不同源的log資訊(同時地)交叉輸出進行區分,關於ndc方面的詳細介紹會在下文中提到。

(12)格式對齊,比如std::string pattern ="%-10m"時表示左對齊,寬度是10,此時會輸出"teststr   ",當

然其它的控制字元也可以相同的方式來使用,比如"%-12d","%-5p"等等(剛接觸log4cplus文件時還以為

"%-5p"整個字串代表loglevel呢,呵呵)。

3. ttcclayout

是在patternlayout基礎上發展的一種預設的帶格式輸出的布局器, 其格式由時間,執行緒id,logger和ndc 組

成(consists of time, thread, logger and nested diagnostic context information, hence the name),

因而得名(怎麼得名的?logger裡**有那個"c"的縮寫啊!名字起得真夠爛的,想扁人)。提供給那些想顯示

典型的資訊(一般情況下夠用了)又懶得配置pattern的同志們。

ttcclayout在構造時有機會選擇顯示本地時間或gmt時間,預設是按照本地時間顯示:

ttcclayout::ttcclayout(bool use_gmtime  = false)

以下**片段演示了如何使用ttcclayout:
... ...
/* step 2: instantiate a layout object */

std::auto_ptr _layout(new ttcclayout());

/* step 4: instantiate a logger object */

logger _logger = logger::getinstance("test_logger");

/* log activity */

log4cplus_debug(_logger, "teststr")

... ...

輸出結果:

10-16-04 19:08:27,501 [1075298944] debug test_logger <> - teststr

當構造ttcclayout物件時選擇gmt時間格式時:

... ...

/* step 2: instantiate a layout object */

std::auto_ptr _layout(new ttcclayout(true));

... ...

輸出結果:

10-16-04 11:12:47,678 [1075298944] debug test_logger <> - teststr

開源日誌系統 log4cplus 二

本文介紹了使用log4cplus有六個步驟,並提供了一些例子引導你了解log4cplus的基本使用。基本使用 使用log4cplus有六個基本步驟 下面通過一些例子來了解log4cplus的基本使用。using namespace log4cplus using namespace log4cplu...

開源日誌系統 log4cplus 七

經過短暫的熟悉過程,log4cplus已經被成功應用到了我的專案中去了,效果還不錯,除了上文提及的 功能之外,下面將介紹log4cplus提供的執行緒和套接字的使用情況。ndc 首先我們先了解一下log4cplus中嵌入診斷上下文 nested diagnostic context 即ndc。對lo...

開源日誌系統 log4cplus 六

一些可以改進之處 1.使用者自定義loglevel的實現機制不夠開放在第五篇中曾經介紹過如何實現使用者自行定義loglevel,為了實現比較理想的效果,甚至還需要改log4cplus 的源 2.生成logger物件的機制可以改進我在使用時候,經常需要在不同的檔案 函式中操作同乙個logger,雖然l...