Solved

How Can I Pipe the Java Console Output to log file

Posted on 2014-01-02
7
338 Views
Last Modified: 2014-01-12
Good day,

Let me know how to pipe the console info to a log file

thank you
0
Comment
Question by:roy_sanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 27

Expert Comment

by:dpearson
ID: 39750919
Do you mean?

java -jar my.jar > /tmp/mylog.log 2>&1

(Or use 'tee' if you want it in the log file and still on the console
http://linux.101hacks.com/unix/tee-command-examples/)

Doug
0
 

Author Comment

by:roy_sanu
ID: 39750929
I am not able to capture console errors  to an log file

public class MIDSkipPolicy implements SkipPolicy {

	
	private static final Log log = LogFactory.getLog(MIDSkipPolicy.class);

	private Integer skipLimit = null;

	@Override
	public boolean shouldSkip(Throwable throwable, int skipCount)
			throws SkipLimitExceededException {
		
		StringBuffer stringBuffer = new StringBuffer();
		
		if (throwable instanceof FlatFileParseException) {
			int linenumber = ((FlatFileParseException) throwable).getLineNumber();
			String input = ((FlatFileParseException) throwable).getInput();
			
			stringBuffer.append("Parsing exception encountered on line #" + linenumber + "; input = " + input);
			log.error(stringBuffer.toString());
		} else {

			stringBuffer.append("Exception thrown causing record to be skipped: ");
			stringBuffer.append(throwable.getMessage());
			log.error(stringBuffer.toString());
			log.debug(throwable);
		}
		
		if (getSkipLimit() <= skipCount && getSkipLimit() > 0) {
			SkipLimitExceededException skipLimitExceededException = new SkipLimitExceededException(skipCount, throwable);			
			log.error(skipLimitExceededException.getMessage());			
			log.debug(skipLimitExceededException);
			
			throw skipLimitExceededException;
		}
		
		return true;
	}

	public int getSkipLimit() {
		return skipLimit;
	}

	public void setSkipLimit(int skipLimit) {
		this.skipLimit = skipLimit;
	}
}

Open in new window

0
 

Author Comment

by:roy_sanu
ID: 39751100
Adding try catch block  but it is not  able the  capture error to log files.........
where is the issue ......... Any right comments are highly appreciable............

try {
		        logger.setLevel(Level.INFO);

} catch (Exception ex) {
		         // Log the stack trace
		         logger.(Level.SEVERE, ex.getMessage(), ex);
		      }

Open in new window

0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 86

Expert Comment

by:CEHJ
ID: 39751158
That looks like the badly-documented java.util.logging. If so, you can specify the location of log files with the logging.properties file, the location of which itself you can specify as a jvm property

http://www.crazysquirrel.com/computing/java/logging.jspx
0
 

Author Comment

by:roy_sanu
ID: 39760007
i have added like this
 

but am not able to pipe console error  to a log4j... still it is showing in the console...
do not want to see the error in the console,

log4j.properties file

log4j.rootLogger=ERROR, CONSOLE, FILE

# Main appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


# skip logger
log4j.logger.com.tax.care.listener.MIDSkipPolicy=ERROR, skipappender,summaryappender, CONSOLE, File
log4j.additivity.com.tax.care.listener.MIDSkipPolicy=false

log4j.appender.skipappender=com.tax.care.log.MidLogAppender
log4j.appender.skipappender.File=skipped
log4j.appender.skipappender.MaxFileSize=10MB
log4j.appender.skipappender.MaxBackupIndex=2
log4j.appender.skipappender.File.Append=false
log4j.appender.skipappender.File.BufferedIO=true
log4j.appender.skipappender.File.DatePattern=.yyyy-MM-dd
log4j.appender.skipappender.layout=org.apache.log4j.PatternLayout
log4j.appender.skipappender.layout.ConversionPattern=%d %m%n

Open in new window

0
 

Accepted Solution

by:
roy_sanu earned 0 total points
ID: 39762794
removed the

summaryappender, CONSOLE, File

Open in new window


yes i could able to log the record
0
 

Author Closing Comment

by:roy_sanu
ID: 39774461
removed the console it is working fine
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question