Flink配log4j2日志输出

  • Post author:
  • Post category:其他




一. idea中配置



1. resources文件夹添加log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="5">
    <Properties>
        <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
		<!-- LOG_LEVEL 配置你需要的日志输出级别       -->
        <property name="LOG_LEVEL" value="WARN" />
    </Properties>

    <appenders>
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
        </console>
    </appenders>

    <loggers>
        <root level="${LOG_LEVEL}">
            <appender-ref ref="Console"/>
        </root>
    </loggers>

</configuration>



2. 添加依赖
		<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${slf4j2.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${slf4j2.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${slf4j2.version}</version>
            <scope>provided</scope>
        </dependency>


3. 效果

在这里插入图片描述



二. 代码中配置



1. 官网描述
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foobar {
	private static final Logger LOG = LoggerFactory.getLogger(Foobar.class);

	public static void main(String[] args) {
		LOG.info("Hello world!");
	}
}


2. 实际使用
// DwdMedicalTransportAnalysisAppV1 是你任务的class名
private static final Logger LOG = LoggerFactory.getLogger(DwdMedicalTransportAnalysisAppV1.class);

LOG.warn("一个 {} 发生. 查询pgsql. 信息:{}", "warn", resultSet.getString("name"));

//然后就可以到你的项目中查看对应的日志显示


3. 效果

在这里插入图片描述



版权声明:本文为m0_50670853原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。