一些log4j配置问题 [英] a number of log4j config questions

查看:81
本文介绍了一些log4j配置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目上,我们想使用log4j处理日志记录.我遇到了一些问题,这些问题无法通过在线查看log4j文档或其他文档来轻松解决.

I'm working on a project and we want to handle our logging using log4j. I am running into some issues that I am not able to easily resolve looking at the log4j docs, or other documentation online.

我得到了将日志记录代码放在整个代码库中,然后让属性文件将记录的数据分类为附加程序的层次结构以及如何写出文件的基本思想.没关系.基本上,这使我可以在一个硬编码的文件夹中创建可抓取的日志文件,例如:

I get the basic idea of putting logging code throughout the codebase and then having the properties file assort the logged data into a hierarchy of appenders and how to write out to a file. That's fine. This basically allows me to create greppable log files in one hard coded folder, such as this:

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

但是我有两个基本问题:我希望日志位置是动态的,例如:

But I have two basic questions: I want to have the log location be dynamic, such as:

log4j.appender.R.File={$processDir}/example.log

此外,每次用户运行此应用程序时,都会使用输出文件创建一个文件夹.我希望将日志文件放置在此处,但我不确定如何完成该操作.

Also, every time the user runs this app, a folder will be created with the output files. I would like to have the log file be placed there, and I'm not sure how to accomplish that.

另一个问题(尽管我认为解决第一个问题会容易得多...)是关于创建格式化日志,该日志不一定反映应用程序运行的过程...例如,标题,然后是所有输入文件的列表,所有输出文件的列表以及遇到的任何警告.

The other issue (although I think this will be a lot easier once the first issue is addressed...) is about creating a formatted log that does not necessarily reflect the process of how the app ran...for example, a title, followed by a list of all input files, a list of all output files, any warnings encountered.

为此,我想创建一个实现ObjectRenderer的对象,并编写一个doRender方法,该方法为我提供了所需的信息.

I think for that I would create an object that implemented ObjectRenderer and write a doRender method that gave me the info I wanted.

听起来正确吗? 谢谢!

Does that sound correct? Thanks!

推荐答案

您可以使用具有这种语法的变量

You can use variable with this syntax

log4j.appender.R.File=${processDir}/example.log

您必须将变量定义为系统属性(例如-DprocessDir = ...)或使用

You must define the variables as system properties (es. -DprocessDir=...) or manually (after creating folder) with

System.setProperty("processDir",logDir);

这篇关于一些log4j配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