您的当前位置:首页正文

log4j2动态修改日志级别及拓展性使用

2023-09-13 来源:爱站旅游
导读log4j2动态修改日志级别及拓展性使用
log4j2动态修改⽇志级别及拓展性使⽤

⼀、供参考的完整⽇志配置

${sys:vmparam} ${basePath}/app.log

filePattern=\"logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz\">

⼆、动态修改⽇志级别

Collection current = LoggerContext.getContext(false).getLoggers();Collection notcurrent = LoggerContext.getContext().getLoggers();Collection allConfig = current;allConfig.addAll(notcurrent);

for (org.apache.logging.log4j.core.Logger log:allConfig){ log.setLevel(Level.DEBUG);} 

三、⾃定义appender

  以上介绍,均依赖于log4j2提供的官⽅配置,当对⽇志的业务逻辑复杂时,光靠配置也许满⾜不了需要,此时我们会想⾃⼰能操控打印的⽇志,做⽇志的路由,或保存等操作,这个时候就需要有⾃定义的appender,可以配置的就靠配置完成,不能的就⾃⼰写代码⼲预,⽽log4j2刚好提供了这样的拓展性。

  如下代码即是⾃定义的Appender,通过实现AbstractAppender接⼝,配置@Plugin注解对应的信息并在eppend⽅法中写⾃⼰的业务逻辑,从⽽实现了对⽇志更⼤⾃由度的控制,如下展⽰的log4j配置⽂件中的配置节点名称要和注解中配置的name属性⼀致,并在Configuration节点配置好⾃定义Appender所在的包路径即可。

package com.jyk.log4j2.log4j2_test;

import java.io.Serializable;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;import org.apache.logging.log4j.core.Filter;import org.apache.logging.log4j.core.Layout;import org.apache.logging.log4j.core.LogEvent;

import org.apache.logging.log4j.core.appender.AbstractAppender;

import org.apache.logging.log4j.core.appender.AppenderLoggingException;import org.apache.logging.log4j.core.config.plugins.Plugin;

import org.apache.logging.log4j.core.config.plugins.PluginAttribute;import org.apache.logging.log4j.core.config.plugins.PluginElement;import org.apache.logging.log4j.core.config.plugins.PluginFactory;import org.apache.logging.log4j.core.layout.PatternLayout;

@Plugin(name = \"MyAppenderrue)public class MyAppender extends AbstractAppender {

/**

* @fields serialVersionUID */

private static final long serialVersionUID = -830237775522429777L; private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); private final Lock readLock = rwLock.readLock();

//需要实现的构造⽅法,直接使⽤⽗类就⾏

protected MyAppender(final String name, final Filter filter, final Layout layout, final boolean ignoreExceptions) {

super(name, filter, layout, ignoreExceptions); }

@Override

public void append(LogEvent event) { readLock.lock(); try {

final byte[] bytes = getLayout().toByteArray(event);//⽇志⼆进制⽂件,输出到指定位置就⾏

//拿到每次打印的⽇志,写⾃⼰的业务逻辑 System.out.println(\"enter my append...\"); } catch (Exception ex) { if (!ignoreExceptions()) {

throw new AppenderLoggingException(ex); }

} finally {

readLock.unlock(); } }

// 下⾯这个⽅法可以接收配置⽂件中的参数信息 @PluginFactory

public static MyAppender createAppender(@PluginAttribute(\"name\") String name, @PluginElement(\"Filter\") final Filter filter,

@PluginElement(\"Layout\") Layout layout, @PluginAttribute(\"ignoreExceptions\") boolean ignoreExceptions) { if (name == null) {

LOGGER.error(\"No name provided for MyCustomAppenderImpl\"); return null; }

if (layout == null) {

layout = PatternLayout.createDefaultLayout(); }

return new MyAppender(name, filter, layout, ignoreExceptions); }}

因篇幅问题不能全部显示,请点此查看更多更全内容