log4j not logging to file - java

I've got a simple Tomcat web service that I'd like to see the logs for. I figured log4j would be the easiest solution. So far it will log to the in my dev environment, but it won't log to a file.
Here is my log4j.properties file (located in the src/ folder):
#Add these properties to all CallRouter-->data-->ddlog4j.properties files at the bottom to configure the Transactions.jar logging for DB and WS messaging.
log4j.rootCategory=ALL, MAIN_LOG
log4j.rootLogger=DEBUG,console
#Defines the <logging level>, <appender> for custom logging.
log4j.category.com.ddvc.android.resource=INFO, SupportWebServices
#### Transactions.jar appender INFO setup
log4j.logger.com.ddvc.android.resource=INFO
log4j.appender.ResourceInfo=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ResourceInfo.File=C:/test/SupportWebServices.log
log4j.appender.ResourceInfo.Append=true
log4j.appender.ResourceInfo.DatePattern='.' yyyy-MM-dd HH-mm
log4j.appender.ResourceInfo.layout=org.apache.log4j.PatternLayout
log4j.appender.ResourceInfo.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss:SSS]} %p %c{1} (%M: %L) - %m%n
log4j.appender.ResourceInfo.Threshold=INFO
# Output to Console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{[yyyy-MM-dd HH:mm:ss]} %p %c{1} (%M: %L) - %m%n
log4j.appender.CONSOLE.Threshold=CONSOLE
And in my java files I have (located in src/com/ddvc/android/resource):
import org.apache.log4j.Logger;
#Path("/pulse")
#Component
#Scope("request")
public class PulseResource {
private final static Logger LOGGER = Logger.getLogger(PulseResource.class.getName());
#GET
#Path("list")
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<Pulse> postXML() {
LOGGER.info("Start: list");
}
}
Like I said I can see the logs to the Console:
INFO [2015-02-19 15:19:34,961] [http-bio-8080-exec-3] [PulseResource] [] - Start: list
But I don't see any file created or logged to in the C:\test folder. (plan to change it to ${catalina.base}\logs\SupportWebServices.log)

Try changing
log4j.rootLogger=DEBUG,console
to
log4j.rootLogger=DEBUG,console,ResourceInfo

remove this line:
log4j.rootCategory=ALL, MAIN_LOG
edit this line:
log4j.rootLogger=DEBUG,console,ResourceInfo

Related

Log4j - How can I write only my messages logs to file without other details

