tomcat logs to be stored in a separate file - java

I have developed an spring mvc application which is executing perfectly on tomcat , but the logs I am getting of the server tomcat is on console , I want that logs to be stored in a file ,in other words I want tomcat logs and events that are shown on console to be get stored on a file , I have tried log 4j of apache and below is the properties file I am using ...
### direct messages to file or.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:/logs/springmvc.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n
log4j.appender.file.append=true
### set log levels - for more verbose logging change 'info' to 'debug' ##
log4j.rootCategory=ALL, file
log4j.logger.Demo=\=debug
log4j.logger.org.eclipse=debug
and the main class.. on which I want log.info statements to be get recorded..
public class HelloWorldController extends AbstractController{
protected final Log logger = LogFactory.getLog(getClass());
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.info("Returning hello view");
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "saral saxreva");
return model;
}
}
but still the logs are not generated in separate file , please advise as I am stuck up on this.

I think for enabling logging in web app you need some listener (or servlet) that will initialize it. For example in Spring you need to define in your web.xml such listener:
<!-- This listener is necessary to enable log4j logging -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
In internet there are a lot of info about this. Here is the firs link from google. Here is another one link where describet how to init logger in servlet or in listener without Spring.
And also it is better to use log4j.rootLogger instead of log4j.rootCategory because last is deprecated.

Does the springmvc.log file even created when your program starts? That's a first indication of whether or not the log4j is being picked up properly.
Make Sure the log4j properties file is in the correct location and are picked up properly
Double check the import statement for your logger and LogFactory classes (just to make sure they are using the log4j ones
If the log4j is set up properly, you'll see springmvc.log file created when the app starts.
Then in the log4j properties, try:
log4j.rootLogger=DEBUG,file
and also get rid of the other 2 that you have
log4j.logger.Demo=\=debug
log4j.logger.org.eclipse=debug

Related

How generate log files separated by apps running over websphere 9 ? I use java.util.logging to generate logs

I use websphere 9 application server to deploy war's and ear's, and use java.util.logging to generate logs into applications. I try to use properties file to configure the FileHandler of the LogManager, but websphere write ALL other logs on my file.
I not use log4j because i can't set log levels at runtime.
Is possible make differents file logs by application over websphere with java.util.logging ?
This is my properties file Logger.properties
handlers= java.util.logging.FileHandler
#java.util.logging.ConsoleHandler.level = INFO
#java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Set the default formatter to be the simple formatter
java.util.logging.FileHandler.formatter =java.util.logging.SimpleFormatter
# Use UTF-8 encoding
java.util.logging.FileHandler.encoding = UTF-8
# Write the log files to some file pattern
java.util.logging.FileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs.log
# Limit log file size to 5 Kb
java.util.logging.FileHandler.limit = 5000
# Keep 10 log files
java.util.logging.FileHandler.count = 10
#Customize the SimpleFormatter output format
java.util.logging.SimpleFormatter.format = %d [%t] %-5p (%F:%L) - %m%n
I try to use properties file to configure the FileHandler of the LogManager, but websphere write ALL other logs on my file.
Per your logging.properties you are attaching the FileHandler to the root logger. It would be expected to see log records from WebSphere because by default Logger::setUseParentHandlers is set to true.
Is possible make differents file logs by application over websphere with java.util.logging ?
You have to do one of the following:
Attach your FileHandler to the root logger of your application so the FileHandler is not attached to parent of the WebSphere application. Say application package is com.my.app.rules you can add entry of tocom.my.app.rules.handlers=java.util.logging.FileHandler and remove the attachment to the root logger. Next you demand the logger from code and pin it to memory so this package becomes the new root logger of your application code.
Create java.util.logging.Filter and install it on the FileHandler.
Turn off the logging for WebSphere. You can do this by setting the root logger to off and forcing all of your loggers to say info.
.level=OFF
com.my.app.rules.class1.level=INFO
com.my.app.rules.class2.level=INFO
com.my.app.rules.class3.level=INFO
It's possible set one FileHandler by app ?
The default java.util.logging.LogManager only supports one set of FileHandler settings. You can attach instances to various loggers but it doesn't allow for instances with different settings via the logging.properties. However, the LogManager does support per the documentation:
A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.
So what you need to do is bundle a configuration class with your application that performs the needed FileHandler and logger configuration and modify your logging.properties to load it. One issue with this approach is that the LogManager can only see classes from the system class loader.
From jmehrens answer you can see it might be a bit difficult. Maybe you should consider switching logging to High Performance Extensible Logging (HPEL). It is just setting in the application server, so no changes in the applications code.
Then you could query logs for the given application using command line tool:
logViewer.sh -includeExtensions appName=PlantsByWebSphere
This would be much easier, if it fits your purpose. Moreover, generating separate log files is now not recommended, if you plan to move your apps in the future into containers/cloud or refactor them into microservices.
My solution to this isue was create a PersonalFileHandler to use on all Logger's. Properties are read from properties file called "Logger.properties", and invoque read this programmatically, same like this:
//Read config file
LogManager.getLogManager().readConfiguration(LoggerJUL.class.getResourceAsStream("/Logger.properties"));
//Add handler to logger
Logger.getLogger(clazz)).addHandler(new PersonalFileHandler());
PersonalFileHandler extends FileHandler, and properties are set by configure method on FileHandler class on runtime.

