How to print log on Tomcat logs folder - java

I am using Log4j to print logs. Its successfully print the logs on spring console, but i need to check, how its printing them on catalina. Normally on server we write this tail -f /var/log/tomcat8/catalina.out, but how to view this on local machine.
This is what i am doing to print the logs
logger.info("Log4J - "+ "Request: " +
",URL= "+ httprequest.getRequestURI()+
",requestId= "+ uniqueID+ ",Headers= "+ map);
Kindly guide me how and where to check catalina logs.
I have tried to run Tomcat8.5 server from cmd. It starts the server, and put some logs in tomcat/logs folder, but nothings is related to the application. When i open the service from browser, it does not add anything in the log.
This is what it writes in catalina.2017-03-19
19-Mar-2017 21:08:38.766 INFO [main]
org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler
[http-nio-8080] 19-Mar-2017 21:08:38.771 INFO [main]
org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler
[ajp-nio-8009] 19-Mar-2017 21:08:38.772 INFO [main]
org.apache.catalina.startup.Catalina.start Server startup in 1092 ms
EDIT
This is my properties file, it prints the logs in the file mentioned here.C:\\log\\logging.log
# Root logger option
log4j.rootLogger=INFO, 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
# Redirecting to Tomcat Logger
log4j.appender.file.File=${catalina.home}logs/logging.log
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\logging.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

Check application log. logging.properties file for more details.
Log write output in catalina.out if application log is not configured.
Where is your logging.properties file located? It should be available in the root of the classpath. As a sanity check, what does the following code print?
System.out.println(getClass().getClassLoader().getResource("logging.properties"));
If the code is in a static context, use
System.out.println(ClassName.class.getClassLoader().getResource("logging.properties"));

Related

log4j to write log to console and also to file

I use Selenium WebDriver and log4j in my Java project. I set log4j.properties as follows:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:/_privat/myproj/log/log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
# 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 [%-20c{1}:%3L] %m%n
log4j.appender.stdout.Threshold=INFO
During execution I see only these lines instead of full log.
Starting ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770#{#40}) on port 3320
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
jún. 14, 2019 8:26:53 DE org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
When execution is stopped I get all the log messages also to console, which was written to file. I need to see log also in console during execution.

Why wasn't log message written my log file when running Junit tests?

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.

how to seprate the jetty.logs from solr.logs?

I using jetty as my container and I set up solr inside it, I using log4j.properties for my logging,when I used jetty standalone I can see my jetty logs ,but when I using solr an d log4j I only see solr.log file that all my jetty and solr logs are mixed in it,and my jetty.logs file created at the same time but it is empty,how can I separate the solr and jetty logs?
this is my log4j.properties:
# Logging level
solr.log=logs
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x [%X{collection} %X{shard} %X{replica} %X{core}] \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; [%X{collection} %X{shard} %X{replica} %X{core}] %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
#set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
This is how you can separate Solr and Jetty logs and write them into separate files using additivity:
log4j.additivity.org.apache.solr=false
log4j.additivity.org.eclipse=false
log4j.logger.org.apache.solr=INFO, SOLR
log4j.logger.org.eclipse=INFO, JETTY

log4j FileAppender not writing in Linux

In a standalone application I'm using log4j for logging and have configured it with the following properties file:
# Root logger option
log4j.rootLogger=WARN, file, stdout
log4j.logger.com.tr = INFO
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=log4j.log
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
In Windows this works as expected, producing a log4j.log file in the current working directory. When I run the application in Linux, it reports no error but does not generate the log file. The logger outputs to console just fine.
I tried programatically changing the File property from a relative path to an absolute path and that changed nothing.
I've also verified that if I supply an invalid file name the tool reports an error saying the file cannot be generated. So I'm baffled as to why no error is being reported and no file is being generated.
I've searched my hard disk for the file to verify it's not being generated elsewhere, and it's not.
Any help here would be much appreciated.

Log4j in tomcat not showing logs

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

Categories

Resources