Servlet Tomcat 8.0.9 logging

Hi everyone,

I am pretty new to Apache Tomcat; I have been playing around with it years ago but never got to a productive state until now. So I started another trial, but I keep hanging at the same points. I have also been scanning much material, and even it seems to be an easy issue (even to me...) I just can't find how to solve it. I suppose I am just missing a tiny little trick...

I was trying to get some proper logging from my Java classes in TomCat 8.0.9 (new, standard installation on Windows 7). I placed a logging.properties file in WEB-INF\classes:

Code:
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
org.apache.juli.FileHandler.level = ALL
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
org.apache.juli.FileHandler.prefix = Gourmet01.
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
I use these lines in my code:


Java:
...
import java.util.logging.*;
public class Customer {
    static Logger logger = Logger.getLogger( Customer.class.getName() );
    public String save() {
...
       logger.log(    Level.SEVERE, "message 1");
...
       logger.log(    Level.FINEST, "message 7");*/
        return "/showCustomer.xhtml";
    }
}
And there is the standard logging.properties in the Tomcat 8.0\conf directory. When starting TomCat and using my WebApp (invoking save()), I see the SEVERE message (as well as other WARNING and INFO), but no finer ones. I have pretty much the same problem with log4j as well, but now I switched back to the juli logging.

Does anyone see where I missed out? I have been trying dozens of variations, most of which I would think they should work, but they never do. What else do I have to consider?

Thanks a lot,
Stephan
 
I do recommend to leave tomcat logging as is.

You're webapp can use any logging framework it sees fit.

I almost always use log4j.

This can be achieved by copying the proper log4j.jar into the WEB-INF\lib directory of the webapp in question. This way you're logging doesn't interfere with any other logging, be it tomcat itself or other web applications.

When you use log4j, you will have a log4j.properties in the WEB-INF\classes as this directory by default is in the classpath of the webapp.
 
Hi turtle,

that's exactly as I did... I didn't intend to change Tomcat logging, sorry if I phrased wrongly... However, even I placed the files as you described, I would always only see the Error messages, but not other logging like debug or info. I also used the properties files, even I prefer the xml variant.

As I said, I just don't know where the problem is, also from my perspective it's all perfect, I just don't get the right output. Can you give me an example properties or xml configuration file? Then I would compare that to my settings. If you have a small example maybe I can figure out step by step where the differences are and maybe I find the glitch?

Thanks,
Stephan

BTW: I installed the latest log4j, which jars would you say I need? I have the core, the api and the 1.2 / 2.0 jar as well to make it build correctly, what do you think?
 
Zuletzt bearbeitet:
I just verified that all I said is indeed working, though my tomcat is currently only 7.0.23;)

I wrote a very simple servlet to test the setup and it's logging. Here it is:
Java:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

@SuppressWarnings("serial")
public class SampleLog extends HttpServlet {
    Logger logger = Logger.getLogger(SampleLog.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	logger.debug("Inside doGet");
	PrintWriter out = response.getWriter();
	out.println("<html>");
	out.println("<body>");
	out.println("<h1>Hello Servlet SampleLog</h1>");
	out.println("</body>");
	out.println("</html>");
    }
}
File system is as follows

  • WEB-INF
    • web.xml
  • WEB-INF\lib
    • log4j-1.2.17.jar
  • WEB-INF\classes
    • SampleLog.class
    • log4j.properties
My web.xml
[XML]<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Turtle</display-name>
<servlet>
<servlet-name>Turtle</servlet-name>
<servlet-class>SampleLog</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Turtle</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app> [/XML]
And log4j.properties as "usual"
Code:
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{DATE} %5p [%t] (%F\:%L) - %m%n
When I surf to my servlet
HTML:
http://localhost:8080/turtle
I see the following log in the output
Code:
15 Jul 2014 11:08:15,043 DEBUG [http-bio-8080-exec-3] (SampleLog.java:15) - Inside doGet
Please note that the DEBUG message is shown.
 
Zuletzt bearbeitet:
Hi, I got some news!

I tried your example and it didn't work for me - at first. I have a stdout log from TomCat and nothing showed up there. No differences to your example except the version of log4j... I use 3 jars in the WEB-INF/lib directory: log4j-1.2-api-2.0-rc2.jar, log4j-api-2.0-rc2.jar, log4j-core-2.0-rc2.jar, builds and runs perfectly but nothing on the output. With this faulty servlet I only exchanged these jars for version 1.2.15 (had it just at hand) - and it works... Now how can that be???

Thanks a lot for your hints, they took me to the right point!

Stephan
 
You used Apache Log4j 2, which is very new and I changed my project accordingly.

