How do I capture error messages?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

cp88483998
Posts: 8
Joined: 25 Aug 2017, 10:14

How do I capture error messages?

Postby cp88483998 » 12 Sep 2017, 06:27

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~
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How do I capture error messages?

Postby Lapo » 12 Sep 2017, 07:45

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
Lapo
--
gotoAndPlay()
...addicted to flash games
cp88483998
Posts: 8
Joined: 25 Aug 2017, 10:14

Re: How do I capture error messages?

Postby cp88483998 » 12 Sep 2017, 07:59

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?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How do I capture error messages?

Postby Lapo » 12 Sep 2017, 08:39

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
Lapo

--

gotoAndPlay()

...addicted to flash games
cp88483998
Posts: 8
Joined: 25 Aug 2017, 10:14

Re: How do I capture error messages?

Postby cp88483998 » 12 Sep 2017, 08:45

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!

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 50 guests