You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Raúl Santiago <mi...@virtualsw.com> on 2006/12/28 18:16:33 UTC

log per thread questions

Hello. I think this question has been asked before, but not exactly with 
the same intentions.
I have extended ConsoleAppender to log to a ScrollArea. This way:

public class taAppender extends ConsoleAppender{
        private JTextArea taLog=new JTextArea();
        public JScrollPane ScrollTA= new JScrollPane(taLog, 
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        private JScrollBar vbar = ScrollTA.getVerticalScrollBar();
   
        public taAppender(){
            System.out.println("constructor taAppender");
        }

        protected void subAppend(LoggingEvent event){
            taLog.append(getLayout().format(event));
            taLog.setCaretPosition( taLog.getDocument().getLength() );
        }
}

The JScrollPane is public so it can referenced to add it to a JPane.
Everything worked smoothly... until my boss decided "to multithread" the 
process.

So now the app creates several threads on startup, and I want a 
taAppender per thread, every of them with it own ScrollTA, added to the 
main app JTabbedPane.

The problem is that the taAppender is only instantiated once, and 
everytime I reference its scrollTA it is the one created the very first 
time.

So my question is: How can I get a new instance of my appender, in a 
different logger per thread (to get as many loggers as threads) and be 
able to add these to main app panel?
bear in mind that besides this thread independent taAppender, I will 
probably need a filelog per thread and a console shared log, too.

I've been reading some clues about using ThreadLocal and/or 
LoggerFactory, but I have not been able to find any good example on the 
subject.

Any advice will be greatly appreciated.
Thanks in advance.
 

-- 

Un Saludo,
Raúl Santiago Gómez
Virtual Software S.L.
mijac@virtualsw.com

"El programador crea aplicaciones a prueba de tontos; Dios crea tontos a prueba de programadores"


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: log per thread questions

Posted by Jacob Kjome <ho...@visi.com>.
You could try using a custom repository selector 
that is keyed by thread.  Log4j-1.3 has one keyed 
by JNDI.  Others out there are keyed by 
ClassLoader, but that just leads to classloading 
issues.  Look up in your favorite search engine 
or in the Log4j Wiki.  The Log4j -sandbox used to 
have one that used JNDI  and was compatible with 
Log4j 1.2, but since an equivalent one was added 
to Log4j 1.3 (albeit incompatible with Log4j 
1.2), it was removed.  It was tagged as something 
like 0.[1|2|3|4]alpha, so you might find it by 
looking at that tags in SVN if the conversion from CVS to SVN saved the tags.

Jake

At 11:16 AM 12/28/2006, you wrote:
 >Hello. I think this question has been asked before, but not exactly with
 >the same intentions.
 >I have extended ConsoleAppender to log to a ScrollArea. This way:
 >
 >public class taAppender extends ConsoleAppender{
 >        private JTextArea taLog=new JTextArea();
 >        public JScrollPane ScrollTA= new JScrollPane(taLog,
 >JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
 >JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 >        private JScrollBar vbar = ScrollTA.getVerticalScrollBar();
 >
 >        public taAppender(){
 >            System.out.println("constructor taAppender");
 >        }
 >
 >        protected void subAppend(LoggingEvent event){
 >            taLog.append(getLayout().format(event));
 >            taLog.setCaretPosition( taLog.getDocument().getLength() );
 >        }
 >}
 >
 >The JScrollPane is public so it can referenced to add it to a JPane.
 >Everything worked smoothly... until my boss decided "to multithread" the
 >process.
 >
 >So now the app creates several threads on startup, and I want a
 >taAppender per thread, every of them with it own ScrollTA, added to the
 >main app JTabbedPane.
 >
 >The problem is that the taAppender is only instantiated once, and
 >everytime I reference its scrollTA it is the one created the very first
 >time.
 >
 >So my question is: How can I get a new instance of my appender, in a
 >different logger per thread (to get as many loggers as threads) and be
 >able to add these to main app panel?
 >bear in mind that besides this thread independent taAppender, I will
 >probably need a filelog per thread and a console shared log, too.
 >
 >I've been reading some clues about using ThreadLocal and/or
 >LoggerFactory, but I have not been able to find any good example on the
 >subject.
 >
 >Any advice will be greatly appreciated.
 >Thanks in advance.
 >
 >
 >--
 >
 >Un Saludo,
 >Raúl Santiago Gómez
 >Virtual Software S.L.
 >mijac@virtualsw.com
 >
 >"El programador crea aplicaciones a prueba de tontos; Dios crea tontos
 >a prueba de programadores"
 >
 >
 >---------------------------------------------------------------------
 >To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
 >For additional commands, e-mail: log4j-user-help@logging.apache.org
 >
 >
 > 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: log per thread questions

Posted by James Stauffer <st...@gmail.com>.
You could probably also stay with one appender but use a ThreadLocal
to get the ScrollPane in taAppender.  You would probably need to make
the ThreadLocal hold a wrapper classes that holds your 3 variables.

On 12/28/06, Raúl Santiago <mi...@virtualsw.com> wrote:
> Hello. I think this question has been asked before, but not exactly with
> the same intentions.
> I have extended ConsoleAppender to log to a ScrollArea. This way:
>
> public class taAppender extends ConsoleAppender{
>         private JTextArea taLog=new JTextArea();
>         public JScrollPane ScrollTA= new JScrollPane(taLog,
> JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
> JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
>         private JScrollBar vbar = ScrollTA.getVerticalScrollBar();
>
>         public taAppender(){
>             System.out.println("constructor taAppender");
>         }
>
>         protected void subAppend(LoggingEvent event){
>             taLog.append(getLayout().format(event));
>             taLog.setCaretPosition( taLog.getDocument().getLength() );
>         }
> }
>
> The JScrollPane is public so it can referenced to add it to a JPane.
> Everything worked smoothly... until my boss decided "to multithread" the
> process.
>
> So now the app creates several threads on startup, and I want a
> taAppender per thread, every of them with it own ScrollTA, added to the
> main app JTabbedPane.
>
> The problem is that the taAppender is only instantiated once, and
> everytime I reference its scrollTA it is the one created the very first
> time.
>
> So my question is: How can I get a new instance of my appender, in a
> different logger per thread (to get as many loggers as threads) and be
> able to add these to main app panel?
> bear in mind that besides this thread independent taAppender, I will
> probably need a filelog per thread and a console shared log, too.
>
> I've been reading some clues about using ThreadLocal and/or
> LoggerFactory, but I have not been able to find any good example on the
> subject.
>
> Any advice will be greatly appreciated.
> Thanks in advance.
>
>
> --
>
> Un Saludo,
> Raúl Santiago Gómez
> Virtual Software S.L.
> mijac@virtualsw.com
>
> "El programador crea aplicaciones a prueba de tontos; Dios crea tontos a prueba de programadores"
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org