Java:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@SuppressWarnings("serial")
public class SampleLog extends HttpServlet {
    private static Logger logger = LogManager.getLogger();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	logger.debug("Inside doGet");
	PrintWriter out = response.getWriter();
	out.println("<html>");
	out.println("<body>");
	out.println("<h1>Hello Servlet SampleLog</h1>");
	out.println("</body>");
	out.println("</html>");
    }
}
WEB-INF\lib
  • log4j-api-2.0-beta9.jar
  • log4j-core-2.0-beta9.jar
and my log4j2-test.xml
[XML]<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="info"/>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>[/XML]
 
Still doesn't work for me... Where did you get these jars from? I was following what I expect to be the official site: Download Apache Log4j 2 - Apache Log4j 2 but the jars have a strange "rc" at the end in the name. Nevertheless, the documentation (on that page at the bottom) tell it's fine, so I proceeded. With my configuration still only INFO and above messages...

Stephan
 
This is indeed the official website. But I downloaded an earlier version (beta at that time) than the one found currently on site.

BTW: rc stands for release candidate, which demonstrates that it is still not considered a "stable" release.

Nevertheless, you should definitely delete the 1.2 jar from you're WEB-INF/lib folder as IIRMC this is an old version which might interfere with the new 2.0 jars.
 
Hmm, still doesn't help for me. I get these lines:

Code:
...
16-Jul-2014 08:42:04.959 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Users\049SWoebbeking\TomCatApps\Gourmet01_tryLog4j
16-Jul-2014 08:42:06.793 INFO [localhost-startStop-1] null.null Mojarra 2.2.7 ( 20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/[email protected]) für Kontext '/Gourmet01_tryLog4j' wird initialisiert.
16-Jul-2014 08:42:07.016 INFO [localhost-startStop-1] null.null JSF1048: PostConstruct/PreDestroy-Annotationen vorhanden.  Verwaltete Bean-Methoden, die mit diesen Annotationen markiert sind, lassen die entsprechenden Annotationen verarbeiten.
16-Jul-2014 08:42:07.252 INFO [localhost-startStop-1] null.null Monitoring file:/C:/Users/049SWoebbeking/TomCatApps/Gourmet01_tryLog4j/WEB-INF/faces-config.xml for modifications
16-Jul-2014 08:42:07.263 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Users\049SWoebbeking\TomCatApps\Gourmet01_tryLog4j has finished in 2,304 ms
...
16-Jul-2014 08:42:14.078 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 15017 ms
16-Jul-2014 08:42:18.745 WARNING [http-nio-8080-exec-6] null.null JSF1063: WARNUNG! Der nicht serialisierbare Attributswert wird in HttpSession festgelegt (Schlüssel: customer, Wertklasse: at.irian.jsfatwork.gui.page.Customer).
16-Jul-2014 08:42:20.926 SEVERE [http-nio-8080-exec-5] at.irian.jsfatwork.gui.page.Customer.save message 1
16-Jul-2014 08:42:20.926 WARNING [http-nio-8080-exec-5] at.irian.jsfatwork.gui.page.Customer.save message 2
16-Jul-2014 08:42:20.926 INFO [http-nio-8080-exec-5] at.irian.jsfatwork.gui.page.Customer.save message 3
in two files: catalina.2014-07-16.log and tomcat8-stderr.2014-07-16.log.

But there should some more messages:

Java:
		logger.log(	Level.SEVERE, "message 1");
		logger.log(	Level.WARNING, "message 2");
		logger.log(	Level.INFO, "message 3");
		logger.log(	Level.CONFIG, "message 4");
		logger.log(	Level.FINE, "message 5");
		logger.log(	Level.FINER, "message 6");
		logger.log(	Level.FINEST, "message 7");
Also, it should be in a different file according to the configuration I suppose:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
	<Appenders>
		<Console name="STDOUT" target="SYSTEM_OUT">
			<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
		</Console>
		<File name="logfile" fileName="Gourmet.log">
			<PatternLayout pattern=="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
		</File>			
	</Appenders>
	<Loggers>
		<Logger name="org.apache.log4j.xml" level="debug"/>
		<Logger name="at.irian.jsfatwork.gui.page.Customer" level="debug">
			<AppenderRef ref="logfile"/>
		</Logger>
		<Root level="debug">
			<AppenderRef ref="logfile"/>
		</Root>
	</Loggers>
</Configuration>
I have the strong impression that the log4j just doesn't find/read/care about the configuration file at all. But no idea why that would be. So any more ideas?

Thanks for all your time dedicated to this my problem...

Regards,
Stephan
 
You'd know already that tomcat is using JULI for it's logging. So, tomcat itself doesn't use the configuration you posted.

It is within you're webapp which will use log4j and it's configuration.

As you didn't specify any path for the file Gourmet.log, it is presumably in WEB-INF/classes of that webapp. Probably you should search for the file?

