如何纯粹以编程方式配置log4j 2.x? [英] How to configure log4j 2.x purely programmatically?

查看:84
本文介绍了如何纯粹以编程方式配置log4j 2.x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 log4j 2.3 rel =noreferrer> console appender 纯编程(没有任何格式的配置文件)?

How do I configure log4j 2.3 with console appender pure programmatically (no configuration files of any format)?

基本上我正在寻找这个的一个2.x版本 1.x代码

Basically I'm looking for 2.x version of this 1.x code.

在我的课程中,我会使用

In my classes I would then use

private static final Logger logger = LogManager.getLogger();
// 
    // some method
       logger.debug(someString);

没有任何配置我(按预期)面对

Without any configuration I'm (as expected) facing


错误StatusLogger找不到log4j2配置文件。使用默认配置:仅将错误记录到控制台。

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

虽然配置文件的使用似乎是正确记录了,我找不到一个只有裸机代码的好例子。

While usage of configuration files seems to be properly documented, I couldn't find a good example of a bare-bone code-only case.

我最接近的是这篇文章仍然使用虚拟文件。

The closest I got is this article that still uses a dummy file.

这是我最好的(虽然完全不成功)拍摄:

Here's my best (though completely unsuccessful) shot:

private static void configureLog4J() {
    PatternLayout layout = PatternLayout.createDefaultLayout();
    ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(layout);
    LoggerConfig loggerConfig = new LoggerConfig();
    loggerConfig.addAppender(appender, DEBUG, null);
}

我错过了什么吗?

如果它仍然是RTFM案例,请指出正确的方向。

If it's still a RTFM case, please point me into the right direction.

推荐答案

package com;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.nio.charset.Charset;

public class MyLoggerTest  {

    public static void main(String[] args){
        LoggerContext context= (LoggerContext) LogManager.getContext();
        Configuration config= context.getConfiguration();

        PatternLayout layout= PatternLayout.createLayout("%m%n", null, null, Charset.defaultCharset(),false,false,null,null);
        Appender appender=ConsoleAppender.createAppender(layout, null, null, "CONSOLE_APPENDER", null, null);
        appender.start();
        AppenderRef ref= AppenderRef.createAppenderRef("CONSOLE_APPENDER",null,null);
        AppenderRef[] refs = new AppenderRef[] {ref};
        LoggerConfig loggerConfig= LoggerConfig.createLogger("false", Level.INFO,"CONSOLE_LOGGER","com",refs,null,null,null);
        loggerConfig.addAppender(appender,null,null);

        config.addAppender(appender);
        config.addLogger("com", loggerConfig);
        context.updateLoggers(config);

        Logger logger=LogManager.getContext().getLogger("com");
        logger.info("HELLO_WORLD");


    }
}

不确定是否这就是你要找的东西。这将创建默认配置并添加控制台记录器。但是,LogManager.getLogger()不起作用并使用LogManager.getContext()。getLogger()不允许记录器层次结构。老实说我不建议使用程序化方法,Log4j2对它过敏。

Not sure if this is what you are looking for. This creates a default configuration and adds a console logger. However, LogManager.getLogger() does not work and using the LogManager.getContext().getLogger() does not allow for logger hierarchy. Honestly i don't recommend the programmatic approach, Log4j2 is allergic to it.

这篇关于如何纯粹以编程方式配置log4j 2.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