Mansour wrote:
Hi Everybody:
I am writing a java server project and got to the point where the program stops and hangs there with no error it just does nothing. I decided to go ahead and learn how to use log4j, to be able to see what's going on and for my future projects. However through all the documentaion I have came accros, I found no single usefull example for a beginner. One of the sockets in my program is stuck there not sending and not recieving, and I am trying to find out what's wrong without modifying the code for my project. If I have to go and modify the code, adding something similar to print statements like logger.debug and logger.info as teh examples show, then log4j is the most useless thing I came across. I am not expecting in any way to do somehting like this. Here's a piece of code that I was able to add to my main method, but again nothign useful came out.

static Logger lgr1 = Logger.getLogger("Joint");
BasicConfigurator.configure();
lgr1.setLevel(Level.DEBUG);
lgr1.info("This is from logger lgr1");
lgr1.debug("This is from logger lgr1");

Please not that, It's urgent and I can not at this point use any of the extra classes or layers. All I need is the basic information about th erunning program. Any advice ??


YODA SAYS:

Adding logging statements to your program is NEVER useless because they are a form of auto-documentation and debugging aid. The difference with System.out.println being evidently that you can switch them off if you do not need them and switch them on again if it misbehaves. Then you can push the output to files, sockets, a database, swing applications etc. etc. Any source code that you come accross that has no logging statements should immediately get a couple of demerit points. Additionally, any source code that you come accross that has no assert() statements and input verifications should get a few additional demerit points.

YODA ALSO SAYS:

It is not good to be in a hurry when you debug socket handling code.


Now, ro get the logging to work:

1) Initialize inside main() or someplace where you are sure to pass:

       //
       // add layout and appender to root category
       //
       {
           Layout layout = new TTCCLayout();
Logger.getRootLogger().addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
           Logger.getRootLogger().setLevel(Level.DEBUG);
       }

This will cause your program to log to the console, and accepts all messages down to DEBUG

2) Group your message sources into conceptual sources, aka. 'Loggers'. A good idea is to call your Loggers by the
name of the method in which they appear. For example, in you class 'Main':

public class Main {

   static final private String CLASS = Main.class.getName();
static final private Logger LOGGER_allocateWatchdogServerSocket = Logger.getLogger(CLASS + ".allocateWatchdogServerSocket");

   public static ServerSocket allocateWatchdogServerSocket() {
       Logger logger = LOGGER_allocateWatchdogServerSocket;
       logger.debug("Going to allocate now");
// BLAH BLAH
       if (initBunch.getServerPort() == 0) {
logger.info("Watchdog's port is 0 - it will be chosen randomly");
       }
        // BLAH BLAH
       }
   }






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to