f



log4j configuration and Applets

Hi,

I need to do some logging from my Applet with varying levels of verbosity
and log4j looked like the most likely, best-of-breed, widley used, option
available for the right price. (The Applet is unsigned so a Console Appender
to System.out is all I require and have available)

So I downloaded log4j-1_2_15.jar and included it with my HTML <object> and
everything looked ok, but when the Applet loaded it was now instructing the
browser to look for a log4j.xml or log4j.conf file. I thought I was
good-to-code with the default configuration options (plus runtime
configuration of the logging level [info,debug,fatal, etc]) but I was happy
to stick a minimal XML file where the browser could find it. (See below)

What I don't like about it now is it's asking for all sorts of
infrastructure bloat to be resolved and sent down the line: -

T3$APPLET_ROOT:[000000.APPLETS.META-INF.SERVICES]

JAVAX^.XML^.PARSERS.DOCUMENTBUILDERFACTORY

T3$APPLET_ROOT:[000000.APPLETS.ORG.APACHE.LOG4J]

CONSOLEAPPENDERBEANINFO.CLASS
WRITERAPPENDERBEANINFO.CLASS
APPENDERSKELETONBEANINFO.CLASS
PATTERNLAYOUTBEANINFO.CLASS
LAYOUTBEANINFO.CLASS

T3$APPLET_ROOT:[000000.APPLETS.JAVA.LANG]

OBJECTBEANINFO.CLASS

I guess I'm asking why these classes (if needed) aren't in the JAR file
already and just home much "baggage" does log4j need?

Is there a "minimalist" switch I can set in the config, or perhaps log4j is
not the most appropriate tool after all?

Cheers Richard Maher

Minimal XML file: -

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug" />
    <appender-ref ref="console" />
  </root>

</log4j:configuration>


0
maher_rj (1626)
6/7/2009 5:54:39 AM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

15 Replies
638 Views

Similar Articles

[PageSpeed] 23

Richard Maher wrote:
> I need to do some logging from my Applet with varying levels of verbosity
> and log4j looked like the most likely, best-of-breed, widley used, option
> available for the right price. (The Applet is unsigned so a Console Appender
> to System.out is all I require and have available)
> 
> So I downloaded log4j-1_2_15.jar and included it with my HTML <object> and
> everything looked ok, but when the Applet loaded it was now instructing the
> browser to look for a log4j.xml or log4j.conf file. I thought I was
> good-to-code with the default configuration options (plus runtime
> configuration of the logging level [info,debug,fatal, etc]) but I was happy
> to stick a minimal XML file where the browser could find it. (See below)
> 
> What I don't like about it now is it's asking for all sorts of
> infrastructure bloat to be resolved and sent down the line: -
> 
> T3$APPLET_ROOT:[000000.APPLETS.META-INF.SERVICES]
> 
> JAVAX^.XML^.PARSERS.DOCUMENTBUILDERFACTORY
> 
> T3$APPLET_ROOT:[000000.APPLETS.ORG.APACHE.LOG4J]
> 
> CONSOLEAPPENDERBEANINFO.CLASS
> WRITERAPPENDERBEANINFO.CLASS
> APPENDERSKELETONBEANINFO.CLASS
> PATTERNLAYOUTBEANINFO.CLASS
> LAYOUTBEANINFO.CLASS
> 
> T3$APPLET_ROOT:[000000.APPLETS.JAVA.LANG]
> 
> OBJECTBEANINFO.CLASS
> 
> I guess I'm asking why these classes (if needed) aren't in the JAR file
> already and just home much "baggage" does log4j need?
> 
> Is there a "minimalist" switch I can set in the config, or perhaps log4j is
> not the most appropriate tool after all?

I have no idea about where those classes come from. But I do
not think they are needed.

See simple example below, which works for me.

I use log4j.properties instead of log4j.xml, but that should
not matter.

Arne

========================================================

Jar content
-----------

      0 Sun Jun 07 15:48:38 EDT 2009 META-INF/
     97 Sun Jun 07 15:48:38 EDT 2009 META-INF/MANIFEST.MF
    799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$1.class
    798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$2.class
    798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$3.class
    799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$4.class
   1302 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$5.class
   1291 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$6.class
   2216 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet.class
   2947 Sun Jun 07 15:48:34 EDT 2009 test/GuiAppender.class
    487 Sun Jun 07 15:31:40 EDT 2009 log4j.properties

Manifest
--------

Class-Path: log4j-1.2.9.jar

LogApplet.java
--------------

package test;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LogApplet extends JApplet {
     private Logger log = Logger.getLogger(LogApplet.class);
     private JButton debugbtn = new JButton("Log debug");
     private JButton infobtn = new JButton("Log info");
     private JButton warnbtn = new JButton("Log warning");
     private JButton errorbtn = new JButton("Log error");
     private JButton consolebtn = new JButton("Log to console");
     private JButton guibtn = new JButton("Log to GUI");
     private boolean console = false;
     private boolean gui = false;
     public void init() {
         debugbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.debug("This is a test");
             }
         });
         infobtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.info("This is a test");
             }
         });
         warnbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.warn("This is a test");
             }
         });
         errorbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.error("This is a test");
             }
         });
         consolebtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 console = !console;
                 ConsoleAppender app = 
