Java 使用log4j2将数据记录到日志文件中?

Java 使用log4j2将数据记录到日志文件中?,java,logging,log4j2,Java,Logging,Log4j2,您好,我需要在MySQL中将来自表的条目写入日志文件。我做了研究,但对我找到的解决方案并不满意。我读过关于使用日志级别作为调试来检查sql查询是否正确运行的文章 但我希望将数据记录到特定的日志文件中,该文件不记录任何其他内容,只记录sql表中的数据。有人能帮忙吗 以下是我作为POC编写的代码。这是我上课的部分 private static final Logger logger = LogManager.getLogger(LogDemo.class.getName()); private st

您好,我需要在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>

C:/Users/712054/Desktop/Source
%d{dd/MMM/yyyy HH:mm:ss,SSS}-%c{1}:%m%n

当我运行这个程序时,两个语句都被打印出来了。但是我只想要log.info部分,所以我最终只使用过滤器解决了这个问题。下面是将一些消息打印到日志文件中的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>

你试过什么了吗?我试过使用simple log.info登录到日志文件中,在配置文件中我设置了过滤器,只记录信息部分。但是我们还有其他方法吗?似乎使用jdbc读取mysql表并使用log4j2记录它的简单解决方案应该可以工作。Log4j2可以配置为将日志写入此类/记录器的单独文件中。有什么并发症吗?我应该用过滤器吗?
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


    }