Facing problem in log4j error log and exception handling - java

I have used log4j for looging error log using FileAppender. The problem is its logging the same error two times in a log file when the below situation
Case1:
Class1 :
public void func(){
try{
new Class2.prop()
}catch(IOException ioe){
logger.log(2,ioe);
}
}
Class2 :
public void prop(){
try{
//error oocurs here
}catch(FileNotFoundException fe){
logger.log(2,fe);
}
}
Error :
Class2 .FileNotFoundException
at Class2.prop(Class2.java:3)
at Class1.func(Class1.java:4)
Log File :
FileNotFound exception
FileNotFound exception
But its logging the error one time for the below case.
Case2 :
Class1 :
public void func(){
try{
new Class2.prop()
//error oocurs here
}catch(IOException ioe){
logger.log(2,ioe);
}
}
Class2 :
public void prop(){
try{
}catch(FileNotFoundException fe){
logger.log(2,fe);
}
}
Error :
Class2 .IOException
at Class1.func(Class1.java:4)
Log File :
IOException exception
Help me what should i do to log the error only one time in a log file whereever it is.

But its logging the error one time for the below case.
That's because you're handling the exception:
Class1:
public void func() {
try{
new Class2.prop()
}catch(IOException ioe){
logger.log(2,ioe);
}
}
Class2 :
public void prop() throws IOException {
try{
//error oocurs here
}catch(FileNotFoundException fe){
logger.log(2,fe);
throw fe;
}
// Here!!!!!
}
In your catch block of the class2 ( after your //error oocurs here ) you log the exception, that's what you've got on your logs.
But since you are just logging the exception, you're telling the program that the exception has been controlled somehow, or handled ( which is more appropriate) and the program continues it's flow to the line where I added the comment // Here!!!!!
Later in class1, since the exception was handled, nothing happens in your try/catch block and your second exception is never logged ( as you expected ) because it never happened.
If you want to see two logs ( which I think it's unnecessary ) you should re-throw the exception as I did in your class2 and plus, modify the method signature to mark indicate it throws an IOException.
That way you will have two logs.
Better will be like this:
Class1:
public void func() {
try{
new Class2.prop()
}catch(IOException ioe){
logger.log(2,ioe);
}
}
Class2 :
public void prop() throws IOException {
//error oocurs here
}
In class 2 you don't handle the exception, you just let it go through the caller. In the stacktrace you'll have the information anyway.
I hope this helps.

In your log4j configuration, do you have the logger used in Class 2 being sent to your appender twice?

Related

chain exception in catch block

I have the following java code :
public void someMethod(){
try{
// some code which generates Exception
}catch(Exception ex1) {
try{
// The code inside this method can also throw some Exception
myRollBackMethodForUndoingSomeChanges();
}catch(Exception ex2){
// I want to add inside `ex2` the history of `ex1` too
// Surely , I cannot set cause of `ex2` as `ex1` as `ex2`
// can be caused by it's own reasons.
// I dont want `ex1` details to be lost if I just throw `ex2` from my method
}
}
}
How to do it ?
EDIT : Actually this happens in my service layer and I have controller advice for logging. Hence I don't want to add 2 loggers here.
You can add ex1 to the supressed exceptions in ex2 via the method addSuppressed before rethrowing it.
Quick code example:
public static void main(final String[] args) {
try {
throw new IllegalArgumentException("Illegal Argument 1!");
} catch (final RuntimeException ex1) {
try {
throw new IllegalStateException("Illegal State 2!");
} catch (final RuntimeException ex2) {
ex2.addSuppressed(ex1);
throw ex2;
}
}
}
will produce the exception output:
Exception in thread "main" java.lang.IllegalStateException: Illegal State 2!
at package.main(Main.java:26)
Suppressed: java.lang.IllegalArgumentException: Illegal Argument 1!
at package.main(Main.java:20)

remove last method from stacktrace before throwing exception

I have a utility method for timing and logging various queries all over the project.
The problem is, when looking at crashlytics now all unrelated crashes are joined together into one crash-instance.
Can I catch all exceptions on the utility method, and throw them after removing that method from the stack?
The environment is Android (Java)
UPDATE:
based on #Dhananjay's answer below, here's my code:
public static Cursor get(...) {
try {
// my utility code
} catch (RuntimeException e) {
throw cleanException(e);
}
}
private static RuntimeException cleanException(RuntimeException e) {
try {
StackTraceElement[] stackTrace = e.getStackTrace();
StackTraceElement[] subTrace = new StackTraceElement[stackTrace.length - 1];
System.arraycopy(stackTrace, 1, subTrace, 0, subTrace.length);
e.setStackTrace(subTrace);
return e;
} catch (Throwable ignored) {
return e;
}
}
This approach might solve your problem: Set the stacktrace of the exception in the utility logging method to exclude the utility method itself, and then throw the exception, here is a working example, you can modify it to eliminate any StackTraceElement you want to:
package test;
public class TestMain {
public static void main(String args[]) throws Exception {
try {
apiCall();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void apiCall() throws Exception {
logAndThrow();
}
public static void logAndThrow() throws Exception {
Exception e = new Exception();
StackTraceElement[] cleanedUpStackTrace = new StackTraceElement[e.getStackTrace().length -1];
// Eliminate this mehod i.e. logAndThrow's stack trace entry (i.e. the first one) in cleanedUpStackTrace
System.arraycopy(e.getStackTrace(), 1, cleanedUpStackTrace, 0, cleanedUpStackTrace.length);
for(StackTraceElement ste : cleanedUpStackTrace) {
System.out.println(ste.getMethodName());
}
e.setStackTrace(cleanedUpStackTrace);
throw e;
}
}
Here is the output of this program, the logAndThrow method is not present in stack trace now:
apiCall
main
java.lang.Exception
at test.TestMain.apiCall(TestMain.java:33)
at test.TestMain.main(TestMain.java:25)

catch exception without getting in its try block?

I have an interesting issue. My code block is given below. The problem is : log file does not contains "Getting ready to sleep.." line but it contains "Backup thread was interrupted..." lines.
My question : Is it possible to catch exception without getting in its try block ?
long sleepTime = runtime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
try {
log("Getting ready to sleep...(" + sleepTime + ")");
Thread.sleep(sleepTime);
Database db = new Database();
log(db.performDatabaseBackup());
// Set it to run the next day.
runtime.add(Calendar.DATE, 1);
} catch (InterruptedException ie) {
log("Backup thread was interrupted...");
}
Edit : Log method
private void log(String message) {
Logger.debug(this, message);
}
and Logger.debug
public static void debug(
Object obj,
String message ){
debug( obj.getClass(), message );
}
public static void debug(
String className,
String message )
{
try
{
Class inputClass = Class.forName( className );
debug( inputClass, message );
}
catch ( ClassNotFoundException e )
{
debug( ( Class )null, message );
}
}
No it is not possible to catch an Exception not thrown during execution of its try block. Possible situations:
InterruptedException is thrown before you log "Getting ready..." i.e. in your log(...) call. (unlikely)
Log method does not work as expected and does not log your line. (likely) Can you check if the sleep is performed by setting a high sleep time. That would imply this reason.
You have other pieces of code logging "Backup thread was interrupted..." that cause the log output and the presented code fragement is not executed at all.
My guess would be that the log method is not functioning properly? I don't believe that the catch block can be entered without entering the try block. Another alternative is that the log method throws InterruptedException?
Can you post your log method as well?
If you enter the catchblock an exception has been thrown inside the try block. Period.
To solve your problem: Print the stacktrace of the exception and look if log() also throws some exceptions.

File writing in Robocode (Java)

Basically, I am trying to generate a log file in Robocode, but I am having issues as you cannot use try/catch in Robocode (as far as I am aware). I have done the following:
public void onBattleEnded(BattleEndedEvent e) throws IOException
{
writeToLog();
throw new IOException();
}
and
public void writeToLog() throws IOException
{
//Create a new RobocodeFileWriter.
RobocodeFileWriter fileWriter = new RobocodeFileWriter("./logs/test.txt");
for (String line : outputLog)
{
fileWriter.write(line);
fileWriter.write(System.getProperty("line.seperator"));
}
throw new IOException();
}
and am getting the following error at compile time:-
MyRobot.java:123: onBattleEnded(robocode.BattleEndedEvent) in ma001jh.MyRobot cannot implement onBattleEnded(robocode.BattleEndedEvent) in robocode.robotinterfaces.IBasicEvents2; overridden method does not throw java.io.IOException
public void onBattleEnded(BattleEndedEvent e) throws IOException
^
1 error
As you can see here, the interface doesn't declare any checked exceptions. So you can't throw one in your implementing class.
One way to solve this would be to implement your method like this:
public void onBattleEnded(BattleEndedEvent e)
{
writeToLog();
throw new RuntimeException(new IOException());
}
public void writeToLog()
{
//Create a new RobocodeFileWriter.
RobocodeFileWriter fileWriter = new RobocodeFileWriter("./logs/test.txt");
for (String line : outputLog)
{
fileWriter.write(line);
fileWriter.write(System.getProperty("line.seperator"));
}
throw new new RuntimeException(new IOException());
}
but I am having issues as you cannot use try/catch in Robocode (as far as I am aware)
Where did this assumption came from? I just because of your question here installed robocode (so it's your fault if I'll answer here less often in future), wrote my own robot and it can catch exceptions quite good:
try {
int i = 1/0;
}
catch(ArithmeticException ex) {
ex.printStackTrace();
}
And why are you throwing IOExceptions in your example?

How to send a stacktrace to log4j?

Say you catch an exception and get the following on the standard output (like, say, the console) if you do a e.printStackTrace() :
java.io.FileNotFoundException: so.txt
at java.io.FileInputStream.<init>(FileInputStream.java)
at ExTest.readMyFile(ExTest.java:19)
at ExTest.main(ExTest.java:7)
Now I want to send this instead to a logger like, say, log4j to get the following:
31947 [AWT-EventQueue-0] ERROR Java.io.FileNotFoundException: so.txt
32204 [AWT-EventQueue-0] ERROR at java.io.FileInputStream.<init>(FileInputStream.java)
32235 [AWT-EventQueue-0] ERROR at ExTest.readMyFile(ExTest.java:19)
32370 [AWT-EventQueue-0] ERROR at ExTest.main(ExTest.java:7)
How can I do this?
try {
...
} catch (Exception e) {
final String s;
... // <-- What goes here?
log.error( s );
}
You pass the exception directly to the logger, e.g.
try {
...
} catch (Exception e) {
log.error( "failed!", e );
}
It's up to log4j to render the stack trace.
If you want to log a stacktrace without involving an exception just do this:
String message = "";
for(StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
message = message + System.lineSeparator() + stackTraceElement.toString();
}
log.warn("Something weird happened. I will print the the complete stacktrace even if we have no exception just to help you find the cause" + message);
You can also get stack trace as string via ExceptionUtils.getStackTrace.
See: ExceptionUtils.java
I use it only for log.debug, to keep log.error simple.
The answer from skaffman is definitely the correct answer. All logger methods such as error(), warn(), info(), debug() take Throwable as a second parameter:
try {
...
} catch (Exception e) {
logger.error("error: ", e);
}
However, you can extract stacktrace as a String as well. Sometimes it could be useful if you wish to take advantage of formatting feature using "{}" placeholder - see method void info(String var1, Object... var2); In this case say you have a stacktrace as String, then you can actually do something like this:
try {
...
} catch (Exception e) {
String stacktrace = TextUtils.getStacktrace(e);
logger.error("error occurred for usename {} and group {}, details: {}",username, group, stacktrace);
}
This will print parametrized message and the stacktrace at the end the same way it does for method: logger.error("error: ", e);
I actually wrote an open source library that has a Utility for extraction of a stacktrace as a String with an option to smartly filter out some noise out of stacktrace. I.e. if you specify the package prefix that you are interested in your extracted stacktrace would be filtered out of some irrelevant parts and leave you with very consized info. Here is the link to the article that explains what utilities the library has and where to get it (both as maven artifacts and git sources) and how to use it as well. Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison See the paragraph "Stacktrace noise filter"
Just because it happened to me and can be useful.
If you do this
try {
...
} catch (Exception e) {
log.error( "failed! {}", e );
}
you will get the header of the exception and not the whole stacktrace. Because the logger will think that you are passing a String.
Do it without {} as skaffman said
In Log4j 2, you can use Logger.catching() to log a stacktrace from an exception that was caught.
try {
String msg = messages[messages.length];
logger.error("An exception should have been thrown");
} catch (Exception ex) {
logger.catching(ex);
}
This answer may be not related to the question asked but related to title of the question.
public class ThrowableTest {
public static void main(String[] args) {
Throwable createdBy = new Throwable("Created at main()");
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(os);
createdBy.printStackTrace(pw);
try {
pw.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
logger.debug(os.toString());
}
}
OR
public static String getStackTrace (Throwable t)
{
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
printWriter.close(); //surprise no IO exception here
try {
stringWriter.close();
}
catch (IOException e) {
}
return stringWriter.toString();
}
OR
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(StackTraceElement stackTrace: stackTraceElements){
logger.debug(stackTrace.getClassName()+ " "+ stackTrace.getMethodName()+" "+stackTrace.getLineNumber());
}
this would be good log4j error/exception logging - readable by splunk/other logging/monitoring s/w. everything is form of key-value pair.
log4j would get the stack trace from Exception obj e
try {
---
---
} catch (Exception e) {
log.error("api_name={} method={} _message=\"error description.\" msg={}",
new Object[]{"api_name", "method_name", e.getMessage(), e});
}
Try this:
catch (Throwable t) {
logger.error("any message" + t);
StackTraceElement[] s = t.getStackTrace();
for(StackTraceElement e : s){
logger.error("\tat " + e);
}
}
You can use bellow code:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class LogWriterUtility {
Logger log;
public LogWriterUtility(Class<?> clazz) {
log = LogManager.getLogger(clazz);
}
public void errorWithAnalysis( Exception exception) {
String message="No Message on error";
StackTraceElement[] stackTrace = exception.getStackTrace();
if(stackTrace!=null && stackTrace.length>0) {
message="";
for (StackTraceElement e : stackTrace) {
message += "\n" + e.toString();
}
}
log.error(message);
}
}
Here you can just call : LogWriterUtility.errorWithAnalysis( YOUR_EXCEPTION_INSTANCE);
It will print stackTrace into your log.
Create this class:
public class StdOutErrLog {
private static final Logger logger = Logger.getLogger(StdOutErrLog.class);
public static void tieSystemOutAndErrToLog() {
System.setOut(createLoggingProxy(System.out));
System.setErr(createLoggingProxy(System.err));
}
public static PrintStream createLoggingProxy(final PrintStream realPrintStream) {
return new PrintStream(realPrintStream) {
public void print(final String string) {
logger.info(string);
}
public void println(final String string) {
logger.info(string);
}
};
}
}
Call this in your code
StdOutErrLog.tieSystemOutAndErrToLog();

Categories