Details
Description
Hi Team,
The problem is when I use file appender with below configuration.
In each application thread I am calling "initLog4j" followed by other function mentioned in below class, JVM at server level not application level which is configured to load log4j specific jar files. The log files are getting created as per my configuration but being a Admin when I try to open them I am getting messages " document "..........." is under use by some other application and can't be accessed." I can see one thread is still running although my application thread is ended.
As per my understanding if admin can't see the logs even when application is running, no use of such logging , Could you please confirm is this expected behavior, is their any alternative for this.....?
Daemon Thread :
Daemon Thread [MemoryPoolMXBean notification dispatcher] (Running)
My class
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class log4j { public log4j() { } public static void initLog4j(String logPath1, String filename1) { String env = System.getenv("MQSI_WORKPATH"); String fs = System.getProperty("file.separator"); String filePath = logPath1; System.setProperty("logPath", filePath); System.setProperty("fileName", filename1); String configFilePath = (new StringBuilder(String.valueOf(env))) .append(fs).append("shared-classes").append(fs) .append("log4j2.xml").toString(); System.setProperty("log4j.configurationFile", configFilePath); } public static void logDebug(String logText) { String loggerName = logText.substring(logText.indexOf('[') + 1, logText.indexOf(']')); Logger logger = LogManager.getLogger(loggerName); logger.debug(logText); } public static void logInfo(String logText) { String loggerName = logText.substring(logText.indexOf('[') + 1, logText.indexOf(']')); Logger logger = LogManager.getLogger(loggerName); logger.info(logText); } public static void logWarn(String logText) { String loggerName = logText.substring(logText.indexOf('[') + 1, logText.indexOf(']')); Logger logger = LogManager.getLogger(loggerName); logger.warn(logText); } public static void logTrace(String logText) { String loggerName = logText.substring(logText.indexOf('[') + 1, logText.indexOf(']')); Logger logger = LogManager.getLogger(loggerName); logger.trace(logText); } public static void logError(String logText) { String loggerName = logText.substring(logText.indexOf('[') + 1, logText.indexOf(']')); Logger logger = LogManager.getLogger(loggerName); logger.error(logText); } }
log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <File name="info-log" fileName="${sys:logPath}/${sys:fileName}_info.log" immediateFlush="true"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </File> <File name="trace-log" fileName="${sys:logPath}/${sys:fileName}_trace.log" immediateFlush="true"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </File> <File name="error-log" fileName="${sys:logPath}/${sys:fileName}_error.log" immediateFlush="true"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </File> <File name="debug-log" fileName="${sys:logPath}/${sys:fileName}_debug.log" immediateFlush="true"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </File> <File name="warn-log" fileName="${sys:logPath}/${sys:fileName}_warn.log" immediateFlush="true"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </File> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="trace-log" level="TRACE" /> <AppenderRef ref="info-log" level="INFO" /> <AppenderRef ref="warn-log" level="WARN" /> <AppenderRef ref="debug-log" level="DEBUG" /> <AppenderRef ref="error-log" level="ERROR" /> </Root> </Loggers> </Configuration>