log4j将日志文件输出到相对路径

建议直接使用jvm中定义的变量或者操作系统的环境变量。

log4j.appender.logfile.File=${user.dir}/logs/app.log,使用tomcat容器时${user.dir}对应tomcat的bin目录;
log4j.appender.logfile.File=${user.home}/logs/app.log,${user.home}对应操作系统当前用户目录;
log4j.appender.logfile.File=${webApp.root}/logs/app.log,${webApp.root}对应当前应用根目录;

log4j.appender.E.File = ${catalina.base}/logs/error.log

System.getProperty(..)方法可以得到的变量也是可以的,比如说user.dir等与操作系统相关的变量会默认加载进来。

另外,利用启动参数-D也可以手动加入系统参数.

比如可以这样:

在程序启动时获取classpath路径,用System.setProperty(log_path,classpath)把值设置到一个变量中,在log4j.property的日志文件中路径前面加上${log_path}/

// 初始化日志路径
String rootPath = PathUtil.getPath();
System.setProperty("log_path",rootPath);

陷阱tips:

log4j将日志文件输出到相对路径

附:

    public static void main(String args[]){
Properties properties = System.getProperties();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
java.runtime.name:Java(TM) SE Runtime Environment
sun.boot.library.path:C:\Program Files\Java\jdk1.8.0_11\jre\bin
java.vm.version:25.11-b03
java.vm.vendor:Oracle Corporation
java.vendor.url:http://java.oracle.com/
path.separator:;
java.vm.name:Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg:sun.io
user.country:CN
user.script:
sun.java.launcher:SUN_STANDARD
sun.os.patch.level:
java.vm.specification.name:Java Virtual Machine Specification
user.dir:F:\workspace2
java.runtime.version:1.8.0_11-b12
java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs:C:\Program Files\Java\jdk1.8.0_11\jre\lib\endorsed
os.arch:amd64
java.io.tmpdir:C:\Users\AppData\Local\Temp\
line.separator: java.vm.specification.vendor:Oracle Corporation
user.variant:
os.name:Windows 8.1
sun.jnu.encoding:GBK
java.library.path:C:\Program Files\Java\jdk1.8.0_11\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;F:\oracle;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;E:\tools\apache-maven-3.1.0\bin;C:\Program Files\Java\jdk1.8.0_11\bin;C:\Users\qing\AppData\Roaming\npm;D:\Program Files\TortoiseSVN\bin;D:\Program Files\SlikSvn\bin;D:\Program Files\nodejs\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\Program Files\MATLAB\R2016b\runtime\win64;D:\Program Files\MATLAB\R2016b\bin;D:\Program Files\MATLAB\R2016b\polyspace\bin;C:\Users\qing\AppData\Roaming\npm;C:\Users\qing\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\HMA! Pro VPN\bin;.
java.specification.name:Java Platform API Specification
java.class.version:52.0
sun.management.compiler:HotSpot 64-Bit Tiered Compilers
os.version:6.3
user.home:C:\Users\qi
user.timezone:Asia/Shanghai
java.awt.printerjob:sun.awt.windows.WPrinterJob
file.encoding:UTF-8
java.specification.version:1.8
java.class.path:C:\Program Files\Java\jdk1.8.0_11\jre\lib\charsets.jar;……省略
user.name:qing
java.vm.specification.version:1.8
sun.java.command:com.sung.risk.client.Test
java.home:C:\Program Files\Java\jdk1.8.0_11\jre
sun.arch.data.model:64
user.language:zh
java.specification.vendor:Oracle Corporation
awt.toolkit:sun.awt.windows.WToolkit
java.vm.info:mixed mode
java.version:1.8.0_11
java.ext.dirs:C:\Program Files\Java\jdk1.8.0_11\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path:C:\Program Files\Java\jdk1.8.0_11\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_11\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_11\jre\classes
java.vendor:Oracle Corporation
file.separator:\
java.vendor.url.bug:http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding:UnicodeLittle
sun.cpu.endian:little
sun.desktop:windows
sun.cpu.isalist:amd64
上一篇:Java - 自动配置log4j的日志文件路径


下一篇:[转]spring_bean的属性