I m using embedded tomcat in my java application. below is my source code. however tomcat is not generating any log.
embedded = new Embedded();
embedded.setDebug(3);
org.apache.catalina.logger.FileLogger embeddedFileLogger = new org.apache.catalina.logger.FileLogger();
embeddedFileLogger.setDirectory(tomcatHome+"/log");
embeddedFileLogger.setPrefix("Embedded_log_");
embeddedFileLogger.setSuffix(".txt");
embeddedFileLogger.setTimestamp(true);
embeddedFileLogger.setVerbosity(3);
//embedded.setLogger(new SystemOutLogger());
engine = embedded.createEngine();
//engine.setLogger(embeddedFileLogger);
embeddedFileLogger.setContainer(engine);
engine.setDefaultHost("localhost");
host = embedded.createHost("localhost", tomcatHome + "/webapps");
//host.setLogger(embeddedFileLogger);
engine.addChild(host);
_context = embedded.createContext("", tomcatHome + "/webapps/ROOT");
host.addChild(_context);
embedded.addEngine(engine);
CoyoteConnector connector = (CoyoteConnector)embedded.createConnector(InetAddress.getByName(ipAddress), port, false);
embedded.addConnector(connector);
embedded.setLogger(embeddedFileLogger);
embedded.start();
Please let me know how can i enable embedded tomcat logging through code or tomcat configuration.
By default the embedded Tomcat uses the logging configuration provided by the JDK. If you haven't changed the configuration only a
ConsoleHandler
is configured. If you wanted to programmatically add aFileHandler
you can add it to the root logger. Here's an example that writes to the filecatalina.out
by appending the messages onINFO
level. This works for Tomcat 6.x and 7.x.For me this worked only partially. This requires adding one more line:
I wanted to show ALL logs and make logging level configurable. Here is what worked for me:
I had just to figure out, how I can control the logging of the embedded Tomcat the same way the standalone version is doing it.
You will need the
tomcat-juli.jar
, because it has a customLogManager
in it. ThisLogManager
has advantages, since it is capable of registering multipleFileHandlers
. So it enables you to separate Logs per WebApp.It is not enough to just include the
tomcat-juli.jar
, but you also need to activate itsLogManager
. This would be done through a JVM-Parameter:-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Now it is possible to register
FileHandlers
like shown in the Tomcat documentation.With an additional JVM-Parameter you can set the path to the
logging.properties
-File:-Djava.util.logging.config.file=/logs/logging.properties
It is also possible to load the
.properties
-File programmatically, but then you need to set ajava.util.logging.config.class
with a JVM-Parameter, like above. In it's constructor you then must callLogManager.readProperties(...)
. Look here for more information.Best regards
You need to include the tomcat-embed-logging-juli-8.0.15.jar in your classpath.
I used this code to load the logging.properties file and configure the logging externally using the normal java logging approach:
For more details about configure the logging.properties use this pages: Tomcat logging, org.apache.juli.FileHandler
Also you can use the java.util.logging.FileHandler that is also compatible with the juli aproach an has another advanced configuration options: java.util.logging.FileHandler
There is another jar for use log4j but for me this works fine. Note: I used the tomcat embedded 8.0.15 but there are new versions now.