a number of log4j config questions

2019-08-29 11:10发布

问题:

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.

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!

回答1:

You can use variable with this syntax

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

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

System.setProperty("processDir",logDir);


标签: log4j