(ConsoleAppender)log.getAppender("console");
                 if(console) {
                     app.setThreshold(Level.DEBUG);
                 } else {
                     app.setThreshold(Level.OFF);
                 }
             }
         });
         guibtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 gui = !gui;
                 GuiAppender app = (GuiAppender)log.getAppender("gui");
                 if(gui) {
                     app.setThreshold(Level.DEBUG);
                     app.open();
                 } else {
                     app.setThreshold(Level.OFF);
                 }
             }
         });
         getContentPane().setLayout(new GridLayout(3,2 ));
         getContentPane().add(debugbtn);
         getContentPane().add(infobtn);
         getContentPane().add(warnbtn);
         getContentPane().add(errorbtn);
         getContentPane().add(consolebtn);
         getContentPane().add(guibtn);

     }
}

GuiAppender.java
----------------

package test;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

public class GuiAppender extends AppenderSkeleton {
     private JFrame f;
     private JTextPane tp;
     public GuiAppender() {
         f = new JFrame();
         f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
         //f.setAlwaysOnTop(true);
         tp = new JTextPane();
         f.getContentPane().setLayout(new BorderLayout());
         f.getContentPane().add(tp, BorderLayout.CENTER);
         f.setSize(600, 400);
     }
     private Color LevelToColor(Level lvl) {
         if(lvl.equals(Level.DEBUG)) {
             return Color.GRAY;
         } else if(lvl.equals(Level.INFO)) {
             return Color.GREEN;
         } else if(lvl.equals(Level.WARN)) {
             return Color.YELLOW;
         } else if(lvl.equals(Level.ERROR)) {
             return Color.RED;
         } else if(lvl.equals(Level.FATAL)) {
             return Color.RED;
         } else {
             return Color.BLACK;
         }

     }
     protected void append(LoggingEvent ev) {
         try {
             StyledDocument doc = tp.getStyledDocument();
             MutableAttributeSet attrs = tp.getInputAttributes();
             StyleConstants.setForeground(attrs, 
LevelToColor(ev.getLevel()));
             StyleConstants.setBackground(attrs, Color.LIGHT_GRAY);
             doc.insertString(doc.getLength(), getLayout().format(ev), 
attrs);
         } catch (BadLocationException e) {
             e.printStackTrace();
         }
     }
     public void open() {
         f.setVisible(true);
     }
     public void close() {
         f.dispose();
     }
     public boolean requiresLayout() {
         return true;
     }
}

log4j.properties
----------------

log4j.category.test.LogApplet = debug, console, gui
log4j.appender.console.threshold = off
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-30c %d %-5p %m%n
log4j.appender.gui.threshold = off
log4j.appender.gui = test.GuiAppender
log4j.appender.gui.layout = org.apache.log4j.PatternLayout
log4j.appender.gui.layout.ConversionPattern = %-30c %d %-5p %m%n
0
arne6 (9808)
6/7/2009 7:55:26 PM
Hi Arne,