I use logger(Log4j) to create my file log program.
log4j.properties file:
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\WorkSpace\\ToDoList\\src\\log\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My class:
import org.apache.log4j.Logger;
#WebServlet("/Login")
public class LoginApp extends HttpServlet {
private static final long serialVersionUID = 1L;
private final static Logger logger = Logger.getLogger(LoginApp.class);
private final static HibernateToDoListDao actions = HibernateToDoListDao.getInstance();
public LoginApp()
{
super();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
{
try
{
logger.info("The user: "+ username+" connect to app.");
request.getRequestDispatcher("Index.jsp").forward(request, response);
}
catch(ToDoListDaoException e)
{
System.out.println(e.getMessage());
}
}
}
My file log:
2017-01-14 17:14:29 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2017-01-14 17:14:29 DEBUG TwoPhaseLoad:131 - resolving associations for [model.User#1]
2017-01-14 17:14:29 DEBUG TwoPhaseLoad:239 - done materializing entity [model.User#1]
2017-01-14 17:14:29 DEBUG StatefulPersistenceContext:892 - initializing non-lazy collections
2017-01-14 17:14:29 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection
How can i write only my messages logs to file without other details.
e.g. in my class only the line
"logger.info("The user: "+ username +"connect to app.")"
will write to the file.
Thanks
You need to set the package name in log4j configuration file for which you want to enable logging. Your configuration file should look like below. Remember to write your package name in 2 line -
# Root logger option
log4j.rootLogger=ERROR, stdout, file
# Write your package Name here
log4j.logger.packageName=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\WorkSpace\\ToDoList\\src\\log\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Enable TRACE in custom DFS application (DfLogger, log4j)

Summary
We're struggling with enabling log4j's TRACE level in a custom Documentum DFS application. The application runs on Tomcat 7.0 and uses Log4j 1.2.13, and is packaged as a WAR archive.
We have an appender called CustomServiceAppender that works just fine with all the other levels, i.e. DEBUG and upwards. During development we have this appender configured for the DEBUG level, and for production we change it to INFO or WARN. The logging then behaves like we expect (it limits the logging).
However we have written some code that logs at the TRACE level that we want to see in our logs. We've tried restarting, redeploying, deleting cache, checking log4j and commons-logging versions (they are 1.2.13 and 1.1.1, respectively). Also, there is no XML configuration file, and we know that our properties file is being picked up.
We use the default logger from DFS, namely the DfLogger class.
Changing the configuration - why doesn't it work?
In log4j.properties we change the following line from
log4j.logger.com.mycompany.services=DEBUG,CustomerServiceAppender
to
log4j.logger.com.mycompany.services=TRACE,CustomerServiceAppender
... which does not work! However, the following does work:
log4j.logger.com.mycompany.services=INFO,CustomerServiceAppender
What's happening here? Why can't we enable TRACE?
log4j.properties
For reference, here's our complete configuration file (identifying names altered):
#------------------- COMMON LOGGING LEVELS --------------------------
log4j.logger.com.emc.documentum.fs.rt = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.datamodel = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.services = WARN, A1, F1
log4j.logger.com.emc.documentum.fs.tools = WARN, A1, F1
log4j.logger.com.documentum.debug=ERROR, A1, F1
log4j.rootLogger=ERROR, A1
#------------------- CONSOLE LOG --------------------------
log4j.logger.com.mycompany.services=ERROR,A1
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %c - %m%n
#------------------- ALL --------------------------
log4j.appender.F1=org.apache.log4j.RollingFileAppender
log4j.appender.F1.File=E\:/Documentum/logs/dfs-runtime.log
log4j.appender.F1.MaxFileSize=10MB
log4j.appender.F1.layout=org.apache.log4j.PatternLayout
log4j.appender.F1.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %c- %m%n
#------------------- ASPECT_TRACE --------------------------
log4j.logger.com.emc.documentum.fs.tracing = DEBUG, ASPECT_TRACE
log4j.appender.ASPECT_TRACE=org.apache.log4j.RollingFileAppender
log4j.appender.ASPECT_TRACE.File=E\:/Documentum/logs/dfs-runtime-trace.log
log4j.appender.ASPECT_TRACE.MaxFileSize=10MB
log4j.appender.ASPECT_TRACE.layout=org.apache.log4j.PatternLayout
log4j.appender.ASPECT_TRACE.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] %m%n
#------------------- Customer TBO --------------------------
log4j.logger.no.customer=DEBUG,CustomerTBOAppender
log4j.appender.CustomerTBOAppender=org.apache.log4j.RollingFileAppender
log4j.appender.CustomerTBOAppender.File=E\:/Documentum/logs/CustomerTBO.log
log4j.appender.CustomerTBOAppender.MaxFileSize=10MB
log4j.appender.CustomerTBOAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomerTBOAppender.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] [%t] %c - %m%n
#------------------- Customer Service --------------------------
log4j.logger.com.mycompany.services=TRACE,CustomerServiceAppender
log4j.appender.CustomerServiceAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CustomerServiceAppender.File=E\:/Documentum/logs/CustomerService.log
log4j.appender.CustomerServiceAppender.Append=true
log4j.appender.CustomerServiceAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomerServiceAppender.layout.ConversionPattern=%d{ISO8601} %5p [MYCOMPANY-SP] [%t] %c - %m%n
****************************************************************************************

REST JAX-RS Logging

I have followed the HelloWorld example and got my fist REST done successfully. Now, I would like to add the logging capability the code. For example, I would like to have any logging frameworks (Log4j, JUL) and be able to log in the code, and have output to a file. How do I implement the logging to my REST code?
#Path("/hello")
public class HelloWorldService {
Logger log = Logger.getLogger("className");
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg) {
String output = "Jersey say : " + msg;
//for example, here. hopefully to a file
log.info("Log Jersey say : " + msg);
return Response.status(200).entity(output).build();
}
}
I am using Jersey 1.19, Tomcat 8
You can use open source Apache Log4j library. Add below dependency in your pom or download it from here.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\\logigng.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
With this configuration, all logging will be redirected to your specified log file.
Source : http://www.mkyong.com/logging/log4j-log4j-properties-examples/
How to log a message using Log4j?
private static final Logger logger = Logger.getLogger(HelloWorldService.class);
Logs messages with different priorities, for example, debug, info, warn, error and fatal. Normally, you just need to use debug or error.
//logs a debug message
if(logger.isDebugEnabled()){
logger.debug("This is debug");
}
//logs an error message with parameter
logger.error("This is error : " + parameter);
//logs an exception thrown from somewhere
logger.error("This is error", exception);
To set logger in debug mode, change this in your property file
log4j.rootLogger=DEBUG, file