Deploy a html file on tomcat which gets created after deploying a war

I have a dynamic web-app. I'm working in a spring environment, with maven and mybatis. I'm deploying a war file on tomcat. I'm using log4j for logging the data. I want this data to be accessible real time to everyone. For example if the home page of the web-app is localhost:8080 then I want something like localhost:8080/logs to display the logs real time. I know I have to Spring request mapping for this. But I don't know how to make it work, when my file keeps on updating itself.
Any help is appreciated
If it's a spring mvc.
In your controller, you need to read the file and write it into the response.
It's actually fair simple.
Code snippet for you as reference: The following is SUDO code. Just give you the direction
#RequestMapping("/log")
public class Controller {
public void readLog(HttpRequest req, HttpResponse resp) {
File file = new File('YOUR_FILE_LOCATION')
FileReader fr = new FileReader(file);
// you can read every line for the log file and write into resposne
while(str = nextLine()) {
resp.println(str)
}
fr.close()
}
}
You can use HTML Layout feature of log4j as following:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${catalina.base}/webapps/yourappname/log.html
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML logs
log4j.appender.FILE.layout.LocationInfo=true
This file should be accessible on http://localhost:8080/yourappname/log.html. Tomcat will provide catalina.base/catalina.home as system property or you can provide full file path.

How to log messages in a separate log file with java logging?

I have a simple question where I still can not find any answer. I want to log messages into a separate log file. I am using Java logging and not log4j.
I have the following class:
package org.imixs.workflow;
public class MailPlugin {
....
private static Logger logger = Logger.getLogger(MailPlugin.class.getName());
...
logger.info("some info...");
}
I am using GlassFish server. So I need to customize the settings in the logger.properties file from GlassFish.
What entries need to be added to the GlassFish logger.properties file to log all messages from my class 'MailPlugin' into a separate log file?
You can create a file appender and apply a Filter to it that only returns true when it the logging is coming from the MailPlugin

Log4j doesn't log INFO Level