"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4a2c1b2b$0$90263$14726298@news.sunsite.dk...
> Richard Maher wrote:
> > I need to do some logging from my Applet with varying levels of
verbosity
> > and log4j looked like the most likely, best-of-breed, widley used,
option
> > available for the right price. (The Applet is unsigned so a Console
Appender
> > to System.out is all I require and have available)
> >
> > So I downloaded log4j-1_2_15.jar and included it with my HTML <object>
and
> > everything looked ok, but when the Applet loaded it was now instructing
the
> > browser to look for a log4j.xml or log4j.conf file. I thought I was
> > good-to-code with the default configuration options (plus runtime
> > configuration of the logging level [info,debug,fatal, etc]) but I was
happy
> > to stick a minimal XML file where the browser could find it. (See below)
> >
> > What I don't like about it now is it's asking for all sorts of
> > infrastructure bloat to be resolved and sent down the line: -
> >
> > T3$APPLET_ROOT:[000000.APPLETS.META-INF.SERVICES]
> >
> > JAVAX^.XML^.PARSERS.DOCUMENTBUILDERFACTORY
> >
> > T3$APPLET_ROOT:[000000.APPLETS.ORG.APACHE.LOG4J]
> >
> > CONSOLEAPPENDERBEANINFO.CLASS
> > WRITERAPPENDERBEANINFO.CLASS
> > APPENDERSKELETONBEANINFO.CLASS
> > PATTERNLAYOUTBEANINFO.CLASS
> > LAYOUTBEANINFO.CLASS
> >
> > T3$APPLET_ROOT:[000000.APPLETS.JAVA.LANG]
> >
> > OBJECTBEANINFO.CLASS
> >
> > I guess I'm asking why these classes (if needed) aren't in the JAR file
> > already and just home much "baggage" does log4j need?
> >
> > Is there a "minimalist" switch I can set in the config, or perhaps log4j
is
> > not the most appropriate tool after all?
>
> I have no idea about where those classes come from. But I do
> not think they are needed.
>
> See simple example below, which works for me.
>
> I use log4j.properties instead of log4j.xml, but that should
> not matter.
>
> Arne
>
> ========================================================
>
> Jar content
> -----------
>
>       0 Sun Jun 07 15:48:38 EDT 2009 META-INF/
>      97 Sun Jun 07 15:48:38 EDT 2009 META-INF/MANIFEST.MF
>     799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$1.class
>     798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$2.class
>     798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$3.class
>     799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$4.class
>    1302 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$5.class
>    1291 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$6.class
>    2216 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet.class
>    2947 Sun Jun 07 15:48:34 EDT 2009 test/GuiAppender.class
>     487 Sun Jun 07 15:31:40 EDT 2009 log4j.properties
>
> Manifest
> --------
>
> Class-Path: log4j-1.2.9.jar
>
> LogApplet.java
> --------------
>
> package test;
>
> import java.awt.GridLayout;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
>
> import javax.swing.JApplet;
> import javax.swing.JButton;
>
> import org.apache.log4j.ConsoleAppender;
> import org.apache.log4j.Level;
> import org.apache.log4j.Logger;
>
> public class LogApplet extends JApplet {
>      private Logger log = Logger.getLogger(LogApplet.class);
>      private JButton debugbtn = new JButton("Log debug");
>      private JButton infobtn = new JButton("Log info");
>      private JButton warnbtn = new JButton("Log warning");
>      private JButton errorbtn = new JButton("Log error");
>      private JButton consolebtn = new JButton("Log to console");
>      private JButton guibtn = new JButton("Log to GUI");
>      private boolean console = false;
>      private boolean gui = false;
>      public void init() {
>          debugbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.debug("This is a test");
>              }
>          });
>          infobtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.info("This is a test");
>              }
>          });
>          warnbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.warn("This is a test");
>              }
>          });
>          errorbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.error("This is a test");
>              }
>          });
>          consolebtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  console = !console;
>                  ConsoleAppender app =
> (ConsoleAppender)log.getAppender("console");
>                  if(console) {
>                      app.setThreshold(Level.DEBUG);
>                  } else {
>                      app.setThreshold(Level.OFF);
>                  }
>              }
>          });
>          guibtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  gui = !gui;
>                  GuiAppender app = (GuiAppender)log.getAppender("gui");
>                  if(gui) {
>                      app.setThreshold(Level.DEBUG);
>                      app.open();
>                  } else {
>                      app.setThreshold(Level.OFF);
>                  }
>              }
>          });
>          getContentPane().setLayout(new GridLayout(3,2 ));
>          getContentPane().add(debugbtn);
>          getContentPane().add(infobtn);
>          getContentPane().add(warnbtn);
>          getContentPane().add(errorbtn);
>          getContentPane().add(consolebtn);
>          getContentPane().add(guibtn);
>
>      }
> }
>
> GuiAppender.java
> ----------------
>
> package test;
>
> import java.awt.BorderLayout;
> import java.awt.Color;
>
> import javax.swing.JFrame;
> import javax.swing.JTextPane;
> import javax.swing.text.BadLocationException;
> import javax.swing.text.MutableAttributeSet;
> import javax.swing.text.StyleConstants;
> import javax.swing.text.StyledDocument;
>
> import org.apache.log4j.AppenderSkeleton;
> import org.apache.log4j.Level;
> import org.apache.log4j.spi.LoggingEvent;
>
> public class GuiAppender extends AppenderSkeleton {
>      private JFrame f;
>      private JTextPane tp;
>      public GuiAppender() {
>          f = new JFrame();
>          f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
>          //f.setAlwaysOnTop(true);
>          tp = new JTextPane();
>          f.getContentPane().setLayout(new BorderLayout());
>          f.getContentPane().add(tp, BorderLayout.CENTER);
>          f.setSize(600, 400);
>      }
>      private Color LevelToColor(Level lvl) {
>          if(lvl.equals(Level.DEBUG)) {
>              return Color.GRAY;
>          } else if(lvl.equals(Level.INFO)) {
>              return Color.GREEN;
>          } else if(lvl.equals(Level.WARN)) {
>              return Color.YELLOW;
>          } else if(lvl.equals(Level.ERROR)) {
>              return Color.RED;
>          } else if(lvl.equals(Level.FATAL)) {
>              return Color.RED;
>          } else {
>              return Color.BLACK;
>          }
>
>      }
>      protected void append(LoggingEvent ev) {
>          try {
>              StyledDocument doc = tp.getStyledDocument();
>              MutableAttributeSet attrs = tp.getInputAttributes();
>              StyleConstants.setForeground(attrs,
> LevelToColor(ev.getLevel()));
>              StyleConstants.setBackground(attrs, Color.LIGHT_GRAY);
>              doc.insertString(doc.getLength(), getLayout().format(ev),
> attrs);
>          } catch (BadLocationException e) {
>              e.printStackTrace();
>          }
>      }
>      public void open() {
>          f.setVisible(true);
>      }
>      public void close() {
>          f.dispose();
>      }
>      public boolean requiresLayout() {
>          return true;
>      }
> }
>
> log4j.properties
> ----------------
>
> log4j.category.test.LogApplet = debug, console, gui
> log4j.appender.console.threshold = off
> log4j.appender.console = org.apache.log4j.ConsoleAppender
> log4j.appender.console.layout = org.apache.log4j.PatternLayout
> log4j.appender.console.layout.ConversionPattern = %-30c %d %-5p %m%n
> log4j.appender.gui.threshold = off
> log4j.appender.gui = test.GuiAppender
> log4j.appender.gui.layout = org.apache.log4j.PatternLayout
> log4j.appender.gui.layout.ConversionPattern = %-30c %d %-5p %m%n

