Log4j2 使用自定義Logger, Log中打印不出類名
2017-08-29更新:
20:02:
<AsyncLogger name="lottery" level="info" additivity="false" includeLocation="true">
<AppenderRef ref="lotteryLog"/>
</AsyncLogger>
使用非同步日誌需要加上 includeLocation="true" ,解決沒有類名和行數之類的 日誌詳細資訊的問題
但據官網描述,這樣好像就喪失非同步效能方面的優勢了 http://logging.apache.org/log4j/2.x/manual/appenders.html
Extracting location is an expensive operation (it can make logging 5 - 20 times slower). T
o improve performance, location is not included by default when adding a log event to the queue.
You can change this by setting includeLocation="true".
發現時 非同步Logger的問題,,
換成<Logger name="lottery" level="info" additivity="false">
<AppenderRef ref="lotteryLog"/>
</Logger>
就好了。。。
發現getLogger()傳入class,始終能打出類名
若直接使用 傳入 LogManager.getLogger( class name) 時,也可以打出,但使用包裝類巢狀一層就無法打出
LogUtil.get(class name)
public static Logger get(String name) {
return LogManager.getLogger(name);
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
如果想看到具體類和行數的話,還是傳入 類 Class 吧,然後將 自定義logger的name 更改為 對應的包名 或者類名
即將需要 統一記錄的 類放置在同一個包下,使用xxxx.xxx 包名進行 logger的定義,由於父關係,則會被一起打出
配置檔案:
<RollingRandomAccessFile name="lotteryLog" fileName="${LOG_HOME}/lottery.log"
filePattern="${LOG_HOME}/lottery.%d{yyyy-MM-dd}-%i.log.gz" append="true">
<PatternLayout pattern="[main][%date{yyyy-MM-dd HH:mm:ss.SSS}][%thread][%level][%class][%line]:%message%n"/>
<Filters>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
<AsyncLogger name="lottery" level="info" additivity="false">
<AppenderRef ref="lotteryLog"/>
</AsyncLogger>
java:
private final static Logger logger = LogUtil.get(DrawPrizeRemoteServiceImpl.class);
private final static Logger log2 = LogManager.getLogger("lottery");
private static final org.slf4j.Logger log = LoggerFactory.getLogger("lottery");
均可以列印日誌檔案,但發現除了 getLogger方法傳入是 類 或者 傳入的類名在 log4j配置檔案中 能找到匹配的logger 的之外,
其餘打印出的日誌檔案 在 類名 這塊(%class) 和行數(%line) 都是 問號
[main][2016-12-27 00:00:00.003][Pigeon-Server-Request-Processor-34-thread-49][INFO][?][] XXXXXXX
當使用類做引數或有完整類名匹配的logger時,才會打出完整資訊
[2017-08-29 14:13:26.630][Pigeon-Server-Request-Processor-34-thread-3][INFO][xxxxxxxx.DrawPrizeRemoteServiceImpl][89]:dasdsadasdasdasd