tomcat jdbc SlowQueryReport interceptors - log in a separate file - java

I am trying to log all my slow queries in a separate file. Until now I have to following Tomcat context configuration:
<Resource name="jdbc/paymentDB" auth="Container" type="org.apache.tomcat.jdbc.pool.DataSource"
driverClassName="...oracle..."
...
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor(queryTimeout=2);org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1000,maxQueries=200)"
</Context>
This works as long as I do not set another kind logger and it prints to the console. One thing that I believe should be added is that I run this test in IntelliJ Idea using default IDE configuration.
The next thing I wanted to do was to log into a separate file. So I opened logging.properties and did the following changes:
handlers = ..., 5slowqueries.org.apache.juli.FileHandler, ...
.handlers =..., 5slowqueries.org.apache.juli.FileHandler, ...
5slowqueries.org.apache.juli.FileHandler.level = ALL
5slowqueries.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
5slowqueries.org.apache.juli.FileHandler.prefix = slow-queries.
org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport.level = ALL
org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport.handlers = 5slowqueries.org.apache.juli.FileHandler
The problem is that executing the same slow queries and have been printed earlier in console, this time, using this configuration, no slow-queries.* file is created. (I ran this from IntelliJ Idea)
I can't figure out how to make this work. Maybe it has something to do with IDE? I have noticed that IDEA has a Logs category in Run/Debug Configurations, I tried to play with these options too but didn't have any luck.

I found the problem. It was the IDE. When IDEA starts the server it prints something like this in the console:
Using CATALINA_BASE: "C:\Users\..."
Using CATALINA_HOME: "C:\..."
another few variables
The logs are created, by default, if not changed, in CATALINA_BASE/logs.

Related

Spring Boot War File Tomcat 9 Deployment Help Status 500 Error