Thanks for the reply.

I'm guessing/hoping that the problem is that I haven't included the
log4j.properties file in the Applet JAR (as it looks like you have done) I
just stuck it in the codebase.

I'll give it a go now and if I don't report back then all's ok, thanks.

Cheers Richard Maher

PS. If anyone can tell me, given that I have a package "tier3Client" that
uses log4j, should the properties file be at the top/root level in the JAR
or under the package tier3Client/log4j.properties then that would save me
time and be welcomed.


0
maher_rj (1626)
6/8/2009 11:17:53 AM
Hi (again) Arne,

"Richard Maher" <maher_rj@hotspamnotmail.com> wrote in message
news:h0iru1$drh$1@news-01.bur.connect.com.au...
> Hi Arne,
>
> "Arne Vajh�j" <arne@vajhoej.dk> wrote in message
> news:4a2c1b2b$0$90263$14726298@news.sunsite.dk...
> > Richard Maher wrote:
> > > I need to do some logging from my Applet with varying levels of
> verbosity
> > > and log4j looked like the most likely, best-of-breed, widley used,
> option
> > > available for the right price. (The Applet is unsigned so a Console
> Appender
> > > to System.out is all I require and have available)
> > >
> > > So I downloaded log4j-1_2_15.jar and included it with my HTML <object>
> and
> > > everything looked ok, but when the Applet loaded it was now
instructing
> the
> > > browser to look for a log4j.xml or log4j.conf file. I thought I was
> > > good-to-code with the default configuration options (plus runtime
> > > configuration of the logging level [info,debug,fatal, etc]) but I was
> happy
> > > to stick a minimal XML file where the browser could find it. (See
below)
> > >
> > > What I don't like about it now is it's asking for all sorts of
> > > infrastructure bloat to be resolved and sent down the line: -
> > >
> > > T3$APPLET_ROOT:[000000.APPLETS.META-INF.SERVICES]
> > >
> > > JAVAX^.XML^.PARSERS.DOCUMENTBUILDERFACTORY
> > >
> > > T3$APPLET_ROOT:[000000.APPLETS.ORG.APACHE.LOG4J]
> > >
> > > CONSOLEAPPENDERBEANINFO.CLASS
> > > WRITERAPPENDERBEANINFO.CLASS
> > > APPENDERSKELETONBEANINFO.CLASS
> > > PATTERNLAYOUTBEANINFO.CLASS
> > > LAYOUTBEANINFO.CLASS
> > >
> > > T3$APPLET_ROOT:[000000.APPLETS.JAVA.LANG]
> > >
> > > OBJECTBEANINFO.CLASS
> > >
> > > I guess I'm asking why these classes (if needed) aren't in the JAR
file
> > > already and just home much "baggage" does log4j need?
> > >
> > > Is there a "minimalist" switch I can set in the config, or perhaps
log4j
> is
> > > not the most appropriate tool after all?
> >
> > I have no idea about where those classes come from. But I do
> > not think they are needed.
> >
> > See simple example below, which works for me.
> >
> > I use log4j.properties instead of log4j.xml, but that should
> > not matter.
> >
> > Arne
> >
> > ========================================================
> >
> > Jar content
> > -----------
> >
> >       0 Sun Jun 07 15:48:38 EDT 2009 META-INF/
> >      97 Sun Jun 07 15:48:38 EDT 2009 META-INF/MANIFEST.MF
> >     799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$1.class
> >     798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$2.class
> >     798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$3.class
> >     799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$4.class
> >    1302 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$5.class
> >    1291 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$6.class
> >    2216 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet.class
> >    2947 Sun Jun 07 15:48:34 EDT 2009 test/GuiAppender.class
> >     487 Sun Jun 07 15:31:40 EDT 2009 log4j.properties
> >
> > Manifest
> > --------
> >
> > Class-Path: log4j-1.2.9.jar
> >
> > LogApplet.java
> > --------------
> >
> > package test;
> >
> > import java.awt.GridLayout;
> > import java.awt.event.ActionEvent;
> > import java.awt.event.ActionListener;
> >
> > import javax.swing.JApplet;
> > import javax.swing.JButton;
> >
> > import org.apache.log4j.ConsoleAppender;
> > import org.apache.log4j.Level;
> > import org.apache.log4j.Logger;
> >
> > public class LogApplet extends JApplet {
> >      private Logger log = Logger.getLogger(LogApplet.class);
> >      private JButton debugbtn = new JButton("Log debug");
> >      private JButton infobtn = new JButton("Log info");
> >      private JButton warnbtn = new JButton("Log warning");
> >      private JButton errorbtn = new JButton("Log error");
> >      private JButton consolebtn = new JButton("Log to console");
> >      private JButton guibtn = new JButton("Log to GUI");
> >      private boolean console = false;
> >      private boolean gui = false;
> >      public void init() {
> >          debugbtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  log.debug("This is a test");
> >              }
> >          });
> >          infobtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  log.info("This is a test");
> >              }
> >          });
> >          warnbtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  log.warn("This is a test");
> >              }
> >          });
> >          errorbtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  log.error("This is a test");
> >              }
> >          });
> >          consolebtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  console = !console;
> >                  ConsoleAppender app =
> > (ConsoleAppender)log.getAppender("console");
> >                  if(console) {
> >                      app.setThreshold(Level.DEBUG);
> >                  } else {
> >                      app.setThreshold(Level.OFF);
> >                  }
> >              }
> >          });
> >          guibtn.addActionListener(new ActionListener() {
> >              public void actionPerformed(ActionEvent e) {
> >                  gui = !gui;
> >                  GuiAppender app = (GuiAppender)log.getAppender("gui");
> >                  if(gui) {
> >                      app.setThreshold(Level.DEBUG);
> >                      app.open();
> >                  } else {
> >                      app.setThreshold(Level.OFF);
> >                  }
> >              }
> >          });
> >          getContentPane().setLayout(new GridLayout(3,2 ));
> >          getContentPane().add(debugbtn);
> >          getContentPane().add(infobtn);
> >          getContentPane().add(warnbtn);
> >          getContentPane().add(errorbtn);
> >          getContentPane().add(consolebtn);
> >          getContentPane().add(guibtn);
> >
> >      }
> > }
> >
> > GuiAppender.java
> > ----------------
> >
> > package test;
> >
> > import java.awt.BorderLayout;
> > import java.awt.Color;
> >
> > import javax.swing.JFrame;
> > import javax.swing.JTextPane;
> > import javax.swing.text.BadLocationException;
> > import javax.swing.text.MutableAttributeSet;
> > import javax.swing.text.StyleConstants;
> > import javax.swing.text.StyledDocument;
> >
> > import org.apache.log4j.AppenderSkeleton;
> > import org.apache.log4j.Level;
> > import org.apache.log4j.spi.LoggingEvent;
> >
> > public class GuiAppender extends AppenderSkeleton {
> >      private JFrame f;
> >      private JTextPane tp;
> >      public GuiAppender() {
> >          f = new JFrame();
> >          f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
> >          //f.setAlwaysOnTop(true);
> >          tp = new JTextPane();
> >          f.getContentPane().setLayout(new BorderLayout());
> >          f.getContentPane().add(tp, BorderLayout.CENTER);
> >          f.setSize(600, 400);
> >      }
> >      private Color LevelToColor(Level lvl) {
> >          if(lvl.equals(Level.DEBUG)) {
> >              return Color.GRAY;
> >          } else if(lvl.equals(Level.INFO)) {
> >              return Color.GREEN;
> >          } else if(lvl.equals(Level.WARN)) {
> >              return Color.YELLOW;
> >          } else if(lvl.equals(Level.ERROR)) {
> >              return Color.RED;
> >          } else if(lvl.equals(Level.FATAL)) {
> >              return Color.RED;
> >          } else {
> >              return Color.BLACK;
> >          }
> >
> >      }
> >      protected void append(LoggingEvent ev) {
> >          try {
> >              StyledDocument doc = tp.getStyledDocument();
> >              MutableAttributeSet attrs = tp.getInputAttributes();
> >              StyleConstants.setForeground(attrs,
> > LevelToColor(ev.getLevel()));
> >              StyleConstants.setBackground(attrs, Color.LIGHT_GRAY);
> >              doc.insertString(doc.getLength(), getLayout().format(ev),
> > attrs);
> >          } catch (BadLocationException e) {
> >              e.printStackTrace();
> >          }
> >      }
> >      public void open() {
> >          f.setVisible(true);
> >      }
> >      public void close() {
> >          f.dispose();
> >      }
> >      public boolean requiresLayout() {
> >          return true;
> >      }
> > }
> >
> > log4j.properties
> > ----------------
> >
> > log4j.category.test.LogApplet = debug, console, gui
> > log4j.appender.console.threshold = off
> > log4j.appender.console = org.apache.log4j.ConsoleAppender
> > log4j.appender.console.layout = org.apache.log4j.PatternLayout
> > log4j.appender.console.layout.ConversionPattern = %-30c %d %-5p %m%n
> > log4j.appender.gui.threshold = off
> > log4j.appender.gui = test.GuiAppender
> > log4j.appender.gui.layout = org.apache.log4j.PatternLayout
> > log4j.appender.gui.layout.ConversionPattern = %-30c %d %-5p %m%n
>
> Thanks for the reply.
>
> I'm guessing/hoping that the problem is that I haven't included the
> log4j.properties file in the Applet JAR (as it looks like you have done) I
> just stuck it in the codebase.
>
> I'll give it a go now and if I don't report back then all's ok, thanks.
>
> Cheers Richard Maher
>
> PS. If anyone can tell me, given that I have a package "tier3Client" that
> uses log4j, should the properties file be at the top/root level in the JAR
> or under the package tier3Client/log4j.properties then that would save me
> time and be welcomed.
>

