• 作者:老汪软件
  • 发表时间:2024-01-12 15:00
  • 浏览量:

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

文章目录

日志级别:

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

log4j官网:

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

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

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

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

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

显示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

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

或者xml的配置方式:


            
                
                
                
                
                
                
            
            
            
                
                
            
        

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

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

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

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

################################################################################
#  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

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