[Java] Logging

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
JohnnyD
Posts: 128
Joined: 29 Oct 2007, 22:13

[Java] Logging

Post by JohnnyD »

SF server writes things to log files, with severity level like INFO, WARN, ERROR, etc. Obviously there is functionality inside SF to do this; is this exposed so that I can use it from my Java extension? Is SF using Log4J internally or is this a custom logger the SF developers wrote?

Thanks.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Yes you can. We don't use log4j, we use the java.util.logging package
If you use Actionscript we provide a specific object for logging (check the docs)
Under Java you can access the logger as a static member like this:

Code: Select all

SmartFoxServer.log.info("Hello!")
Lapo
--
gotoAndPlay()
...addicted to flash games
JohnnyD
Posts: 128
Joined: 29 Oct 2007, 22:13

Post by JohnnyD »

I never got round to using this, but now I need to. I can't find where the logger is stored (in Java). It sounds like there is one preconfigured I can use.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Just as explained in the above code :)
log is a static member of the SmartFoxServer class.

If you want to configure differently you can check the logging.properties file in you Server/ folder

... and ... if you prefer using another logging system, just plug it into your server extension
Lapo
--
gotoAndPlay()
...addicted to flash games
JohnnyD
Posts: 128
Joined: 29 Oct 2007, 22:13

Post by JohnnyD »

Sorry but as far as I can see from your JavaDocs (http://www.smartfoxserver.com/docs/docP ... index.html) there is no SmartFoxServer class. And I can't see any SFS class with a log attribute. Am I missing something?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Yes this is correct.
We are trying to avoid exposing all the classes in the javadocs and I admit it's not an easy task :oops:
We'd like to simplify the access to the framework as much as possible avoiding dozens of classes (with relative fields and methods) that developers will never use and that would just confuse them.

In doing this a few minor things have escaped (the logger is one) but the support board is here just for this sort of issues.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Boing
Posts: 59
Joined: 11 Apr 2008, 14:57

redirect SFS logging to Log4J

Post by Boing »

Hi!
I use Log4J for logging my own stuff but I would like to find the SFS log output in the same file. Is there a way to intercept the SFS logging?
Boing
Posts: 59
Joined: 11 Apr 2008, 14:57

Post by Boing »

I just came across this possibility:

Code: Select all

SmartFoxServer.log.addHandler(new MyHandler());
I'll try that and post the result...
Boing
Posts: 59
Joined: 11 Apr 2008, 14:57

Post by Boing »

It's actually quite easy. For those who are interested I post my utility class:

Code: Select all

public class SFSLog4JLogger extends java.util.logging.Handler {

    
    private static final org.apache.log4j.Logger log =
                         org.apache.log4j.Logger.getLogger(SFSLog4JLogger.class);

    
    @Override
    public void close() throws SecurityException {}

    @Override
    public void flush() {}

    @Override
    public void publish(final java.util.logging.LogRecord record) {

        // build msg (get more info out of the record if you like to)
        String msg = record.getMessage();

        // find level
        org.apache.log4j.Level level = org.apache.log4j.Level.INFO;

        if (java.util.logging.Level.SEVERE.equals(record.getLevel())) {
            level = org.apache.log4j.Level.ERROR;
            
        } else if (java.util.logging.Level.WARNING.equals(record.getLevel())) {
            level = org.apache.log4j.Level.WARN;
            
        } else if (java.util.logging.Level.INFO.equals(record.getLevel()) ||
                   java.util.logging.Level.CONFIG.equals(record.getLevel())) {
            level = org.apache.log4j.Level.INFO;
            
        } else if (java.util.logging.Level.FINE.equals(record.getLevel()) ||
                   java.util.logging.Level.FINER.equals(record.getLevel())) {
            level = org.apache.log4j.Level.DEBUG;
            
        } else if (java.util.logging.Level.FINEST.equals(record.getLevel())) {
            level = org.apache.log4j.Level.TRACE;
        }        

        // log
        log.log(record.getSourceClassName(), level, msg, record.getThrown());
    }
    
}
Having that you just need to call

Code: Select all

SmartFoxServer.log.addHandler(new SFSLog4JLogger());
at program startup.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Interesting, thanks for posting. :)
Lapo
--
gotoAndPlay()
...addicted to flash games
ffratoni
Posts: 10
Joined: 15 Sep 2008, 13:45

async logging

Post by ffratoni »

Do you have a best practice to handle async logging other than using the Log4j Async appender thorough log4j logger?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

What is asynch logging?
Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
ffratoni
Posts: 10
Joined: 15 Sep 2008, 13:45

asynchronous logging

Post by ffratoni »

asynchronous logging that in common logging is usually handled by
AsyncAppender.
Have you any best practice to avoid "in process logging" and process blocking or using the Log4JHandler with AsyncAppender is the recommended way?
Is there a built-in way to persist events on DB(for logging needs)?
Filippo
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I see, put in simple words ... you're going to log to a potentially "slow" device (i.e. the DB) and wish to avoid blocking the current thread... right?

We don't have a best practice for this, and I've never used asynchronous logging myself, so I am not able to help here.
If Log4J helps in this regards it would probably be ok to use it.

An alternative is to add more threads to the ExtensionHandler stage of the server (see chapter 2.2). This way you don't incur into problems with "slow" operations.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
ffratoni
Posts: 10
Joined: 15 Sep 2008, 13:45

Post by ffratoni »

Exactly
Due to the amount of events to log we probably need decouple log operation
from business processes.
Thanks for the answer
Filippo
Post Reply