[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]log4j2动态创建Logger

上一篇:[备忘]log4j2.xml里配置日志路径,${webapp.root}不好使的问题
下一篇:[备忘]tomcat启动时打印:At least one JAR was scanned for TLDs yet contained no TLDs

添加日期:2024/2/19 16:34:07 快速返回   返回列表 阅读331次


import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender;
import org.apache.logging.log4j.core.config.*;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
import org.apache.logging.log4j.core.appender.rolling.OnStartupTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;

private static Logger getRollingFileAppender(String logName) {
        //日志路径
        String logPath = System.getProperty("webapp.root") + "/../data/log";

        try {
            //获取当前配置
            LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
            Configuration configuration = loggerContext.getConfiguration();

            // 创建RollingFileAppender
            AbstractAppender.Builder appender2 = RollingRandomAccessFileAppender.newBuilder()
                    .withFileName(logPath + "/" + logName + ".log")
                    .withFilePattern(logPath + "/" + logName + "-%d{yyyy-MM-dd}.log")
                    .withAppend(true)
                    .withPolicy(createTriggeringPolicy())
                    .withStrategy(DefaultRolloverStrategy.newBuilder().withFileIndex("max").withConfig(configuration).build())
                    .withImmediateFlush(false)
                    .withLayout(PatternLayout.newBuilder().withPattern("[%d{yyyy-MM-dd HH:mm:ss}][%p][%c][%x][%m]%n").build())
                    .withName(logName);

            //启动appender
            RollingRandomAccessFileAppender appender = ((RollingRandomAccessFileAppender.Builder) appender2).build();
            appender.start();
            configuration.addAppender(appender);

            //创建Logger
            AppenderRef ref = AppenderRef.createAppenderRef(logName + "Appender", null, null);
            LoggerConfig loggerConfig = LoggerConfig.createLogger(false, org.apache.logging.log4j.Level.INFO, logName, "false", new AppenderRef[]{ref}, null, configuration, null);
            loggerConfig.addAppender(appender, null, null);
            configuration.addLogger(logName, loggerConfig);

            //更新配置
            loggerContext.updateLoggers(configuration);

            //返回Logger
            return LoggerFactory.getLogger(logName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日志策略.
     *
     * @return
     */
    private static CompositeTriggeringPolicy createTriggeringPolicy() {
        TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = TimeBasedTriggeringPolicy.newBuilder()
                .withInterval(10)
                .withModulate(true)
                .build();
        OnStartupTriggeringPolicy onStartupTriggeringPolicy = OnStartupTriggeringPolicy.createPolicy(1);
        SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy("100MB");

        return CompositeTriggeringPolicy.createPolicy(sizeBasedTriggeringPolicy, timeBasedTriggeringPolicy, onStartupTriggeringPolicy);
    }

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved