我在基于Linux的系统中使用小型应用程序,我使用slf4j库作为记录器
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
我可以看到日志在控制台中可见,但没有附加到日志文件中。
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE}][%-5p][%-10t]%m%n" />
</layout>
</appender>
<!-- EXAMPLE logging setup NOTES: 1. files are written to "./logs/<filename>".
You must setup the environment so that ./logs is a symlink to the correct
location according to the EXAMPLE log standard. For example, "/opt/logs/".
If that's not possible, change the File setting in each appender appropriately. -->
<appender name="EXAMPLE_AUDIT" class="org.apache.log4j.RollingFileAppender">
<param name="threshold" value="INFO" />
<param name="File" value="./logs/audit.log" />
<param name="MaxFileSize" value="128MB" />
<param name="MaxBackupIndex" value="20" />
<layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
<param name="ConversionPattern" value="EXAMPLE_AUDIT" />
</layout>
</appender>
<appender name="EXAMPLE_METRIC" class="org.apache.log4j.RollingFileAppender">
<param name="threshold" value="DEBUG" />
<param name="File" value="./logs/metric.log" />
<param name="MaxFileSize" value="128MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
<param name="ConversionPattern" value="EXAMPLE_METRIC" />
</layout>
</appender>
<appender name="EXAMPLE_ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="threshold" value="ERROR" /> <!-- only WARN and ERROR are allowed in this log -->
<param name="File" value="./logs/error.log" />
<param name="MaxFileSize" value="128MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
<param name="ConversionPattern" value="EXAMPLE_ERROR" />
</layout>
</appender>
<appender name="EXAMPLE_DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="threshold" value="DEBUG" />
<param name="File" value="./logs/debug.log" />
<param name="MaxFileSize" value="128MB" />
<param name="MaxBackupIndex" value="20" />
<layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
<param name="ConversionPattern" value="EXAMPLE_DEBUG" />
</layout>
</appender>
<logger name="com.rsg.EXAMPLE.audit" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_AUDIT" />
</logger>
<logger name="com.rsg.EXAMPLE.metrics" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_METRIC" />
</logger>
<logger name="com.rsg.EXAMPLE.error" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_ERROR" />
</logger>
<logger name="com.rsg.EXAMPLE.debug" additivity="false">
<level value="info" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="EXAMPLE_DEBUG" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="EXAMPLE_DEBUG" />
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.openEXAMPLE.agcp.commonFunction.input"
additivity="false">
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</logger>
<logger name="org.openEXAMPLE.agcp.commonFunction.output"
additivity="false">
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</logger>
<logger name="org.openEXAMPLE.agcp.commonFunction.error"
additivity="false">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="EXAMPLE_ERROR" />
</logger>
<!-- The EXAMPLE logging standard has four specific classes of logging that
are unrelated to subsystem logger names. If you want them activated, uncomment
this block. -->
<logger name="com.rsg.EXAMPLE.audit" additivity="false">
<level value="info" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="EXAMPLE_AUDIT" />
</logger>
<logger name="com.rsg.EXAMPLE.metrics" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_METRIC" />
</logger>
<logger name="com.rsg.EXAMPLE.error" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_ERROR" />
</logger>
<logger name="com.rsg.EXAMPLE.debug" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_DEBUG" />
</logger>
<logger name="com.rsg.ova.apiClient.http.HttpClient" additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_ERROR" />
<appender-ref ref="CONSOLE" />
</logger>
<logger name="com.rsg.ova.roman.client.impl.romanSimplerBatchPublisher"
additivity="false">
<level value="info" />
<appender-ref ref="EXAMPLE_ERROR" />
<appender-ref ref="CONSOLE" />
</logger>
答案 0 :(得分:0)
我已检查过你的logback.xml文件配置。似乎缺少根标签(&#34;配置&#34;)。你可以添加和检查吗?
答案 1 :(得分:0)
您的xml文件中似乎错过了根标记。请找到以下代码将日志写入文件。
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="128MB" />
<param name="maxBackupIndex" value="20" />
<param name="file" value="./logs/audit.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>
答案 2 :(得分:0)
答案 3 :(得分:0)
已经确定我必须包含sl4j-log4j桥接jar来解决上述问题,我包括log4j和sl4j个别jar,由于这个我可以看到没有编译错误,但桥没有实现。
删除单个罐子并添加桥罐后问题已经解决。