思路:在项目启动时获取主机名放到System中,xml文件中可以直接获取${hostName} <RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info/${hostName}project-info.log" filePattern="${FILE_PATH}/info/${hostName}project-info-%d{yyyy-MM-dd}.log"> import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; @Component public class LoggerInitializer implements ApplicationRunner { public void reloadLog4j() { String HOST_NAME = null; try { HOST_NAME = InetAddress.getLocalHost().getHostName(); System.setProperty("hostName", HOST_NAME); } catch (UnknownHostException e) { e.printStackTrace(); } } @Override public void run(ApplicationArguments args) throws Exception { reloadLog4j(); } }
log4j文件名包含主机名
最新推荐文章于 2023-11-04 01:10:01 发布