Still no good I'm afraid :-(

When you say "Works for me" is it just that the logger functionality works
regardless of whatever bollocks overhead was incurred at startup?

Sorry if I can't help being inflammatory but can you please confirm that you
(anyone?) are running log4j-1_2_15.jar and that your Apache (whatever) error
logger settings are such that HTTP ("GET" for the beaninfo stuff and "HEAD"
for the log4j stuff) failures are recorded and that there is nothing in the
error log for this stuff?

I've tried Windows IE6 and FF2.

I'm just trying to establish if I am incurring unnecessary overhead due to
something stupid that I am doing or if everyone else thinks crap like this
is acceptable.

Cheers Richard Maher
>


0
maher_rj (1626)
6/8/2009 2:18:26 PM
Was there a particular reason that the javax.util.logging
API does not fulfil the needs of this applet?

--
Andrew T.
pscode.org
0
andrewthommo (3108)
6/8/2009 5:42:30 PM
Hi Andrew,

Thanks for the reply.

"Andrew Thompson" <andrewthommo@gmail.com> wrote in message
news:61008c89-45b9-430e-b9a9-c98b8d4ddb35@h11g2000yqb.googlegroups.com...
> Was there a particular reason that the javax.util.logging
> API does not fulfil the needs of this applet?
>

Dunno. Maybe? I'll have a look, thanks for the tip.

