roy_sanu
asked on
How Can I Pipe the Java Console Output to log file
Good day,
Let me know how to pipe the console info to a log file
thank you
Let me know how to pipe the console info to a log file
thank you
ASKER
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;
}
}
ASKER
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............
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);
}
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
http://www.crazysquirrel.com/computing/java/logging.jspx
ASKER
i have added like this
log4j.properties file
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
removed the console it is working fine
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