[英]Log4j2.xml doesn't create file suddenly in java hibernate framework
我在帶有休眠框架的NetBeans中有一個Maven項目。 長期以來,它一直在正確地完成工作。 在數據庫中進行一些修改后,控制台日志仍可正常工作,但已停止寫入文件,沒有任何錯誤或異常。 我已經刪除了日志文件目錄並重新啟動了項目,但是這次它甚至無法創建文件路徑目錄。 這是我的log4j2.xml配置(在src / main / resources中)
我將路徑更改為D:\\ logs和D:/ logs,並嘗試了不同的級別(調試,信息,錯誤)。 還嘗試由管理員從命令行運行我的“ Server.jar”。 應該提到的是,我嘗試在Log4J中創建該日志文件但未寫入日志的解決方案以及許多其他建議的解決方案,但均未成功。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="path">${sys:user.home}/logs/server</Property>
</Properties>
<Appenders >
<RollingFile name="connections" append="true" fileName="${path}/connections.log"
filePattern="${path}/connections-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="unexpected-events" append="true" fileName="${path}/unexpected-events.log"
filePattern="${path}/unexpected-events-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="readouts" append="true" fileName="${path}/readouts.log"
filePattern="${path}/readouts-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="connections" level="info" additivity="false">
<appender-ref ref="connections" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="unexpected-events" level="info" additivity="false">
<appender-ref ref="unexpected-events" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="readouts" level="info" additivity="false">
<appender-ref ref="readouts" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Root level="error" additivity="false">
<!--<AppenderRef ref="console-log"/>-->
</Root>
</Loggers>
</Configuration>
這就是我在java類中使用它的方式:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
private static final Logger connectionsLog = LogManager.getLogger("connections");
....
connectionsLog.info("device" + deviceNumber + " disconnected");
當commons-logging
和jcl-over-slf4j
一起放在類路徑中時,我已經看到了這樣的怪異jcl-over-slf4j
。 從Maven類路徑中排除commons-logging
,通常會有所幫助。
特別感謝user944849,我了解到該問題可能與maven依賴關系有關。 然后,我檢查了項目的先前版本(具有正確的日志記錄)並比較了依賴關系,發現添加了一個庫“ log4j-sf4j”(由於導入了“ org.springframework.boot”而添加了庫)。 然后首先我將其從pom.xml文件的依賴項中排除,因此,它在NetBeans IDE中可以正常工作,但不能超出它。 然后,我從pom.xml中刪除了該依賴關系,並像魅力一樣解決了問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.