I have the following log4j.properties file, for an application deployed in WebSphere Portal:
log4j.rootLogger=DEBUG, InfoAppender, DebugAppender
log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/info.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/debug.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - %m%n
When I code, I define the logger at class level:
private static Logger logger = Logger.getLogger(IWannaLogThis.class);
And I log INFO messages with this:
logger.info(theObjectToLog);
When I deploy my application, the debug.log file gets everything I log with logger.debug() but ignores everything I write with logger.info(). On the other side, the info.log file keeps empty.
The weirdest thing is that in debug.log and info.log appears some INFO and DEBUG messages made by some JARS (like Hibernate Validator) I had in the classpath, but just ignores everything I try to log in my code.
Any ideas?
This is most likely a classloading-related problem. WebSphere Portal uses Log4J internally, so I'm guessing that you end up using WebSphere Portal's provided Log4J JAR file as well as its own Log4J properties.
You can verify that by adding the following to the JVM arguments of the server instance:
-Dlog4j.debug=true
And then inspect the SystemOut.log file. Log4J will spit out lots of tracing information about the configuration file(s) it reads.
The best way to avoid this is to do the following:
Bundle the Log4J JAR file with your application.
Associate a Shared Library with the server. In that Shared Library, place your Log4J configuration file.
As an alternative to step 2, you can bundle your Log4J configuration file with the application itself, however that would carry its own drawbacks (for example, having to repackage your application whenever you perform a Log4J configuration change).
Another common problem is that the JARs you have in your classpath also use log4j and also have their own appenders set. So depending on the settings that they use, and the packages that your classes reside in, this may lead to the problem you describe.
So:
Make sure that your package names are unique and not used by any of the third party libraries.
Check the log4j settings in all libraries in your classpath. They should not contain general settings which override yours.
Make sure your loggers use your log4j.properties (you can be sure if changes you make in your file affect your loggers as expected).
If you can, make sure that your log4j stuff loads last, in case any of the third party libs reset the configuration. They shouldn't, but who can stop them.
Normally, it should be one of these things. Post more explicit example if it doesn't work.
Good luck!
What I have done in the past is set specific logs for the classes I want to log. It sounds like you can try setting your root logger to INFO and see if that gets you the messages you want. Here's a little bit of my log4j property file. I set a logger for each class and assign it to my "data" appender, which defines the log layout. In the loggers I specify specific classes I want to log and set their Log level individually. Any class that logs that is not defined in the Loggers I have use the default log level for the rootCategory.
log4j.rootCategory=INFO, rollingFile, stdout
#GetData Loggers
log4j.logger.com.myapp.data=INFO, data
log4j.logger.com.myapp.data.SybaseConnection=DEBUG, data
log4j.logger.com.myapp.data.GetData=ERROR, data
# data appender
log4j.appender.data=org.apache.log4j.RollingFileAppender
log4j.appender.data.layout=org.apache.log4j.PatternLayout
log4j.appender.data.File=c\:\\Program Files\\MyApp\\logs\\MyApp-data.log
log4j.appender.data.Append=true
log4j.appender.data.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
you root logger opens the log properties in the debug mode,
use INFO instead of DEbug in the first line of your properties file.

Tomcat App says "Unable to configure the logging system. No log.properties was found."

I have an embedded tomcat app and whenever I start it up I see this error printed to the console twice:
Unable to configure the logging system. No log.properties was found.
It seems stupid, but I've done some googling and searched stackoverflow and can't seem to find someone experiencing this problem.
My main class Looks roughly like this:
public class AuthServerEntryPoint {
static {
org.apache.log4j.PropertyConfigurator.configure("conf/log4j.properties");
}
public static void main(String[] args) {
// ...
}
"conf/log4j.properties" contains a seemingly valid configuration:
log4j.appender.mainAppend=org.apache.log4j.ConsoleAppender
log4j.appender.mainAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.mainAppend.layout.ConversionPattern=%d %p [%t] %c -- %m%n
log4j.appender.fileAppend=org.apache.log4j.FileAppender
log4j.appender.fileAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppend.layout.ConversionPattern=%d %p [%t] %c - %m%n
log4j.appender.fileAppend.file=logs/myservice.log
log4j.rootLogger = info, fileAppend
log4j.logger.com.mycompany.myservice = debug, fileAppend
And the logging actually does work - i.e., logs are correctly written to myservice.log. So what gives?
Thanks!
-Carl
By embedded Tomcat app, do you mean that you are starting Tomcat from Java code and are using the class org.apache.catalina.startup.Embedded?
If yes, my guess is that Tomcat might not be able to find its logging configuration file that is set up in catalina.sh (or equivalent) when using the scripts:
LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
The odd part is that this file is called logging.properties, not log.properties, so I'm really not sure. I didn't check Tomcat sources though, maybe they are doing some kind of black magic in there.
Could you just try to rename $CATALINA_BASE/conf/logging.properties into log.properties (or not) and to put it on the classpath of your app (or to set -Djava.util.logging.config.file)?
By default, log4j will look for a logging properties file on the classpath. Put your webapp's logging properties config file into its WEB-INF/classes directory; e.g.
$CATALINA_HOME/webapps/<yourApp>/WEB-INF/classes/log4j.properties
This is the simple approach to getting a webapp to use Log4j is recommended by the referenced documentation. And it is the approach I use myself for webapp logging.
There are various other ways to configure Log4j logging on Tomcat as well. It is possible that your Tomcat has been (partly) configured to use one of them, but something has not been done quite right.
Configuring Tomcat' log4j logging via the system properties is an option that avoids figuring out where log4j is looking ... and what is going wrong. But you are then stuck with creating/using a custom launch script or having to remember to set environment variables before launching Tomcat.
References:
Configuring logging on Tomcat 5.5
Configuring logging on Tomcat 6
The "Default Initialization under Tomcat" section of the Log4j Manual

Categories