I am working on a multithreading application in which i want to do logging at different locations using log4j.
Scenario:
Folder1 ---> log1.log
log2.log
log3.logFolder2 ---> log1.log
log2.log
log3.log
I want my loggers to independently log into files.
Till now, i have created 3 RollingFileAppenders which corresponds to 3 loggers for Folder1 but the problem is "I will have around 100's of folders and i don't want to write Appenders and loggers for them in my log4j.xml file".
<appender name="logfile1" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log1.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<appender name="logfile2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log2.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<appender name="logfile3" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="Folder1/log3.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<logger name="Logger1" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile1" />
</logger>
<logger name="Logger2" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile2" />
</logger>
<logger name="Logger3" additivity="false">
<level value="INFO" />
<appender-ref ref="logfile3" />
</logger>
The folders will be created at runtime
MY APPROACH:
I want to instantiate my loggers according to my folder names and keep them in a hashmap like
HashMap<FolderName, HashMap<LoggerType, LoggerObject>>
FolderName: String
LoggerType: it will be an Enum of log1, log2 and log3
LoggerObject: It will a log4j Logger object
So, i will provide one method to get the LoggerObject like
public Logger getLoggerObject(String folderName, LoggerType loggerType){
\\logic
return loggerObject;
}
Logging.getLoggerObject("Folder1", LoggerType.log1).info("I am writing to log1.log file in Folder1");
Questions:
1. I am not able to configure my LoggerObjects at runtime. Help me in doing that!
2. How to configure my loggers for multiple folder locations and multiple logging files?