Skip to content

Commit

Permalink
fix: specify async init bean log file path
Browse files Browse the repository at this point in the history
  • Loading branch information
linyimin0812 committed Sep 11, 2023
1 parent 05bc4fb commit ecd8835
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ spring-startup-analyzer.boost.spring.async.init-bean-thread-pool-max-size=8

3. Check if the bean is initialized asynchronously

View the log in the $HOME/spring-startup-analyzer/logs/startup.log file. For asynchronously initialized methods, a log entry will be written in the following format:
View the log in the `$HOME/spring-startup-analyzer/logs/async-init-bean.log` file. For asynchronously initialized methods, a log entry will be written in the following format:

```
async-init-bean, beanName: ${beanName}, async init method: ${initMethodName}
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ spring-startup-analyzer.boost.spring.async.init-bean-thread-pool-max-size=8

3. 检查Bean是否异步初始化

查看日志`$HOME/spring-startup-analyzer/logs/startup.log`文件,对于异步执行初始化的方法,会按照以下格式写一条日志:
查看日志`$HOME/spring-startup-analyzer/logs/async-init-bean.log`文件,对于异步执行初始化的方法,会按照以下格式写一条日志:

```
async-init-bean, beanName: ${beanName}, async init method: ${initMethodName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
**/
public class AsyncInitBeanFinder {

private static final Logger logger = LogFactory.getStartupLogger();
private static final Logger logger = LogFactory.getAsyncBeanLogger();

public static String getAsyncInitMethodName(String beanName, BeanDefinition beanDefinition) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
**/
public class AsyncTaskExecutor {

private static final Logger logger = LogFactory.getStartupLogger();
private static final Logger logger = LogFactory.getAsyncBeanLogger();

private static ThreadPoolExecutor threadPool;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
**/
public class AsyncBeanPriorityLoadPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {

private final Logger logger = LogFactory.getStartupLogger();
private final Logger logger = LogFactory.getAsyncBeanLogger();

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
**/
public class AsyncProxyBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, PriorityOrdered {

private final Logger logger = LogFactory.getStartupLogger();
private final Logger logger = LogFactory.getAsyncBeanLogger();

private ConfigurableListableBeanFactory beanFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public class LogFactory {
private final static Map<LoggerName, Logger> LOGGER_MAP = new HashMap<>();

static {
createLogger(LoggerName.startup);
createLogger(LoggerName.transform);
createLogger(LoggerName.STARTUP);
createLogger(LoggerName.TRANSFORM);

String asyncInitBeanLogPath = System.getProperty("user.home") + File.separator + "spring-startup-analyzer" + File.separator;
createLogger(LoggerName.ASYNC_INIT_BEAN, asyncInitBeanLogPath);
}

public static Logger getStartupLogger() {
Expand All @@ -24,6 +27,10 @@ public static Logger getTransFormLogger() {
return LOGGER_MAP.get(LoggerName.transform);
}

public static Logger getAsyncBeanLogger() {
return LOGGER_MAP.get(LoggerName.ASYNC_INIT_BEAN);
}

public static void close() {
for (Logger logger : LOGGER_MAP.values()) {
logger.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class Logger {
private final PrintWriter LOGGER_WRITER;

public Logger(LoggerName loggerName, String path) {
String file = path + File.separator + loggerName.name() + ".log";

String file = path + File.separator + loggerName.getValue() + ".log";
try {
Path filePath = Paths.get(file);
if (!Files.exists(filePath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ public enum LoggerName {
/**
* information about app startup
*/
startup,
STARTUP("startup"),

/**
* enhanced method information
*/
transform
TRANSFORM("transform"),

/**
* async init method bean information
*/
ASYNC_INIT_BEAN("async-init-bean");

private final String value;

private LoggerName(String value) {
this.value = value;
}

public String getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ void getTransFormLogger() {
assertNotNull(logger);
}

@Test
void getAsyncInitBeanLogger() {
Logger logger = LogFactory.getAsyncBeanLogger();
assertNotNull(logger);
}

@Test
void close() {
LogFactory.close();
Expand Down

0 comments on commit ecd8835

Please sign in to comment.