UPDATE 8/12/2020 approx. 11:00 AM - I checked my logs folder catalina.2020-08-12.log log in the tomcat9 ($CATALINA_BASE) folder and I found three exceptions "org.apache.catalina.LifecycleException: Protocol handler intialization failed", "java.net.BindException: Address already in use (bind failed)","java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol [HTTP/1.10x0aHost:]".
Here is the full error log here: https://drive.google.com/file/d/1pSk5ESHLIRP5Srxwr4R1Tp_1TTcYMf2Y/view?usp=sharing and https://drive.google.com/file/d/1C-RALR3066_hYKeoo4dIJqTKwNDOQ0R0/view?usp=sharing.
UPDATE 8/12/2020 approx. 11:30 AM - I found that there are two servlet dispatchers running by checking the localhost.2020-08-12.log log when I restarted tomcat and tried to access my api again: https://drive.google.com/file/d/1GFKUIDLtZrAraHyMjyyWV9kXK33gQ-Mu/view?usp=sharing.
UPDATE 8/12/2020 approx. 11:35 AM - HA HAAAA I looked in the catalina.out log (finally some of you guys were probably yelling at the screen like "CHECK THE CATALINA.OUT FILE"). I checked it and I found the exception: "java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required": https://drive.google.com/file/d/1A7ZLh70IHOmWXfQ56bgtq951EE_AX68P/view?usp=sharing. Now if I go to my PostgresDataSource class which is where my Hikari config is: https://drive.google.com/file/d/1lYN1dTqiAM4ZqliRGC5J5_VWhNeN-k2d/view?usp=sharing, and I review my application.yaml file from earlier: https://drive.google.com/file/d/1xvPhPneVjMU4BoN6F0Q63l2wCnF20BYq/view?usp=sharing, the jdbc url is present because it is the same one I used for developing. Now postgres is already set up and I manually made all of the tables on the ubuntu droplet. I did not download the jdbc postgresql driver for tomcat yet.
UPDATE 8/12/2020 approx. 1:20 PM - After setting up the jdbc driver now I get this error from catalina.out logs: " Failed to get driver instance for jdbcUrl=java:/comp/env/jdbc:postgresql://localhost:5432/****" and then I get "Caused by: java.sql.SQLException: No suitable driver". I used the postgresql section of this link to set it up: https://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html.
Hi I am using Digital Ocean with apache tomcat 9 to deploy a war file that is generated using IntelliJ. I have the war file generated using mvn clean install and I get the default tomcat page when I go to the droplet IP address on port 8080. I am not sure why I get a 500 error with no stack trace information even though my spring boot application works fine when I run it in IntelliJ. My CSS styling also does not show up when I go to my hosted application I have tried to configure a few things in my application.yml file and it still does not work. I figured it might have to do with a spring security issue with CORS, but I am not knowledgeable enough in that area to know for sure. I started my server by using sudo $CATALINA_HOME/bin/startup.sh and it said tomcat started and it showed the default tomcat page when I went there in the browser. I used the hobynapi.war file in my target folder a picture of my target folder directory can be viewed here https://drive.google.com/file/d/1XXga1Bgf5-_81gceuWjpvVIX6J0jiNgQ/view?usp=sharing. I copied the war file that was generated after using mvn clean install to the /var/lib/tomcat9/webapps folder. I then ran sudo $CATALINA_HOME/bin/startup.sh again and it ran tomcat successfully again. Then the error happened as I tried to log in with the admin account with spring security basic authentication where I get the 500 error. I also have no artifacts in the artifact section of the project structure settings if that means anything.
UPDATE 8/12/2020 approx. 10:00 AM - I've removed the exclusions tag in the pom.xml file as well as the profiles tag at the bottom of the file. This is because I read here that using the spring-boot-starter-tomcat dependency removes the embedded server for you: https://developer.okta.com/blog/2019/04/16/spring-boot-tomcat.
This is the screen without the CSS styling that should be there:
https://drive.google.com/file/d/1CEKuB556ek8pTZspvprNTa9M5XCI7qtT/view?usp=sharing
This is the error I get without a stack trace:
https://drive.google.com/file/d/19gYGQFhZdZ_4IwwGE5tQzb4dTqlBpg9L/view?usp=sharing
These are pictures of my pom.xml file:
https://drive.google.com/file/d/1ir5uBuBuJqCb_U8Jarly9mraO468QA_y/view?usp=sharing
https://drive.google.com/file/d/1ma1_EPro4NvJiOy8lWPrAmGLMrVWHZPS/view?usp=sharing
https://drive.google.com/file/d/11NDIwA9g02zkCKnsmYSLSNF5R-BrFpgt/view?usp=sharing
https://drive.google.com/file/d/1p6T9-oOwyfj7NUsLxTRLtqXKHU4qHrZx/view?usp=sharing
https://drive.google.com/file/d/1spD7KT-3WBo7KFF0Hb_WgJ5dVKRHF-un/view?usp=sharing
https://drive.google.com/file/d/16QtJQtT9zaU7nGcpnm2KXJ6jiLywNt5s/view?usp=sharing
https://drive.google.com/file/d/1cnZJshFS5xW-HbeaH_C_e6KcDo9xYRdY/view?usp=sharing
This is my application.yml file:
https://drive.google.com/file/d/1xvPhPneVjMU4BoN6F0Q63l2wCnF20BYq/view?usp=sharing
https://drive.google.com/file/d/1B9_5WueRvqQv4ODsJAfbUzGIm-xaaSYV/view?usp=sharing
This is my main application java file:
https://drive.google.com/file/d/1K9lzzlzIKT8irqXsPY1WeiBvN-9vsoWT/view?usp=sharing
This is my web config file in my config package:
https://drive.google.com/file/d/19EYa2kkqqPu9Wh8nN-U02szSVBT30lOP/view?usp=sharing
https://drive.google.com/file/d/11Zw8dLrAu6c_OI29j0NsljvXdeDam0-t/view?usp=sharing
This is my spring security config file in my config package:
https://drive.google.com/file/d/1inqlTjfxahcF6ZeJ_xkJM18UgsDu119n/view?usp=sharing
https://drive.google.com/file/d/1nTeYiKhiNOH71Km-s_T4IT1BKRyJG4mS/view?usp=sharing
I hope that these pictures help for diagnosing the issue.
I figured it out. First I changed the postgres jdbc url back to the original one that I had and then I altered my data source config file to the following: https://drive.google.com/file/d/1Zmv8cfnVzW8NcQfjNxN14D4aKFaXpdfU/view?usp=sharing. One important thing I also messed up on was not setting a password for the default user "postgres" when downloading postgresql. I did this my calling "ALTER USER postgres PASSWORD 'myPassword';" in the postgresql command line. Also make sure that you use systemctl restart postgresql after any changes are made to postgresql in the postgresql command line.

