博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
log4j2指定日志文件路径到工程路径
阅读量:6325 次
发布时间:2019-06-22

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

在log4j2中,我个人比较喜欢把web项目的日志文件保存到对应项目的路径下,

网上这样的写法:(错误处理方法,正确请看下面)
在日志路径中调用System property的参数,大概是这样的:
log4j2配置文件:

而这个webApp.root在项目中使用Sysetm.setProperty("webApp.root", "xxxx");来设置

我用静态块来设置webApp.root,但他是在log4j2加载后再加载的,结果扑街。。。
输出结果变成这样:日志文件保存到一个名为${webApp.root}的文件夹里面了。

==========================正确处理方================================

看官方介绍,还有更多方法:
1、这个是上面方法的正确写法:

System Properties Lookup As it is quite common to define values inside
and outside the application by using System Properties, it is only
natural that they should be accessible via a Lookup. As system
properties are often defined outside the application it would be quite
common to see something like:

2、直接随web项目路径:

Web Lookup The WebLookup allows applications to retrieve variables
that are associated with the ServletContext. In addition to being able
to retrieve various fields in the ServletContext, WebLookup supports
looking up values stored as attributes or configured as initialization
parameters. The following table lists various keys that can be
retrieved:

Key Description attr.name Returns the ServletContext attribute with

the specified name contextPath The context path of the web application
effectiveMajorVersion Gets the major version of the Servlet
specification that the application represented by this ServletContext
is based on. effectiveMinorVersion Gets the minor version of the
Servlet specification that the application represented by this
ServletContext is based on. initParam.name Returns the ServletContext
initialization parameter with the specified name majorVersion Returns
the major version of the Servlet API that this servlet container
supports. minorVersion Returns the minor version of the Servlet API
that this servlet container supports. rootDir Returns the result of
calling getRealPath with a value of "/". serverInfo Returns the name
and version of the servlet container on which the servlet is running.
servletContextName Returns the name of the web application as defined
in the display-name element of the deployment descriptor Any other key
names specified will first be checked to see if a ServletContext
attribute exists with that name and then will be checked to see if an
initialization parameter of that name exists. If the key is located
then the corresponding value will be returned.

转载地址:http://ntmaa.baihongyu.com/

你可能感兴趣的文章
wget 参数大全
查看>>
使用Loadrunner进行文件的上传和下载
查看>>
Linux C 静态库(.a) 与 动态库(.so) 的详解
查看>>
JS函数
查看>>
sql语句分组/排序/计算总数/连接等sql语句书写
查看>>
【.net 深呼吸】启动一个进程并实时获取状态信息
查看>>
MVC5 的MicrosoftOwinSecurity扩展插件——微信,QQ登录第三方源码
查看>>
分布式系统理论基础 - CAP
查看>>
mysql 用户管理和权限设置
查看>>
【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目
查看>>
[转]Asp.Net大型项目实践(11)-基于MVC Action粒度的权限管理【续】【源码在这里】(在线demo,全部源码)...
查看>>
Numpy 的ndarray
查看>>
牛人博客 列表
查看>>
数据库连接池dataesoruce pool深入理解
查看>>
vuejs2.0使用Sortable.js实现的拖拽功能
查看>>
oracle多实例的启动与关闭
查看>>
码农生涯杂记_4
查看>>
10-排序4 统计工龄
查看>>
postgresql的show databases、show tables、describe table操作
查看>>
利用jQuery设计横/纵向菜单
查看>>