Hello:
I want use "try{}catch{}" to capture error messages and print error line numer to log file.
What I'm using now is "throw new SFSExcception(..)", but it can't print error line numer.
Can you tell which api i should use and provide me with a case as well?
thanks~
How do I capture error messages?
-
- Posts: 8
- Joined: 25 Aug 2017, 10:14
Re: How do I capture error messages?
Hi,
you need to send the stack trace to the logs.
Every Exception object in Java has a getStackTrace() method, with the details of the error.
In SmartFoxServer 2X we have a helpful class called ExceptionMessageComposer (found under com.smartfoxserver.v2.exceptions)
The class formats an exception with several extra information and sends it to the logs.
Here's an Example:
setDescription() is where you add a description of the error, for example "The expected file was not found".
addInfo() will add extra lines of explanation, if needed. You can add as many as necessary.
Keep in mind that both methods are optional, and even if you don't use it the final output will contain Exception message and its stack trace.
Hope it helps
you need to send the stack trace to the logs.
Every Exception object in Java has a getStackTrace() method, with the details of the error.
In SmartFoxServer 2X we have a helpful class called ExceptionMessageComposer (found under com.smartfoxserver.v2.exceptions)
The class formats an exception with several extra information and sends it to the logs.
Here's an Example:
Code: Select all
try
{
// some code
}
catch (Exception ex)
{
ExceptionMessageComposer emc = new ExceptionMessageComposer(ex);
emc.setDescription("Optional description of the problem");
emc.addInfo("additional information, if necessary");
// ...
log.warn(emc.toString());
}
setDescription() is where you add a description of the error, for example "The expected file was not found".
addInfo() will add extra lines of explanation, if needed. You can add as many as necessary.
Keep in mind that both methods are optional, and even if you don't use it the final output will contain Exception message and its stack trace.
Hope it helps
-
- Posts: 8
- Joined: 25 Aug 2017, 10:14
Re: How do I capture error messages?
Lapo wrote:Hi,
you need to send the stack trace to the logs.
Every Exception object in Java has a getStackTrace() method, with the details of the error.
In SmartFoxServer 2X we have a helpful class called ExceptionMessageComposer (found under com.smartfoxserver.v2.exceptions)
The class formats an exception with several extra information and sends it to the logs.
Here's an Example:Code: Select all
try
{
// some code
}
catch (Exception ex)
{
ExceptionMessageComposer emc = new ExceptionMessageComposer(ex);
emc.setDescription("Optional description of the problem");
emc.addInfo("additional information, if necessary");
// ...
log.warn(emc.toString());
}
setDescription() is where you add a description of the error, for example "The expected file was not found".
addInfo() will add extra lines of explanation, if needed. You can add as many as necessary.
Keep in mind that both methods are optional, and even if you don't use it the final output will contain Exception message and its stack trace.
Hope it helps
thanks~
I found an other way to print error message:
Code: Select all
try{
......
}catch (Exception e) {
getLogger().error(e.toString()
+"\n"
+"at "+e.getStackTrace()[3].getClassName()
+"."+e.getStackTrace()[3].getMethodName()
+" ("+e.getStackTrace()[3].getFileName()
+":"+e.getStackTrace()[3].getLineNumber()
+")");
}
but, why could't "e.printStackTrace();" print to my log file?
Re: How do I capture error messages?
Hi,
there is an even simpler way using getLogger():
This will send the full stack trace and your custom message straight to the logs.
That doesn't work because printStackTrace() sends the text to the standard output, not to the logger. The standard output by default goes to the console, unless you redirect it manually, which is redundant given you already a logging system in place.
Hope it helps
there is an even simpler way using getLogger():
Code: Select all
catch(Exception ex)
{
getLogger().warn("My custom error message, ex);
}
This will send the full stack trace and your custom message straight to the logs.
but, why could't "e.printStackTrace();" print to my log file?
That doesn't work because printStackTrace() sends the text to the standard output, not to the logger. The standard output by default goes to the console, unless you redirect it manually, which is redundant given you already a logging system in place.
Hope it helps
-
- Posts: 8
- Joined: 25 Aug 2017, 10:14
Re: How do I capture error messages?
Lapo wrote:Hi,
there is an even simpler way using getLogger():Code: Select all
catch(Exception ex)
{
getLogger().warn("My custom error message, ex);
}
This will send the full stack trace and your custom message straight to the logs.but, why could't "e.printStackTrace();" print to my log file?
That doesn't work because printStackTrace() sends the text to the standard output, not to the logger. The standard output by default goes to the console, unless you redirect it manually, which is redundant given you already a logging system in place.
Hope it helps
I see. Thank you very much!
Who is online
Users browsing this forum: No registered users and 100 guests