Velocity template cannot be found in Windows 10

My laptop just got upgraded to windows 10 from windows 7, and a piece of code stopped working. A large application using velocity templates which used to work on windows 7 just fine now cannot find the template files.
The templates are kept at the path WebContent\WEB-INF\config\templates under the project directory. An EngineInitializer class is used to load them. The code for the class is as follows:
private static Logger logger = Logger.getLogger(EngineInitializer.class);
private static String RELATIVE_PATH_FOR_TEMPLATES = "/WEB-INF/config/templates";
if(logger.isDebugEnabled())
logger.debug("About to initialize the Velocity Engine");
Properties p = new Properties();
String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory
//configure the velocity logger to use the default logging
p.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,"org.apache.velocity.runtime.log.Log4JLogChute");
p.put("runtime.log.logsystem.log4j.logger", "defaultLog");
p.put("file.resource.loader.path", absolutePath + RELATIVE_PATH_FOR_TEMPLATES);
p.put("file.resource.loader.cache", "true");
p.put("file.resource.loader.modificationCheckInterval", "-1");
p.put("parser.pool.size", "30");
Velocity.init(p);
if(logger.isInfoEnabled())
logger.info("The velocity engine is now initialized..");
The following lines in the applicationBeans.xml file initializes the engine:
<!-- initialize the velocity engine before the listener thread starts -->
<bean id="engineInitializer" class="com.file.myprogram.template.processor.EngineInitializer"
init-method="initializeEngine" />
At the beginning, the log is getting printed. Inside the individual classes, the templates are called using Velocity.getTemplate() method. This is now returning a org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'MediationZone.vm' error. Nothing other than the underlying OS has been changed. This code runs fine on an RHEL server as a web app. Code has been downloaded from subversion and run on the windows 10 laptop using eclipse v4.9.
What is going wrong here?
I had the same issue and nothing worked (file:/ prefix variants, C: or c:).
Finally I tried it with some relative path (like used in the production environment) and voilĂ , it worked.
Figured out current run dir using System.getProperty("user.dir"):
vel.getTemplate( "foo/bar/some.vm", UTF_8.name() )

Jetty deletes log file every day

I have a fresh install of Jetty 9.4.4.v20170414. I enabled logging with:
java -jar start.jar --add-to-start=console-capture
and configured its settings in start.ini. java -jar start.jar --list-config gives this output:
...
jetty.console-capture.append = true
jetty.console-capture.dir = logs
jetty.console-capture.retainDays = 90
...
When jetty starts it creates log file in logs directory, it works just fine.
But the file is gone the next day and no new file for following day is created.
When I was using 9.3.x every day jetty just created another file to log into.
Upgrade to Jetty 9.4.5.v20170502
There was a nasty side effect of a different bug that is causing this, that was fixed with the 9.4.5 release.

using tinylog to write loggings into tomcat's log-folder