Get spring messages from sysout to file using log4j

I'm trying to get Spring messages redirected to a log file.
I am able to get only some of the messages but not all of them.
This is my log4j file
log4j.rootLogger=DEBUG, stdout, file, fileDebug
log4j.logger = ERROR
log4j.category.org.springframework = DEBUG
log4j.category.org.springframework.jdbc.core = DEBUG
org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file INFO level
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.Threshold=INFO
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file DEBUG level
log4j.appender.fileDebug=org.apache.log4j.RollingFileAppender
log4j.appender.fileDebug.Threshold=DEBUG
log4j.appender.fileDebug.MaxFileSize=5MB
log4j.appender.fileDebug.MaxBackupIndex=10
log4j.appender.fileDebug.layout=org.apache.log4j.PatternLayout
log4j.appender.fileDebug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# log locations
log4j.appender.file.File=logInfo.log
log4j.appender.fileDebug.File=debug.log
What I get in my debug.log is for example
2015-04-14 11:18:16 DEBUG ConfigurationImpl:120 - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator 2015-04-14 11:18:16 DEBUG ConfigurationImpl:140 - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory 2015-04-14 11:18:16 DEBUG ConfigurationImpl:154 - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy15 2015-04-14 11:18:16 DEBUG ValidationXmlParser:88 - Trying to load META-INF/validation.xml for XML based Validator configuration. 2015-04-14 11:18:16 DEBUG ValidationXmlParser:95 - No META-INF/validation.xml found. Using annotation based configuration only.
But I don't see there anything from jdbcTemplate but it is in my console.
11:19:19.673 [http-bio-8080-exec-6] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
So this should be debug level for org.springframework.jdbc.core.JdbcTemplate, but that doesn't seem to do anything.
I am probably missing something obvious but I just can't see it... Thanks for advice.
You need to Change following lines:
From
org.springframework.jdbc.core.JdbcTemplate = DEBUG
org.springframework.jdbc.core.StatementCreatorUtils = DEBUG
To
log4j.logger.org.springframework.jdbc.core.JdbcTemplate = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.JdbcTemplate=false
log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils = DEBUG, fileDebug
log4j.additivity.org.springframework.jdbc.core.StatementCreatorUtils=false

Log Level per appender for a single Logger

Is it possible to configure different log levels for a single Logger based on the appender?
I realize this is similar to this question, and this is as far as I had already got myself, but the problem with this is that the threshold applies to all loggers that log to that appender, whereas I only want the threshold to apply to a single logger.
i.e. So far I have something like this:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=WARN
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.logger.mylogger=DEBUG,logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern=${roll.pattern.daily}
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %C{2} - %m%n
log4j.appender.logfile.File=mylogfile.log
I want mylogger DEBUG messages to be send to the logfile appender, but I also want mylogger INFO messages to be sent to the stdout appender (but for all other loggers only WARN ings). Using the Threshold to limit stdout to WARN restricts the output of mylogger.
Aha, I fixed it by changing
log4j.appender.stdout.Threshold=WARN
to
log4j.appender.stdout.Threshold=INFO
Should have been more careful first time round.

Categories