Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不用springboot怎么能让log4j2取到apollo的配置? #1007

Closed
xychq8 opened this issue Mar 19, 2018 · 5 comments
Closed

不用springboot怎么能让log4j2取到apollo的配置? #1007

xychq8 opened this issue Mar 19, 2018 · 5 comments
Labels
area/client apollo-client kind/question Categorizes issue when someone want to know answer of a question status/fixed issue fixed already

Comments

@xychq8
Copy link

xychq8 commented Mar 19, 2018

没用springboot,logs4j2.xml里怎么能取到配置?主要是用来配置日志路径

@lepdou
Copy link
Contributor

lepdou commented Mar 20, 2018

在不是Spring的环境下,可通过编程API的方式获取配置。所以,你得看一下log4j2有没有API,可以在代码里配置日志路径的接口,如果有就可以跟Apollo对接上了。

@nobodyiam
Copy link
Member

可以参考 #816

@yaodawei
Copy link

yaodawei commented May 4, 2018

继承ConfigurationFactory getConfiguration 重写这个方法就可以

@hongliang777
Copy link

hongliang777 commented Jul 23, 2018

使用log4j2也遇到了apollo管理日志路径的问题,可以利用log4j2的Property Substitution特性。
http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
自定义个StrLookup plugin,查找的时候从apollo中获取。
如果日志路径也需要不重启动态生效,在apollo的监听事件中加上
LoggerContext loggerContext = LoggerContext.getContext(false);
loggerContext.reconfigure();
简单例子https://github.com/hongliang777/log4j2Test.git

@Plugin(name = "lk", category = StrLookup.CATEGORY)
public class LookupFromApp implements StrLookup{
    @Override
    public String lookup(String key) {
        // use apollo
        // return ConfigService.getConfig("app").getProperty(key,"");
        return "d:/abc/logs1/";
    }

    @Override
    public String lookup(LogEvent event, String key) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.yhl.log4j2">
    <Properties>
        <Property name="filename">${lk:abc}.log</Property>
    </Properties>
    ..........

@nobodyiam nobodyiam added the info label Aug 26, 2018
@nobodyiam
Copy link
Member

1.2.0版本已经支持Spring Boot中Apollo的加载顺序放到日志系统加载之前,从而可以支持日志配置放在Apollo管理(如logging.level.root=info或logback-spring.xml中的参数),可以尝试一下。

@Anilople Anilople added area/client apollo-client kind/question Categorizes issue when someone want to know answer of a question status/fixed issue fixed already and removed info labels Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client apollo-client kind/question Categorizes issue when someone want to know answer of a question status/fixed issue fixed already
Projects
None yet
Development

No branches or pull requests

6 participants