I recently followed this tomcat article to change my stdout log appearance while using log4j. I wanted a time stamp for each action a user takes. After following the article, I am still not receiving the desired result.
The tomcat 8 stdout log looked like:
(action.BaseAction 39 ) Calling Action
/editBlaBlaBla by: System Administrator
However I wanted to add a timestamp to each action a user takes. IE:
(action.BaseAction 2015/07/26 15:46:12 39 ) Calling Action
/editBlaBlaBla by: System Administrator
After adding the necessary jars (tomcat-juli, tomcat-juli-adapter, and log4j) to Tomcat 8.0\lib directory and adding a log4j properties file beside my logging.properties in Tomcat 8.0\conf directory.
My stdout logs look like:
2016-02-26 10:38:18 Commons Daemon procrun stdout initialized running
ControllerServlet init
Running ControllerServlet tempInit
There are no entries corresponding to user actions. My log4j.properties file:
log4j.rootLogger=stdout
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 [%-6p] %c{1} - %m%n
This is how the logger is initialized in my java action classes
import org.apache.log4j.Logger;
private static final Logger logger = Logger.getLogger(MYCLASSHERE.class);
How do I go about getting the time stamp without redoing the entirety of my java logging statements?
Try this.
log4j.appender.stdout.layout.ConversionPattern=%-d{MMM dd yyyy HH:mm:ss} %-5p %C{1} %M %m%n
And your output will be like
Feb 22 2016 15:52:22 INFO ClassName MethodName StringInLogDotInfo
Related
I have a class Store which contains statements like "log.info(...)". I can see the log messages with the log file created when running the program. However, when I run JUnit tests, the log file won't be created, but I still want to see log messages. The logger is:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
And my log4j properties:
log4j.rootLogger=DEBUG, STDOUT, file
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylogs.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
The questions is, how to also create the log file and log messages when running my Junit tests?
Edit:
Part of the test log property:
log4j.rootLogger=ERROR, Console
log4j.logger.play=DEBUG
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=testlog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
According to that configuration, you'd probably expect to have a testlog.log file in the root of your project.
Nonetheless, the file appender is not referenced so the messages will only be logged using the Console appender. However, your rootLogger is set to ERROR level so messages below this level, such as DEBUG, INFO or WARN will be discarded.
You can either change your rootLogger configuration, from which all non-explicitly configured loggers inherit, just like in your other log4j.properties, or define a logger for your package, setting it to DEBUG (or whatever level you want) and use both the appends: log4j.logger.my.package.here=DEBUG, Console, file.
Further reading resources:
- log4j v1.2 into
- log4j 1.2 configuration
P.S.: Please note that sometimes Eclipse has issues with the file-system changes and does not automatically update the project files. If that happens. after running your test, try right clicking your project in the Navigator view, and selecting refresh, then scroll to the end where your pom is. Alternatively open the file explorer of your choice, go to your project root on the disk and you should see the testlog.log file.
running my selenium suite with Intellij IDEA I'm facing this issue:
I configured correctly log4j using this properties file
# Root logger option
log4j.rootLogger=INFO, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c{1} : %L] - %m%n
logs are following the pattern but are written ALL IN ONE LINE in this way:
2016-05-03 11:34:05,612 INFO [CreateTicketPage : 68] - clicked on save button2016-05-03 11:34:05,627 INFO [TicketDetailsViewPage : 27] - Ticket Details View.page.successfully.loaded2016-05-03 11:34:07,452 WARN [AddTransactionPage : 44] - Add Transaction.page.successfully.loaded2016-05-03 11:33:52,032 INFO [TestRunner : 25] - executing TestRunner.setup
%n seems ignored!! but running the tests using just mvn clean install in the command line everything look perfect. any suggestion?? is this an Intellij related issue??
This is my log4j.properties.
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}\MyLog\PmcDemo.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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
# Direct log messages to stdout
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
I am using tomcat 6.0, in my application I have used Logger from log4j yet I don't see any output on server console or in the log file. My application is using struts2 as front end, Spring framework as middle layer and hibernate as the end layer. I don't see my application logging how can I enable it in tomcat 6?
You need to switch the backslashes for forward slashes:
${catalina.home}/MyLog/PmcDemo.log
or to escape them
${catalina.home}\\MyLog\\PmcDemo.log
If that doesn't help, let us know the structure of your project and where the log4j.properties file is stored.
Try this steps,
If running Tomcat 6.x:
1. If you have not already done so, modify the <<TOMCAT_HOME>>/conf/catalina.properties file so that the shared classloader mechanism work the same as Tomcat 5.x.
2. To do this, verify that the entry beginning with shared.loader= reads shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar If running Tomcat 5.x or higher:
If running Tomcat 5.x or higher:
3. If it does not already exist, create a "shared/classes" directory under <<TOMCAT_HOME>>.
4. If it does not already exist, create a "shared/lib" directory under <<TOMCAT_HOME>>.
5. Copy log4j-###.jar into <<TOMCAT_HOME>>/shared/lib.
**Note:** Any specific version of log4j-###.jar should work. You can download the stable log4j version 1.2 installation from http://logging.apache.org/log4j/1.2/download.html
6. Copy a log4j.properties file into <<TOMCAT>>/shared/classes.
Example
To get a log file named "initiate.log" to show up in the <<TOMCAT_HOME>>/logs directory, an initial version of log4j.properties file is:
log4j.rootLogger=ERROR, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/initiate.log
log4j.appender.R.MaxFileSize=1000KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} 5-5p %c{2} - %m %n
log4j.logger.org.springframework=ERROR
log4j.logger.org.springframework.web.servlet.mvc=ERROR
#set to DEBUG to see hibernate and connection-pool logging output
log4j.logger.org.hibernate=ERROR
log4j.logger.org.apache.commons.dbcp=ERROR
#set to DEBUG to see Initiate-specific logging output
log4j.logger.com.initiatesystems=DEBUG
#set to DEBUG to set Initiate-specific verbose logging output
log4j.logger.verbose=ERROR
Quoted from: http://pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.datatrust.doc%2Ftopics%2Ft_datatrust_configuring_log4j_logging_apachetomcat.html
My log4j properties file looks like this:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.category.org.springframework.web=INFO
log4j.category.org.springframework.samples.mvc31=INFO
When I run my app in embedded jetty by mvn jetty:run I see the logger info from my code, stuff like : logger.info(myobj)
However, when I package the app using mvn package and deploy it to tomcat, I don't see the logs.
Is there something I'm missing?
There's no console in Tomcat, so by default your console output is dumped to a file named catalina.out. Do you not have that file?
I am running REST web service on Tomacat 6.0.32. I am using log4j API to log errors or anything else.
Basically you only need to include this line of code
static Logger logger = Logger.getLogger(TestLogging.class.getName());
logger.debug("String blah blah");
I run main method to test if it is working. So, the program writes log info into text file and on the console of NetBeans.
But if you deploy the project and then run it on the browser those logs do not appear no where; neither on console nor text file nor on Tomcat's logs.
The following is properties file:
# Set root category priority to DEBUG and set its only appender to A1
log4j.rootCategory=DEBUG, A1, file
log4j.additivity.logger=false
# A1 is set to be a ConsoleAppender (writes to system console).
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p [%t] %F %L %x - %m%n
#### Second appender writes to a file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=eMart_logger.log
# Control the maximum log file size
log4j.appender.file.MaxFileSize=1MB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%p %d %t %F %L - %m%n
What is the problem?
Cheers
Use a FileAppender to get it to log to a file when you are running on a server.
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html