https://blog.csdn.net/qq_31352839/article/details/106887511
https://blog.csdn.net/lwwl12/article/details/83109815
fileName=”logs/info.log”
1.startup.bat启动 会在 tomcat/bin/logs/目录下
2.window 服务启动 会在 tomcat/logs/目录下
3.linux启动会在 tomcat/bin/logs/目录下
private String getLog4j2Path(){<br /> String path = MVCConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath();<br /> String logpath = "";<br /> try {<br /> logger.info("path_MVCConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath():" + path);<br /> //tomcat运行(已服务运行/tomcat/logs,已startup.bat启动/tomcat/bin/logs)<br /> if (path.contains("tomcat")) {<br /> int tomcatEndIndex = path.indexOf("/",path.toLowerCase().lastIndexOf("tomcat"));<br /> //logger.info("tomcat-path:" + path.substring(1, tomcatEndIndex) + "/bin/logs/");<br /> logpath = path.substring(1, tomcatEndIndex) + "/bin/logs/";<br /> }<br /> //本地调试<br /> else if (path.contains("target")) {<br /> int targetIndex = path.indexOf("/target/");<br /> logpath = path.substring(1, targetIndex) + "/logs/";<br /> }<br /> //jar包运行 file:/F:/self/Source/baseframe/baseframe.jar!/BOOT-INF/classes!/<br /> else if (path.contains("!/BOOT-INF")) {<br /> int index = path.indexOf("!/BOOT-INF");<br /> logpath = new File(path.substring(6, index)).getParentFile().getPath() + "/logs/";<br /> }<br /> logger.info("path_logpath:" + logpath);<br /> return logpath;<br /> }<br /> catch(Exception e){<br /> e.printStackTrace();<br /> logger.error("path:" + path);<br /> logger.error("获取Log4j2Path路径出错:" + e.getMessage());<br /> }<br /> return "";<br /> }