logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Yogesh Bhardwaj (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LOG4J2-1231) Log files are under lock, not able to view them until JVM stop.
Date Mon, 28 Dec 2015 07:03:49 GMT

    [ https://issues.apache.org/jira/browse/LOG4J2-1231?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15072491#comment-15072491
] 

Yogesh Bhardwaj commented on LOG4J2-1231:
-----------------------------------------

Hi Ralph , Thanks for looking into this. Not sure what kind of project you are looking for,
let me share the code snippet to test this configuration.

You can test it in eclipse or even with console, the 2 parameters needs to be change as per
your system.
1) In log4j.java you need to specify the path of log4j2.xml, which i am setting wth below
code
String configFilePath = (new StringBuilder(String.valueOf(env)))
				.append(fs).append("shared-classes").append(fs)
				.append("log4j2.xml").toString();

2) In Test.java you need to specify the file path and file name which i implemented by using
"log4j.initLog4j("D:\\logs", "Test");" .

Most Important, My application need to run continuously( Thread will always be live), on that
basis i have wrote Test.java, my concern is until we provide input "exit " we will not be
able to open the log files created in the directory specified.

NOTE:- Log4j Jar were placed in class path.

Please be advise for any further query, We can connect via Lync(bhardwaj@hpe.com).
 

log4j.java
package com.Flog;

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);
	}
}


Test.java
package com.Flog;

import java.util.Scanner;

public class Test {

	public static void main(String args[]) {
		log4j.initLog4j("D:\\logs", "Test");
		System.out.println("Hi there!");
		boolean sta = true;
		for (; sta;) {

			System.out.println("1. Enter Exit for Quit");
			System.out.println("2. Enter your Name to proceed:");
			Scanner scan = new Scanner(System.in);
			String code = scan.nextLine().toUpperCase();
			String exit = "EXIT";
			if (code.equals(exit)) {
				sta = false;
			} else {
				log4j.logDebug("[IIB9NODE.DEFAULT]"
						+ "sample log message logDebug " + code);
				log4j.logError("[IIB9NODE.DEFAULT]"
						+ "sample log message logError " + code);
				log4j.logInfo("[IIB9NODE.DEFAULT]"
						+ "sample log message logInfo " + code);
				log4j.logTrace("[IIB9NODE.DEFAULT]"
						+ "sample log message logTrace " + code);
				log4j.logWarn("[IIB9NODE.DEFAULT]"
						+ "sample log message logWarn " + code);
			}

		}
	}

}


log4j2.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>


> Log files are under lock, not able to view them until JVM stop.
> ---------------------------------------------------------------
>
>                 Key: LOG4J2-1231
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1231
>             Project: Log4j 2
>          Issue Type: Question
>          Components: Appenders
>    Affects Versions: 2.5
>         Environment:  window, jre 1.7, IBM Integration Bus
>            Reporter: Yogesh Bhardwaj
>            Priority: Blocker
>              Labels: newbie
>             Fix For: 2.5.1
>
>   Original Estimate: 504h
>  Remaining Estimate: 504h
>
> 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
> {code}
> 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);
> 	}
> }
> {code}
> log4j.xml
> {code}
> <?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>
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message