As someone who's used log4j before with web-services and liked the circular
logging stuff, I'd never really questioned how big it's footprint was. I am
also wondering whether I was sheepishly following the crowd/fashion or
whether log4j had in fact become a defacto standard?

> --
> Andrew T.
> pscode.org

Cheers Richard Maher


0
maher_rj (1626)
6/8/2009 10:59:32 PM
Richard Maher wrote:
> I'm guessing/hoping that the problem is that I haven't included the
> log4j.properties file in the Applet JAR (as it looks like you have done) I
> just stuck it in the codebase.

I know that it works in the jar file. I don't know if it works
outside. It should be in the jar file.

> PS. If anyone can tell me, given that I have a package "tier3Client" that
> uses log4j, should the properties file be at the top/root level in the JAR
> or under the package tier3Client/log4j.properties then that would save me
> time and be welcomed.

Top/root level to be used as default.

Arne
0
arne6 (9808)
6/9/2009 12:22:06 AM
Richard Maher wrote:
> When you say "Works for me" is it just that the logger functionality works
> regardless of whatever bollocks overhead was incurred at startup?
> 
> Sorry if I can't help being inflammatory but can you please confirm that you
> (anyone?) are running log4j-1_2_15.jar and that your Apache (whatever) error
> logger settings are such that HTTP ("GET" for the beaninfo stuff and "HEAD"
> for the log4j stuff) failures are recorded and that there is nothing in the
> error log for this stuff?
> 
> I've tried Windows IE6 and FF2.
> 
> I'm just trying to establish if I am incurring unnecessary overhead due to
> something stupid that I am doing or if everyone else thinks crap like this
> is acceptable.

You mean whether I get:

127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/PatternLayoutBeanInfo.class HTTP/1.1" 404 340
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/PatternLayoutBeanInfo.class HTTP/1.1" 404 340
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/LayoutBeanInfo.class HTTP/1.1" 404 333
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/LayoutBeanInfo.class HTTP/1.1" 404 333
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/java/lang/ObjectBeanInfo.class HTTP/1.1" 404 326
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/ConsoleAppenderBeanInfo.class HTTP/1.1" 404 342
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/ConsoleAppenderBeanInfo.class HTTP/1.1" 404 342
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/WriterAppenderBeanInfo.class HTTP/1.1" 404 341
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/WriterAppenderBeanInfo.class HTTP/1.1" 404 341
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/AppenderSkeletonBeanInfo.class HTTP/1.1" 404 343
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/org/apache/log4j/AppenderSkeletonBeanInfo.class HTTP/1.1" 404 343
127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET 
/test/GuiAppenderBeanInfo.class HTTP/1.1" 503 421
127.0.0.1 - - [08/Jun/2009:20:15:28 -0400] "GET 
/test/GuiAppenderBeanInfo.class HTTP/1.1" 503 421

?

Yes - I do get those.

I don't think you can avoid that.

If you sign the applet, then you can call:

Introspector.setBeanInfoSearchPath(new String[0]);

to avoid it.

Arne
0
arne6 (9808)
6/9/2009 12:24:47 AM
Andrew Thompson wrote:
> Was there a particular reason that the javax.util.logging
> API does not fulfil the needs of this applet?

