博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Log4j日志文件保存位置解决方案
阅读量:7043 次
发布时间:2019-06-28

本文共 1734 字,大约阅读时间需要 5 分钟。

hot3.png

以DailyRollingFileAppender 为例:假设每天一个日志文件

有以下设置:

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=app.loglog4j.appender.A1.DatePattern='.'yyyy-MM-ddlog4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=%d %5p - %c -%-4r [%t]    - %m%n

此时生成日志文件将位于tomcat的bin目录下,如要将日志文件保存在 :根目录/web-info/logs/下,个人有以下4种解决方案:

1 绝对路径
log4j.appender.A1.File=D:\apache-tomcat-6.0.18/webapps/项目/WEB-INF/logs/app.log
但这种写法灵活性很差
以下3中使用相同的设置原理: jvm的环境变量
2:spring的Log4jConfigListener
通过以下配置:

webAppRootKey
webApp.root
log4jConfigLocation
classpath:log4j.properties
< listener>
org.springframework.web.util.Log4jConfigListener

...
log4j.appender.logfile.File=${webApp.root}/WEB-INF/logs/app.log
...
来解决
2:使用已有jvm变量:
例如:
    log4j.appender.logfile.File=${user.home}/logs/app.log
  日志将位于:例如windows:C:\Documents and Settings\joe\logs\app.log
3 自己设置目录,也就是在项目启动时通过System.setProperty设置
通过实现ServletContextListener来解决:例如

public class log4jlistener implements ServletContextListener {public static final String log4jdirkey = "log4jdir";public void contextDestroyed(ServletContextEvent servletcontextevent) {  System.getProperties().remove(log4jdirkey);}public void contextInitialized(ServletContextEvent servletcontextevent) {  String log4jdir = servletcontextevent.getServletContext().getRealPath("/");  //System.out.println("log4jdir:"+log4jdir);  System.setProperty(log4jdirkey, log4jdir);}}

web.xml配置:
com.log4j.log4jlistener

log4j.prtperties 配置:
log4j.appender.A1.File=${log4jdir}/WEB-INF/logs/app1.log来解决。

转载于:https://my.oschina.net/Barudisshu/blog/284123

你可能感兴趣的文章
CentOS最常用命令及快捷键整理
查看>>
人工智能教程017:创建卷积神经网络进阶(8)
查看>>
test
查看>>
python 创建PDF文件
查看>>
rfld识别读写器的设计与实现
查看>>
LINUX GRUB的安装
查看>>
技术人员创业后最好就不要做编程了
查看>>
我的友情链接
查看>>
磁盘IO性能
查看>>
linux网卡问题
查看>>
HttpClient4.0 Http连接池 长连接
查看>>
健身训练
查看>>
我的友情链接
查看>>
denied: requested access to the resource is denied
查看>>
悟道:成功之路在何方
查看>>
Play 2 示例(种子)项目
查看>>
Lamp的架设
查看>>
src 和 href 的区别
查看>>
Java堆中对象创建、布局、访问全过程
查看>>
JavaScript跨域原因分析与解决办法
查看>>