您好我需要将条目写入来自MySQL中的表的日志文件中。我做了我的研究,但对我找到的解决方案并不满意。我读到了使用日志级别作为调试来检查sql查询是否正常运行。
但我希望将数据记录到特定的日志文件中,该日志文件不会记录任何其他内容,只是来自sql表的数据。有人可以帮忙吗?
这是我作为POC编写的代码。这是我的班级部分。
private static final Logger logger = LogManager.getLogger(LogDemo.class.getName());
private static final Marker QUERY_MARKER = MarkerManager.getMarker("SQL");
public static void main(final String... args) {
// Set up a simple configuration that logs on the console.
logger.info("Ankush Bhan created this");
logger.error("THIS IS TRACE");
}
这是我的配置log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/Users/712054/Desktop/Source</Property>
</Properties>
<Appenders>
<RollingFile name="info" fileName="${log-path}/info.log"
filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" >
<AppenderRef ref="info"/>
</Root>
</Loggers>
</Configuration>
当我运行它时,两个语句都被打印出来。但我只想要log.info部分??
答案 0 :(得分:0)
所以我终于通过使用过滤器解决了这个问题。这是将类消息打印到日志文件中的java类。在另一个日志文件中,我添加了跟踪消息。这是我编辑的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/Users/712054/Desktop/Source</Property>
</Properties>
<Appenders>
<RollingFile name="info" fileName="${log-path}/info.log"
filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy size="1"/>
</Policies>
</RollingFile>
<RollingFile name="trace" fileName="${log-path}/trace.log"
filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy size="1"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all" >
<AppenderRef ref="info"/>
<AppenderRef ref="trace"/>
</Root>
</Loggers>
</Configuration>
相应的java类是
public class LogDemo {
private static final Logger logger = LogManager.getLogger(LogDemo.class.getName());
public static void main(final String... args) {
logger.entry(); //this goes in the trace log file
// Set up a simple configuration that logs on the console.
logger.info("This goes only in the info log file");
logger.trace("This goes only in the trace log file");
logger.exit(); //this goes in the trace log file
}