当前位置: 首页 > news >正文

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

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

文章目录

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

日志级别:
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

由于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><!-- onMatch:Action to take when the filter matches. The default value is NEUTRAL --><!-- onMismatch:    Action to take when the filter does not match. The default value is DENY --><!-- 级别在 ERROR 之上的都拒绝输出 --><!-- 在组合过滤器中,接受使用 NEUTRAL(中立),被第一个过滤器接受的日志信息,会继续用后面的过滤器进行过滤,只有符合所有过滤器条件的日志信息,才会被最终写入日志文件 --><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>

这种方式看着总感觉有点别扭,那有没有其他的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 = DENYappender.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任务时有类似的需求,可以参考上面的配置进行修改,实际上主要添加的只有filter.threshold配置参数即可。

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

log4j2使用filter过滤日志

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

log4j2中LevelRangeFilter的注意点

Log4j2的Filters配置

http://www.lryc.cn/news/193664.html

相关文章:

  • Ubuntu下怎么配置vsftpd
  • 链表(7.27)
  • 在 Elasticsearch 中实现自动完成功能 1:Prefix queries
  • 『PyQt5-Qt Designer篇』| 13 Qt Designer中如何给工具添加菜单和工具栏?
  • Android Studio新建项目教程
  • 前端页面布局之【响应式布局】
  • 定制排序小案例
  • 如何设计一个ToC的弹窗
  • Idea执行Pom.xml导入jar包提示sun.misc.BASE64Encoder jar找不到---SpringCloud工作笔记197
  • 大数据面试题:Spark和Flink的区别
  • 2023年9月青少年软件编程(C 语言) 等级考试试卷(二级)
  • 【Wifi】Wifi架构介绍
  • 攻防世界数据逆向 2023
  • 分布式链路追踪如何跨线程
  • 怎样在线修剪音频文件了?【免费,无须注册】
  • iMeta框架使用方法
  • 视频编辑软件 Premiere Pro 2024 macv24.0中文版 (pr2024)
  • C/C++:双向队列的实现
  • MySQL逻辑架构
  • python爬虫练手项目之获取某地企业名录
  • Python —— 接口自动化(1)
  • 【MySQL】关于MySQL升级到8.0版本的实践方案
  • 【Python-Django】基于TF-IDF算法的医疗推荐系统复现过程
  • 车辆车型识别系统python+TensorFlow+Django网页界面+算法模型
  • 小程序如何设置各种时间参数
  • CSS变量 var()的用法
  • 设计模式——21. 中介者模式
  • fastjson 1.2.47 远程命令执行漏洞
  • 【k8s 开发排错】k8s组件开发排错之pprof
  • 记录一次典型oom的处理过程