• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2345
  • Last Modified:

How do I use log4j.properties for applet?


Hi,

Here's my setup:

I have an applet that needs to send logging output to the java console. I have the applet jar and the log4j-1.2.8.jar on a Tomcat server and the reference to the log4j-1.2.8.jar is in the applet jar manifest.mf.

I need to be able to configure the logging via a log4j.properties file. Where do I keep this file, so that it will be picked up by the log4j automatically?

See below for my properties file, in case it has errors.

Thanks.

----

log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender which outputs to System.out.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

----
0
rakhare
Asked:
rakhare
1 Solution
 
CEHJCommented:
Put it in the applet codebase. Make sure log4j.jar is in the archive tag
0
 
rakhareAuthor Commented:

Ok, do you mean this?
            codebase="log4j.properties"
            archive="AppCMS.jar, log4j-1.2.8.jar"
            width="950" height="500">

That's throwing errors:

java.io.FileNotFoundException: http://localhost:8084/cms/log4j.properties/AppCMS.jar

      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:713)

      at sun.plugin.net.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:407)

      at sun.plugin.net.protocol.http.HttpUtils.followRedirects(HttpUtils.java:39)

      at sun.plugin.cache.CachedJarLoader.download(CachedJarLoader.java:302)

      at sun.plugin.cache.CachedJarLoader.load(CachedJarLoader.java:128)

      at sun.plugin.cache.JarCache.get(JarCache.java:182)

      at sun.plugin.net.protocol.jar.CachedJarURLConnection.connect(CachedJarURLConnection.java:93)

      at sun.plugin.net.protocol.jar.CachedJarURLConnection.getJarFile(CachedJarURLConnection.java:78)

      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:580)

      at sun.misc.URLClassPath$JarLoader.<init>(URLClassPath.java:541)

      at sun.misc.URLClassPath$3.run(URLClassPath.java:319)

      at java.security.AccessController.doPrivileged(Native Method)

      at sun.misc.URLClassPath.getLoader(URLClassPath.java:308)

      at sun.misc.URLClassPath.getLoader(URLClassPath.java:285)

      at sun.misc.URLClassPath.getResource(URLClassPath.java:155)

      at java.net.URLClassLoader$1.run(URLClassLoader.java:190)

      at java.security.AccessController.doPrivileged(Native Method)

      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)

      at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:142)

      at sun.plugin.security.PluginClassLoader.findClass(PluginClassLoader.java:168)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)

      at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:122)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

      at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:555)

      at sun.applet.AppletPanel.createApplet(AppletPanel.java:567)

      at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1786)

      at sun.applet.AppletPanel.runLoader(AppletPanel.java:496)

      at sun.applet.AppletPanel.run(AppletPanel.java:293)

      at java.lang.Thread.run(Thread.java:536)

java.io.FileNotFoundException: http://localhost:8084/cms/log4j.properties/log4j-1.2.8.jar

      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:713)

      at sun.plugin.net.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:407)

      at sun.plugin.net.protocol.http.HttpUtils.followRedirects(HttpUtils.java:39)

      at sun.plugin.cache.CachedJarLoader.download(CachedJarLoader.java:302)

      at sun.plugin.cache.CachedJarLoader.load(CachedJarLoader.java:128)

      at sun.plugin.cache.JarCache.get(JarCache.java:182)

      at sun.plugin.net.protocol.jar.CachedJarURLConnection.connect(CachedJarURLConnection.java:93)

      at sun.plugin.net.protocol.jar.CachedJarURLConnection.getJarFile(CachedJarURLConnection.java:78)

      at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:580)

      at sun.misc.URLClassPath$JarLoader.<init>(URLClassPath.java:541)

      at sun.misc.URLClassPath$3.run(URLClassPath.java:319)

      at java.security.AccessController.doPrivileged(Native Method)

      at sun.misc.URLClassPath.getLoader(URLClassPath.java:308)

      at sun.misc.URLClassPath.getLoader(URLClassPath.java:285)

      at sun.misc.URLClassPath.getResource(URLClassPath.java:155)

      at java.net.URLClassLoader$1.run(URLClassLoader.java:190)

      at java.security.AccessController.doPrivileged(Native Method)

      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)

      at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:142)

      at sun.plugin.security.PluginClassLoader.findClass(PluginClassLoader.java:168)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)

      at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:122)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

      at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:555)

      at sun.applet.AppletPanel.createApplet(AppletPanel.java:567)

      at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1786)

      at sun.applet.AppletPanel.runLoader(AppletPanel.java:496)

      at sun.applet.AppletPanel.run(AppletPanel.java:293)

      at java.lang.Thread.run(Thread.java:536)
0
 
CEHJCommented:
>>codebase="log4j.properties"

No, that attribute is not right. The properties file should be *in* the codebase. Put it in the same directory as the applet class file. Try codebase="." if that's where the codebase is
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rakhareAuthor Commented:

Now it seems to work, but not quite. I modified the log4j.properties as below, but the following code simply results in "test" in the java console without any other specified properties in the pattern layout. Also, I see the same output even if I take delete the log4j.properties. Am I supposed to load the file somehow, or should the loading be automatic?

        Logger log = Logger.getLogger(MainPanel.class.getName());
        log.info("test");

----
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender which outputs to System.out.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
----
0
 
aozarovCommented:
Never tried to use log4j from an Applet, but if the above method didn't work for you then you can try calling the
PropertyConfigurator.configure manually usign the following URL new URL(getCodeBase(), "log4j.properties");
0
 
CEHJCommented:
Did you get any errors in the console?
0
 
rakhareAuthor Commented:

Neither worked.

The solution is to put log4j.properties at the root inside the jar itself. This works without the PropertyConfigurator.

But thanks for your efforts.

0
 
CEHJCommented:
OK
0
 
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now