Page 1 of 1
[Java] Logging
Posted: 17 Nov 2007, 11:06
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.
Posted: 20 Nov 2007, 13:57
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:
Posted: 02 May 2008, 15:58
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.
Posted: 03 May 2008, 16:15
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
Posted: 03 May 2008, 20:05
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?
Posted: 04 May 2008, 06:03
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
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
redirect SFS logging to Log4J
Posted: 30 Jul 2008, 13:59
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?
Posted: 30 Jul 2008, 14:11
by Boing
I just came across this possibility:
Code: Select all
SmartFoxServer.log.addHandler(new MyHandler());
I'll try that and post the result...
Posted: 30 Jul 2008, 15:24
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.
Posted: 30 Jul 2008, 19:46
by Lapo
Interesting, thanks for posting.

async logging
Posted: 15 Sep 2008, 15:16
by ffratoni
Do you have a best practice to handle async logging other than using the Log4j Async appender thorough log4j logger?
Posted: 15 Sep 2008, 15:54
by Lapo
What is asynch logging?
Thanks
asynchronous logging
Posted: 16 Sep 2008, 09:28
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
Posted: 16 Sep 2008, 11:33
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
Posted: 16 Sep 2008, 14:34
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