I would like to set the log file name for a log4j.xml. We are doing Daily rollovers. The log file name format would be hostname_current datetime.log. I am using application server as JBoss EAP 6.2
2 Answers
You can configure log4j on runtime with PropertyConfigurator.configure(path.log4j.properties); Now, if you want to change the log file name, you can use different paths each time or change the properties file dynamically. Maybe there is a better way to do that without using the file, but I do not know it.
I dont know if you use log4j 1.x or log4j 2.x. In log4j there are appenders where you clearly define what the name of your log-file is and where log4j has to deploy it etc...
If you want to set it dynamically you have to rewrite the log4j.xml by a programm or whatever.
Here is a snip of a settings-file using a FileAppender
:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<File name="MyFile" fileName="logs/anameyouwant.log" append="true">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%-5level] %msg [%t] %logger{30} %n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
In this example I use a FileAppender which writes the log to /logs/anameyouwant.log
. It appends the information to the logfile.
This is the part you have to rewrite dynamically: fileName="logs/anameyouwant.log"
I dont see other ways to resolve the problem except this solution where you rewrite the settings-file at the beginning before your run your programm and start logging.
Here are maybe some helpfull Links: