【问题标题】:Logging data into log files using log4j2?使用 log4j2 将数据记录到日志文件中?
【发布时间】:2015-02-07 00:52:29
【问题描述】:

您好,我需要将条目写入来自 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 部分??

【问题讨论】:

  • 你试过了吗?
  • 我尝试使用简单的 log.info 登录日志文件,并在配置文件中放置过滤器以仅记录信息部分。但是我们还有其他办法吗?
  • 似乎使用 jdbc 从 mysql 表读取并使用 log4j2 记录它的简单解决方案应该可以工作。 Log4j2 可以配置为将日志写入此类/记录器的单独文件。有什么并发症吗?
  • 我应该使用过滤器来完成这项工作吗?

标签: java logging log4j2


【解决方案1】:

所以我最终只使用过滤器解决了这个问题。这是将一些消息打印到日志文件中的 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


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2021-11-19
    • 2016-01-26
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多