I don't recommend to store log file within the webapp. You'd use a different path. Therefore tomcat itself store log files into the logs directory. Therefore I specify it always this way:
[XML]log4j.appender.FILE.File=${catalina.home}/logs/gourmet.log[/XML]

Which also puzzles me, are the messages concerning the jsfatwork or you're JSF thing you're doing. Therefore I do recommend to try with a plain vanilla tomcat without any other webapp deployed.

The logging lines are not clear to me, where they are stored. Could you please be more specific?

PS: Talking english in a german board is no problem for me but I'm asking whether we can switch to german to let other join the converstion;)
 
Hallo turtle,

ja, du hast natürlich Recht, wir können auch gern in deutsch schreiben. Meist schreib ich solche technischen Dinge rein aus Gewohnheit in englisch, weil es dann mehr Leute lesen können. ;)

Die Trennung zwischen dem Tomcat Logging und dem meiner WebApp ist mir schon klar. Bloß ich bekomme es nicht realisiert. Die WebApp Meldungen mischen sich immer wieder in die Standard Logfiles, genau das ist ja mein Problem (neben der Tatsache, dass nicht alles geloggt wird).

Ich habe nach "Gourmet*.log" files gesucht. Es gibt zwei, die aber beide schon früher bei meinen Versuchen mit Logging geschrieben wurden. In WEB-INF/classes habe ich auch immer wieder geschaut. Ich habe das so angegeben, damit ich mir erstmal keine Gedanken um die Slash/Backslash Problematik machen muss. Das würde ich natürlich anpassen, sobald IRGENDWO das Logfile auftaucht... Tut's aber nicht *snief*.

Das JSF würde ich halt gerne verwenden, möglicherweise kommen ja gerade da die Seiteneffekte her, aber die würde ich ja gerne klären, damit ich beides nutzen kann... Und die Installation ist im Prinzip nagelneu, alles was ich bisher gemacht habe, sind die Versuche zu diesem Logging, damit habe ich angefangen.

Die o.g. Dateien sind catalina.2014-07-16.log und tomcat8-stderr.2014-07-16.log, beide sind Standard TomCat logfiles und liegen im "logs" Verzeichnis meiner Tomcat Installation bei "C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs"... Hilft das?

vG,
Stephan
 
catalina.*log und tomcat8-stderr*.log sind natürlich die Logdateien vom Tomcat.

Da ich dir gesagt habe, was bei mir läuft, nämlich ein "stinknormales" Servlet, schlage ich vor, das du das erstmal zum Laufen bringst.

Wenn du auf das Servlet triffst (per URL im Browser), kannst du es auch notfalls debuggen. Ich nutze dazu immer das Sysdeo-Plugin. Aber immer mehr SW auf das Problem schmeissen, bringt meiner Ansicht nach wenig.

Wenn das Servlet nicht richtig loggt, muss irgendwo ein Hinweis sein, warum Tomcat das nicht macht.

Ich habe echt noch nie gehört, das Tomcat zu wenig (bis gar nichts loggt).
 
Hallo!

Neue Info, ich denke sogar, jetzt ist es gelöst... Hoffe, dass ich da nicht noch was finde. ;)

Dein Servlet hatte ich ja schon früher zum laufen bekommen, nur bisher nicht zusammen mit log4j2 (Version 2!), sondern nur mit der älteren Version. Jetzt habe ich nochmal etwas rumexperimentiert und - weiß wer auch immer warum mir das vorher nicht aufgefallen ist, bzw. warum ich es noch nicht probiert hatte - dabei ist mir diese Zeile aufgefallen (die auch sehr häufig im Web zu finden ist, vgl. http://logging.apache.org/log4j/2.x/manual/migration.html):

[XML]<Logger name="org.apache.log4j.xml" level="debug"/>[/XML]

Nach meinem jetzigen Eindruck der Versuch muss das (man könnte es sich denken, aber wenn es mehrfach so im Web zu finden ist...) so lauten:

[XML]<Logger name="org.apache.logging.log4j.xml" level="debug"/>[/XML]

Jetzt bekomme ich ein benanntes Logfile aus Log4j2 mit allen Leveln dargestellt... Schwere Geburt!

Vielen Dank für deine Unterstützung!!!

Stephan
 
Da hast du einen Punkt gefunden, der mir noch nicht bekannt war:toll:

Es steht sogar auf der offiziellen Website "falsch".
[XML]<Logger name="org.apache.log4j.xml" level="debug">[/XML]
Aber, wie du richtig bemerktest, wenn's so im Web steht:D

Zumal ich derzeit noch davon ausgehe, das log4j-2 selten verwendet wird.

PS: Vielleicht solltest du deine Erkenntnisse mal auf der log4j-user Mailing-Liste posten.

Bin aber froh, das es jetzt läuft!
 
Zuletzt bearbeitet:
Passende Stellenanzeigen aus deiner Region:

Oben