Log4j is both more flexible and faster than jul, but it
is a good questions, because most likely the difference
does not mean much for usage in applets.

Arne
0
arne6 (9808)
6/9/2009 12:25:53 AM
Richard Maher wrote:
> "Andrew Thompson" <andrewthommo@gmail.com> wrote in message
> news:61008c89-45b9-430e-b9a9-c98b8d4ddb35@h11g2000yqb.googlegroups.com...
>> Was there a particular reason that the javax.util.logging
>> API does not fulfil the needs of this applet?
> 
> Dunno. Maybe? I'll have a look, thanks for the tip.
> 
> As someone who's used log4j before with web-services and liked the circular
> logging stuff, I'd never really questioned how big it's footprint was. I am
> also wondering whether I was sheepishly following the crowd/fashion or
> whether log4j had in fact become a defacto standard?

Log4j is much more widely used than jul for server side stuff.

I am not so sure for desktop app or applet.

Log4j is only a few hundred KB, which is not much after
todays standard.

And after it is loaded then it is faster than jul. Even though
you can most likely not measure that for an applet.

Arne


0
arne6 (9808)
6/9/2009 12:29:05 AM
Hi Arne,

"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4a2dabc8$0$90263$14726298@news.sunsite.dk...
>
> You mean whether I get:
>
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/PatternLayoutBeanInfo.class HTTP/1.1" 404 340
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/PatternLayoutBeanInfo.class HTTP/1.1" 404 340
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/LayoutBeanInfo.class HTTP/1.1" 404 333
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/LayoutBeanInfo.class HTTP/1.1" 404 333
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /java/lang/ObjectBeanInfo.class HTTP/1.1" 404 326
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/ConsoleAppenderBeanInfo.class HTTP/1.1" 404 342
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/ConsoleAppenderBeanInfo.class HTTP/1.1" 404 342
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/WriterAppenderBeanInfo.class HTTP/1.1" 404 341
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/WriterAppenderBeanInfo.class HTTP/1.1" 404 341
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/AppenderSkeletonBeanInfo.class HTTP/1.1" 404 343
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /org/apache/log4j/AppenderSkeletonBeanInfo.class HTTP/1.1" 404 343
> 127.0.0.1 - - [08/Jun/2009:20:15:27 -0400] "GET
> /test/GuiAppenderBeanInfo.class HTTP/1.1" 503 421
> 127.0.0.1 - - [08/Jun/2009:20:15:28 -0400] "GET
> /test/GuiAppenderBeanInfo.class HTTP/1.1" 503 421
>
> ?
>
> Yes - I do get those.

I really appreciate you trying that out for me!

>
> I don't think you can avoid that.

No worries. The executive decision is that log4j is just too much bloatware
for an Applet, where Initialization-Time is more important than shaving a
few nanoseconds of a debug-message.

>
> If you sign the applet, then you can call:
>
> Introspector.setBeanInfoSearchPath(new String[0]);
>
> to avoid it.

Nah, it's all too hard (at least for an Applet).

IMO, Log4j has assumed a life of its own and is wandering around with
stitches everywhere and bolts hanging out of its neck :-)

>
> Arne

Thanks again.

Cheers Richard Maher


0
maher_rj (1626)
6/9/2009 10:45:42 AM
"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4a2dab28$0$90263$14726298@news.sunsite.dk...
> Richard Maher wrote:
> > I'm guessing/hoping that the problem is that I haven't included the
> > log4j.properties file in the Applet JAR (as it looks like you have done)
I
> > just stuck it in the codebase.
>
> I know that it works in the jar file. I don't know if it works
> outside. It should be in the jar file.

Ok,

>
> > PS. If anyone can tell me, given that I have a package "tier3Client"
that
> > uses log4j, should the properties file be at the top/root level in the
JAR
> > or under the package tier3Client/log4j.properties then that would save
me
> > time and be welcomed.
>
> Top/root level to be used as default.

I tried all combos, but I'll remember for next time.

>
> Arne

Thanks again.

Cheers Richard Maher


0
maher_rj (1626)
6/9/2009 10:47:46 AM
Hi (again) Andrew,

"Andrew Thompson" <andrewthommo@gmail.com> wrote in message
news:61008c89-45b9-430e-b9a9-c98b8d4ddb35@h11g2000yqb.googlegroups.com...
> Was there a particular reason that the javax.util.logging
> API does not fulfil the needs of this applet?
>
Might be taking the proverbial when it comes to doing my own googling, but
if someone has a "nice little" tutorial example of a console logger with
java.util.logging then please share it.

> --
> Andrew T.
> pscode.org

Cheers Richard Maher


0
maher_rj (1626)
6/9/2009 10:50:18 AM
Richard Maher wrote:
> "Andrew Thompson" <andrewthommo@gmail.com> wrote in message
> news:61008c89-45b9-430e-b9a9-c98b8d4ddb35@h11g2000yqb.googlegroups.com...
>> Was there a particular reason that the javax.util.logging
>> API does not fulfil the needs of this applet?
>>
> Might be taking the proverbial when it comes to doing my own googling, but
> if someone has a "nice little" tutorial example of a console logger with
> java.util.logging then please share it.

