• Flink Log4j 2.x使用Filter过滤日志类型


    Flink Log4j 2.x使用Filter过滤日志类型(区别INFO、ERROR)


    日志级别:
    ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

    log4j官网:

    https://logging.apache.org/log4j/2.x/index.html

    ThresholdFilter

    在官网中,有一个Filters的组件。Filters组件允许对日志事件进行评估,以确定是否或如何发布它们。Filter将在其过滤器方法之一上被调用,并将返回一个Result,这是一个Enum,具有3个值之一- ACCEPT, DENY或NEUTRAL。

    如果LogEvent中的级别与配置的级别相同或更具体,则此过滤器返回onMatch结果,否则返回onMismatch值。例如,如果ThresholdFilter配置了ERROR级别,并且LogEvent包含DEBUG级别,那么onMismatch值将被返回,因为ERROR事件比DEBUG事件更高。

    ThresholdFilter过滤器的原理是,如果LogEvent的日志级别被配置的高,则会执行onMatch,否则执行onMismatch。比如如果ThresholdFilter配置了INFO级别,而LogEvent是WARN、ERROR级别,那么onMatch值将会执行。而当LogEvent是DEBUG级别时,则onMismatch值将会执行。

    这里有一个Threshold的过滤器,参数包含了:

    • Level:要匹配的有效日志级别。
    • onMatch: 当过滤器匹配时要采取的操作。可以是ACCEPT, DENY或NEUTRAL。缺省值为NEUTRAL。
    • onMismatch:当过滤器不匹配时采取的操作。可以是ACCEPT, DENY或NEUTRAL。缺省值为DENY。

    借助于这个Threshold过滤器,可以初步实现过滤日志的功能:

    显示info 级别的日志

    appender.rolling.filter.threshold.type = ThresholdFilter
    appender.rolling.filter.threshold.level = ERROR
    appender.rolling.filter.threshold.onMatch = DENY
    appender.rolling.filter.threshold.onMismatch = ACCEPT
    
    • 1
    • 2
    • 3
    • 4

    由于log4j.properties的rootLogger.level = INFO,因此最小的日志级别就已经是INFO了,所以上面的配置可以保证当前appender的输出日志只包含INFO信息。相当于是对ERROR及以上级别的日志执行onMatch=>DENY,而对小于ERROR级别,也就是INFO级别,执行onMismatch => ACCEPT。

    或者xml的配置方式:

    <RollingFile name="RollingFileInfo" fileName="${logFilePath}/${logFileName}-info.log"
                         filePattern="${logFilePath}/$${date:yyyy-MM}/${logFileName}-%d{yyyy-MM-dd}_%i.log.gz">
                <Filters>
                    
                    
                    
                    
                    <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
                    <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
                Filters>
                <PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
                <Policies>
                    <TimeBasedTriggeringPolicy/>
                    <SizeBasedTriggeringPolicy size="30MB"/>
                Policies>
            RollingFile>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    这种方式看着总感觉有点别扭,那有没有其他的Filter组件能更好地支持呢?

    LevelMatchFilter

    查询资料发现,有一个LevelMatchFilter的过滤器组件,其执行原理是:

    如果日志级别等于${指定的日志级别},则onMatch,否则onMismatch

    刚好符合我们的需求,于是此时,log4j.properties的配置文件就可以变成:

    ################################################################################
    #  Licensed to the Apache Software Foundation (ASF) under one
    #  or more contributor license agreements.  See the NOTICE file
    #  distributed with this work for additional information
    #  regarding copyright ownership.  The ASF licenses this file
    #  to you under the Apache License, Version 2.0 (the
    #  "License"); you may not use this file except in compliance
    #  with the License.  You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    #  Unless required by applicable law or agreed to in writing, software
    #  distributed under the License is distributed on an "AS IS" BASIS,
    #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #  See the License for the specific language governing permissions and
    # limitations under the License.
    ################################################################################
    
    # Allows this configuration to be modified at runtime. The file will be checked every 30 seconds.
    monitorInterval=30
    
    # This affects logging for both user code and Flink
    
    # This affects logging for both user code and Flink
    rootLogger.level = INFO
    rootLogger.appenderRef.rolling.ref = RollingFileAppender
    rootLogger.appenderRef.errorLogFile.ref = errorLogFile
    
    # Uncomment this if you want to _only_ change Flink's logging
    #logger.flink.name = org.apache.flink
    #logger.flink.level = INFO
    
    # The following lines keep the log level of common libraries/connectors on
    # log level INFO. The root logger does not override this. You have to manually
    # change the log levels here.
    logger.akka.name = akka
    logger.akka.level = INFO
    logger.kafka.name= org.apache.kafka
    logger.kafka.level = INFO
    logger.hadoop.name = org.apache.hadoop
    logger.hadoop.level = INFO
    logger.zookeeper.name = org.apache.zookeeper
    logger.zookeeper.level = INFO
    
    # Log all infos in the given rolling file
    appender.rolling.name = RollingFileAppender
    appender.rolling.type = RollingFile
    appender.rolling.append = true
    appender.rolling.fileName = ${sys:log.file}
    appender.rolling.filePattern = ${sys:log.file}.%i
    appender.rolling.layout.type = PatternLayout
    appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n
    appender.rolling.policies.type = Policies
    appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
    appender.rolling.policies.size.size=100MB
    appender.rolling.policies.startup.type = OnStartupTriggeringPolicy
    appender.rolling.strategy.type = DefaultRolloverStrategy
    appender.rolling.strategy.max = ${env:MAX_LOG_FILE_NUMBER:-10}
    appender.rolling.filter.threshold.type = LevelMatchFilter
    appender.rolling.filter.threshold.level = INFO
    appender.rolling.filter.threshold.onMatch = ACCEPT
    appender.rolling.filter.threshold.onMisMatch = DENY
    
    appender.errorFile.name = errorLogFile
    appender.errorFile.type = RollingFile
    appender.errorFile.append = true
    appender.errorFile.fileName = ${sys:log.file}.err
    appender.errorFile.filePattern = ${sys:log.file}.err.%i
    appender.errorFile.layout.type = PatternLayout
    appender.errorFile.layout.pattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n
    appender.errorFile.policies.type = Policies
    appender.errorFile.policies.size.type = SizeBasedTriggeringPolicy
    appender.errorFile.policies.size.size = 100MB
    appender.errorFile.policies.startup.type = OnStartupTriggeringPolicy
    appender.errorFile.strategy.type = DefaultRolloverStrategy
    appender.errorFile.strategy.max = ${env:MAX_LOG_FILE_NUMBER:-10}
    appender.errorFile.filter.threshold.type = LevelMatchFilter
    appender.errorFile.filter.threshold.level = ERROR
    appender.errorFile.filter.threshold.onMatch = ACCEPT
    appender.errorFile.filter.threshold.onMisMatch = DENY
    
    # Suppress the irrelevant (wrong) warnings from the Netty channel handler
    logger.netty.name = org.apache.flink.shaded.akka.org.jboss.netty.channel.DefaultChannelPipeline
    logger.netty.level = OFF
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    大家如果在部署flink任务时有类似的需求,可以参考上面的配置进行修改,实际上主要添加的只有filter.threshold配置参数即可。

    分享一些讲解比较好的相关文章:

    log4j2使用filter过滤日志

    Log4j2 将不同线程不同级别日志输出到不同的文件中

    log4j2中LevelRangeFilter的注意点

    Log4j2的Filters配置

  • 相关阅读:
    RabbitMQ延迟消息:死信队列 | 延迟插件 | 二合一用法+踩坑手记+最佳使用心得
    [Vue3 博物馆管理系统] 使用Vue3、Element-plus的Layout 布局构建组图文章
    Python语义分割与街景识别(1):理论学习
    java报告:小不点超市售货系统类图设计
    Python面试高频问题:修改list中某个元素时的坑
    11、matlab将日期和字符串写入EXCEL,并将EXCEL数据读取另存为数字、元胞和结构体形式
    FreeRTOS入门笔记——FreeRTOS介绍
    python循环中的continue和break
    Linux系统使用AndroidStudio创建桌面快捷键
    stm32cubemx:systick系统定时器中断与TIM定时器中断的配置及使用方法
  • 原文地址:https://blog.csdn.net/Urbanears/article/details/133796464