Tracing from custom class.

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

Moderators: Lapo, Bax

Rotty956
Posts: 74
Joined: 02 Jan 2011, 15:58

Tracing from custom class.

Postby Rotty956 » 23 Feb 2011, 14:00

Hey everyone
How would one trace messages from the custom class one that isn't inheriting and smartfox classes? Can I import something? Do I need to extend a base class? Do I need create an object of some sort?
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 24 Feb 2011, 05:37

1) Include the library log4j-1.2.15.jar

2) Then edit log4j.properties and include a line for your namespace

log4j.category.com.mycompany=TRACE,consoleAppender,fileAppender

3) Add import org.apache.log4j.Logger to your code

4) When logging, use

Logger.getLogger(MyClass.class).trace("Yadda Yadda");
rav
Posts: 82
Joined: 06 Dec 2010, 13:14

Postby rav » 25 Feb 2011, 07:44

Is it good practice to keep static reference on SFSExtension class? and call something like that:

MySFSExtension.getInst()._trace("aaa")
or
MySFSExtension._strace("bbb")

Code: Select all

class MySFSExtension extends SFSExtension
{
  private static MySFSExtension ext;
  public void init()
  {
        ext= this;
  }

  public static SFSExtension getInst()
  {
        return ext;
  }
  public static void _strace(String msg)
  {
     getInst()._strace("strace from MySFSExtension : " + msg);
  }
  public void _trace(String msg)
  {
     trace("trace from MySFSExtension : " + msg);
  }
}
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 25 Feb 2011, 23:22

What you have works, but it's opening a Pandora's box - especially once you start having state in that static object.

Log4j is a relatively common logger used in Java and its also what SFS is using directly under the hood whenever you call SFSExtension.trace.

You also get the added benefit of not having tangled dependencies, and the additional ability to categorize your log categories.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 88 guests