We have a static java.util.logging.Logger variable as myLogger. Currently, it prints only to console so we have to add FileHandler to it. The project is Profile based so depending on the profile specified at the compile time, appropriate application-{profile}.properties file will be loaded. This file has different log file location for each profile.
How can a FileHandler be instantiated with value from application.properties?
Code snippet:
private static String logFileName;
@Value("${loggingSftp.logFileName}")
public void setLogFileName(String logFileName) {
SftpConfiguration.logFileName = logFileName;
}
private static final Logger LOGGER = Logger.getLogger(SftpConfiguration.class.getPackage().getName());
private static FileHandler fileHandler;
static {
try {
LOGGER.info("log file name: " + logFileName);
fileHandler = new FileHandler(LoggingFileHandler.getLogFileName()
+ LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")));
LOGGER.addHandler(fileHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
Now, logFileName is 'null' when static block is initializing and adding fileHandler.