Just get the anonymous logger, log to that and don't attempt
to change the config (if you do that then you will get in a fight
with the security manager).

See code below.

Arne

=====================================

package test2;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class LogApplet extends JApplet {
     private Logger log = Logger.getAnonymousLogger();
     private JButton debugbtn = new JButton("Log debug");
     private JButton infobtn = new JButton("Log info");
     private JButton warnbtn = new JButton("Log warning");
     private JButton errorbtn = new JButton("Log error");
     private JButton consolebtn = new JButton("Log to console");
     private JButton guibtn = new JButton("Log to GUI");
     private boolean console = false;
     private boolean gui = false;
     public void init() {
         log.setLevel(Level.OFF);
         debugbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 JOptionPane.showMessageDialog(null, "No debug level 
loggin possible");
             }
         });
         infobtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.info("This is a test");
             }
         });
         warnbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.warning("This is a test");
             }
         });
         errorbtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 log.severe("This is a test");
             }
         });
         consolebtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 console = !console;
                 if(console) {
                     log.setLevel(Level.FINE);
                 } else {
                     log.setLevel(Level.OFF);
                 }
             }
         });
         guibtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 gui = !gui;
                 JOptionPane.showMessageDialog(null, "No fancy GUI 
handler here");
             }
         });
         getContentPane().setLayout(new GridLayout(3,2 ));
         getContentPane().add(debugbtn);
         getContentPane().add(infobtn);
         getContentPane().add(warnbtn);
         getContentPane().add(errorbtn);
         getContentPane().add(consolebtn);
         getContentPane().add(guibtn);

     }
}
0
arne6 (9808)
6/10/2009 12:03:32 AM
Richard Maher wrote:
>> I don't think you can avoid that.
> 
> No worries. The executive decision is that log4j is just too much bloatware
> for an Applet, where Initialization-Time is more important than shaving a
> few nanoseconds of a debug-message.

If the problem is the time of the HTTP requests, then you could
put dummy classes in the jar file.

Arne

0
arne6 (9808)
6/10/2009 12:05:57 AM
"Arne Vajh�j" <arne@vajhoej.dk> wrote in message
news:4a2ef84b$0$90263$14726298@news.sunsite.dk...
> Richard Maher wrote:
> > "Andrew Thompson" <andrewthommo@gmail.com> wrote in message
> >
news:61008c89-45b9-430e-b9a9-c98b8d4ddb35@h11g2000yqb.googlegroups.com...
> >> Was there a particular reason that the javax.util.logging
> >> API does not fulfil the needs of this applet?
> >>
> > Might be taking the proverbial when it comes to doing my own googling,
but
> > if someone has a "nice little" tutorial example of a console logger with
> > java.util.logging then please share it.
>
> Just get the anonymous logger, log to that and don't attempt
> to change the config (if you do that then you will get in a fight
> with the security manager).
>
> See code below.
>
> Arne
>
> =====================================
>
> package test2;
>
> import java.awt.GridLayout;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import java.util.logging.Level;
> import java.util.logging.Logger;
>
> import javax.swing.JApplet;
> import javax.swing.JButton;
> import javax.swing.JOptionPane;
>
> public class LogApplet extends JApplet {
>      private Logger log = Logger.getAnonymousLogger();
>      private JButton debugbtn = new JButton("Log debug");
>      private JButton infobtn = new JButton("Log info");
>      private JButton warnbtn = new JButton("Log warning");
>      private JButton errorbtn = new JButton("Log error");
>      private JButton consolebtn = new JButton("Log to console");
>      private JButton guibtn = new JButton("Log to GUI");
>      private boolean console = false;
>      private boolean gui = false;
>      public void init() {
>          log.setLevel(Level.OFF);
>          debugbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  JOptionPane.showMessageDialog(null, "No debug level
> loggin possible");
>              }
>          });
>          infobtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.info("This is a test");
>              }
>          });
>          warnbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.warning("This is a test");
>              }
>          });
>          errorbtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  log.severe("This is a test");
>              }
>          });
>          consolebtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  console = !console;
>                  if(console) {
>                      log.setLevel(Level.FINE);
>                  } else {
>                      log.setLevel(Level.OFF);
>                  }
>              }
>          });
>          guibtn.addActionListener(new ActionListener() {
>              public void actionPerformed(ActionEvent e) {
>                  gui = !gui;
>                  JOptionPane.showMessageDialog(null, "No fancy GUI
> handler here");
>              }
>          });
>          getContentPane().setLayout(new GridLayout(3,2 ));
>          getContentPane().add(debugbtn);
>          getContentPane().add(infobtn);
>          getContentPane().add(warnbtn);
>          getContentPane().add(errorbtn);
>          getContentPane().add(consolebtn);
>          getContentPane().add(guibtn);
>
>      }
> }

Thanks very much.

Cheers Richard Maher


0
maher_rj (1626)
6/10/2009 10:44:23 AM
Reply: