logback
新建logback-spring.xml
<springProperty scope="context" name="application" source="spring.application.name"/>
<springProperty scope="context" name="project" source="dynamic.base.package"/>
source 表示的是application.yml 里面的配置key
log4j2
没有找到直接配置,只能对spring 初始化监听器的自定义实现,然后利用mdc设置参数,供日志配置文件调用
package com.nfsq.terminal.config.listener;
import org.slf4j.MDC;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
/**
* 自定义spring boot 监听器为log4j2 获取自定义上下文
* 必须实现Ordered,启动顺序可以提到创建日志文件之前
*
*
*
* <p>
* ApplicationStartingEvent: spring boot启动开始时执行的事件
* <p>
* ApplicationEnvironmentPreparedEvent:spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建
* <p>
* ApplicationPreparedEvent: spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的
* <p>
* ApplicationFailedEvent: spring boot启动异常时执行事件
*
*
* @author ckli01
* @date 2018/5/10
*/
public class MyApplicationStartedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment configurableEnvironment = event.getEnvironment();
MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
PropertySource<?> propertySource = mutablePropertySources.get("applicationConfigurationProperties");
if (propertySource != null && propertySource.containsProperty("logging.runtime.root")) {
String rootDir = (String) propertySource.getProperty("root.dir");
String runTimeCountPath = ((String) propertySource.getProperty("logging.runtime.root")).replace("${root.dir}", rootDir);
System.out.println("*******************************");
System.out.println(runTimeCountPath);
System.out.println("*******************************");
MDC.put("runTimeCountPath", runTimeCountPath);
}
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 10;
}
}
然后在启动类中调用:
package com.nfsq.terminal;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.nfsq.terminal.config.listener.ApplicationStartedEventListener;
import com.nfsq.terminal.config.listener.MyApplicationStartedEventListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class EndeavourApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(EndeavourApplication.class);
app.addListeners(new MyApplicationStartedEventListener());
//app.addListeners(new ApplicationStartedEventListener());
app.run(args);
}
}
log4j2.yml 调用添加ctx
# AOP监控:统计运行时间
- name: runTimeCount
ignoreExceptions: false
fileName: ${ctx:runTimeCountPath}/endeavour_runTimeCount.log
filePattern: "${ctx:runTimeCountPath}/endeavour_runTimeCount.log.%d{yyyy-MM-dd}"
PatternLayout:
pattern: "%date %-5level [%f:%l] %msg%n%ex"
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
网友评论