2011-05-07 379 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 
相关问题