Hey guys,

Got my logger working as i wanted it to.. basically it just logs to a file using java.utils.logger

anyway now got a problem.

I'm wanting to duplicate the logging output to a JTextArea on my GUI. So I thought of building a wrapper class but to my suprise it didn't work as expected. It kinda half works and I'm wondering if theres a better way:


here's the code so far...

main...

public class Main {
    
   // Logger log = Logger.getLogger (Main.class.toString ());
    
    public static void main (String[] args) {
        logWrapper logs = logWrapper.getInstance ();
        
        logs.logThis (Main.class.toString (), "hello world");  
        
        tester te=new tester();
    }
    
    
}

LogWrapper

public class logWrapper {
    
    //Create the one and only logger
    static logWrapper instance;
    
    Logger diagnosticLog = Logger.getLogger ("My logger");
    
    /** Creates a new instance of logWrapper */
    private logWrapper () {
    }
    
    static logWrapper getInstance () {
        if(instance == null) {
            instance = new logWrapper ();
        }
        
        return instance;
    }
    
    public void logThis ( String className, String msg) {
        diagnosticLog.log(Level.INFO, className +  " " + msg );
    }
        
    
}

tester

public class tester {
    
    logWrapper logs;
    /** Creates a new instance of tester */
    public tester () {
        logs = logWrapper.getInstance ();
        
        logs.logThis (tester.class.toString (), "making an instance");
    }
    
    
    
}

I get the following output which is kinda half right half wrong...

08-Dec-2007 20:39:01 codepadv2.logWrapper logThis
INFO: class codepadv2.Main hello world
08-Dec-2007 20:39:01 codepadv2.logWrapper logThis
INFO: class codepadv2.tester making an instance

if you've noticed that the first line and the thrid line contains the same information whilst they are been called from different functions. I kinda need these lines replaced with the actual function name called....

Any Ideas? I've hoped i've explained it as best as possible....

Recommended Answers

All 3 Replies

Ok update... I noticed the Logger class as a addHandler() method on it... I was wondering if I could somehow register a listener on this?

That way when something gets spooled into the text file... the listener will also be notified and hence i will get a copy of the message....

Anyone ever had experince with this?

That's probably the way to go.
Add a Handler that spools to some memory structure (maybe a List) and add a Listener to that memory structure that notifies the Swing control it needs updating.

How would I implement such a thing?

I;ve made a new class that extends Handler but i've got to implement 3 new abstract methods:

public void publish (LogRecord record) {
    }

    public void flush () {
    }

    public void close () throws SecurityException {
    }

Not quite sure what goes in here? I guess this ain't the right way of doing this is it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.