2011-05-07 35 views

回答

18
[%.7thread] 

這將打印線程名的最後 7個字符。

線程名稱的尾隨字符通常比線程名稱開頭的字符更有趣。 :-)

您可以在logback文檔中閱讀maximum field width modifier

+0

我剛打開%線程新的功能要求{長度}。 http://jira.qos.ch/browse/LOGBACK-813 – 2013-03-20 16:04:56

1

我不認爲存在{x}語法爲%thread的相當,但你可以使用像%.-5thread只打印線程名稱的前5個字符,但只有5,而不是一個縮短版(顯然,根據您的需要調整數量)。

4

您可以使用:

[%.-10t] 

這個表達式打印線程名稱的第一個字符10。

1

這個在你的線程開始時如何?

if (Thread.currentThread().getName().split("-").length > 1) { 
    String threadName = Thread.currentThread().getName(); 
    threadName = "thread" + threadName.split("-")[3]; 
    Thread.currentThread().setName(threadName); 
} 

然後在您的logback.xml:

[%-8thread] %msg%n 

...你會得到這樣的事情:

[main ] Process started 
[thread1 ] Thread started 
[thread2 ] Thread started 
[thread1 ] Thread ended 
[thread1 ] Thread started 
[thread2 ] Thread ended 
[thread2 ] Thread started 
[thread1 ] Thread ended 
[thread2 ] Thread ended 
[thread3 ] Thread ended 
[thread4 ] Thread ended 
[thread5 ] Thread ended 
[main ] Process ended 
相關問題