I want to write logging-messages to a defined file into the tomcat's log-folder, using eclipse, maven, tinylog.
Problem: There is no webapp.log as soon as I run the app in tomcat.
In eclipse everything works fine.
What I did:
add Maven-dependency tinylog-1.2.jar
set configuration-parameter in Run Configuration (Main-Tab) so the tinylog-properties can be found for the build-process:
name: -Dtinylog.configuration
value: C:\Program
Files\Tomcat\apache-tomcat-9.0.0.M13\webapps\folder\subfolder\tinylog.properties
in Java-Class:
import org.pmw.tinylog.Logger;
...
Logger.info(message);
tinylog.properties looks like:
tinylog.writer = file
tinylog.writer.filename = webapp.log
tinylog.writer.buffered = true
tinylog.writer.append = true
tinylog.level = info
I also tried different file-references but none of them worked:
tinylog.writer.file = C:\Program Files\Tomcat\apache-tomcat-9.0.0.M13\logs\webapp.log
tinylog.writer.file= "C:\Program Files\Tomcat\apache-tomcat-9.0.0.M13\logs\webapp.log"
Does anybody know how to write the logs into the named path-file?
Thanks for any valuable hint.
I propose to use the tinylog-jul artifact instead of the usual tinylog artifact. tinylog-jul provides the tinylog API, but uses the Tomcat logging back end. So, you don't need to configure tinylog. All log entries will be automatically output as you are used to with other logging APIs on Tomcat.

Application specific log in tomcat 7 using JULI?

I'm using java system logging in tomcat 7, but no logging statements get written to the log. I've added this file to my WEB-INF/classes. The log file "new-xyz-test" gets created (so I have at least some of the config right) but its empty - no log statements get printed to it.
handlers=java.util.logging.ConsoleHandler, org.apache.juli.FileHandler
org.apache.juli.FileHandler.level=ALL
org.apache.juli.FileHandler.directory=${catalina.base}/logs
org.apache.juli.FileHandler.prefix=new-xyz-test-
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.xyz.level=ALL
com.xyz.handlers=org.apache.juli.FileHandler
To configure JULI in the web applications you need have a logging.properties file in the WEB-INF/classes directory. If you use the default handlers, you may lose messages. You need to specify a prefix for the handler in your file.
handlers=1FILE.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers=java.util.logging.ConsoleHandler
1FILE.org.apache.juli.FileHandler.level=FINEST
1FILE.org.apache.juli.FileHandler.directory=/app-logs
1FILE.org.apache.juli.FileHandler.prefix=file-1
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
com.xyz.level=INFO
com.xyz.handlers=1FILE.org.apache.juli.FileHandler
com.abc.level=INFO
com.abc.handlers=java.util.logging.ConsoleHandler
A
handler prefix (e.g. 1FILE.) starts with a number, then has an arbitrary string, and ends with a period (.).
See more in Logging in Tomcat
Arguments in the JVM
If you are not running the Tomcat from the startup.sh or startup.bat, you need to specify:
The location of the general logging.properties for Tomcat (in the conf directory of Tomcat)
The manager org.apache.juli.ClassLoaderLogManager. This is important because allows you to configure
for each web application different loggin options. By default, a JVM process can only have a single configuration file.) ,
Similar to the next (I'm using eclipse):
-Djava.util.logging.config.file="C:\Users\Paul\workspaces\utils\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
By default, java.util.logging read the file that is included in the JDK or JRE, e.g.:
"C:\Software\jdk1.7.0_17\jre\lib\logging.properties"
Setting Tomcat Heap Size (JVM Heap) in Eclipse, for how to add arguments in the VM
are you sure that you write to the correct logger , i.e. Logger.getLogger("com.xyz")?
I think that you may got wrong when you wrote in logging.properties:com.xyz.level=ALL com.xyz.handlers=org.apache.juli.FileHandler in the case that you actually write to the logger Logger.getLogger(com.xyz.YourClass.class), that because in the logging properties file you should write the logger name which is in your case com.xyz.YourClass

Categories