log file not getting generated - java

i have a batch file from which i am invoking some of the stuff and also the log
set LOG4J="-Dlog4j.debug -Dlog4j.configuration=%DIR%\log4j.properties"
java %LOG4J% -Dwvs.some.properties=%DIR%\xyz.properties %DEBUG% -PORT %PORT% -HOST %HOST% -TYPE %TYPE% -CMDCLASS %CMDCLASS% >stdout.log 2>&1
the port, host, cmdclass,DIR, type have proper values and this is my log4j.properties file, i am on windows machine.
# Set root logger to output only ERROR and FATAL events to R appender
log4j.rootLogger=ERROR,R
# Define R appender to output to local log
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:/logs/sample.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c - %m%n
on D:\logs , nothing gets generated, i am not able to figure out why.

log4j properties are case sensitive
log4j.appender.R.File
should be log4j.appender.R.file

This worked by changing this :
set LOG4J="-Dlog4j.debug -Dlog4j.configuration=%DIR%\log4j.properties" to
set LOG4J="-Dlog4j.configuration=%DIR%\log4j.properties"

Related

Moving log4j.properties out of jar causing log4j warnings (logs are not generated)

I have a shell script test.sh with following contents :
echo " Running test.sh ..." | log
test_cp=./test/jar/*:./test/lib/*
test_main=com.test.TestApp
java -cp $test_cp $test_main | log
The relative paths used in the above file are correct as far as I understand. In this case, log4j jar is located at ./test/lib/ and the jar containing TestApp (test-app.jar) is located at ./test/jar/.
Now, the current scenario is that log4j.properties is packaged inside test-app.jar and all Java classes are using the log4j logger to create logs. The contents of log4j.properties are as follows :
log4j.rootLogger=ALL, CONSOLE, FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p [%t] %c %M - %n%m%n
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=test-app.log
log4j.appender.FILE.Append=false
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p [%t] %c %M - %n%m%n
We want to externalize the log4j.properties and move it outside the test-app.jar. For this, I have placed log4j.properties at ./test/properties/ and modified the shell script accordingly as follows :
echo " Running test.sh ..." | log
test_cp=./test/properties/*:./test/jar/*:./test/lib/*
test_main=com.test.TestApp
java -cp $test_cp $test_main | log
But I am getting the following error and no log file is generated, nor any logs get pronted on console.
log4j:WARN No appenders could be found for logger (com.noknok.util.PropertiesLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I also read somewhere on the Internet that this could be a Java classloader issue, so I have tried changing the sequence of different locations in classpath, placing log4j.properties in the directories that are already included in the classpath, etc.
But I am still getting the same error.
Thanks a lot for the help! Please get back if you need any other info.
Make sure log4j picks the correct log4j.properties file by using -Dlog4.debug
You can specify the config file by -Dlog4j.configuration=/path/to/file

How to log stdout output in Tomcat?

Is there a way to log all stdout output to the catalina.log file in Tomcat? (i.e. everything that gets printed to System.out.println())
The console window that opens when you run TOMCAT/bin/startup.bat displays output from stdout, but it's not saved to TOMCAT/logs/catalina.<date>.log.
My specific problem is that I have a console appender defined in log4j to output to the console. These log messages appear correctly in the Tomcat console window, but they are not written to catalina.log. I'm running Tomcat 5.5 on Windows. Thanks.
EDIT:
Here is my log4j.properties file. It is located at TOMCAT/webapps/app/WEB-INF/classes/log4j.properties:
log4j.rootCategory=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ABSOLUTE} %-5p %c{1}]: %m%n
I've come across similar questions before, and haven't found a way to do this by logging System.out in Windows unless you are running Tomcat as a Windows service. This seems to work by default in Unix since startup.sh points to catalina.sh which logs stdout to the catalina.out file like below
org.apache.catalina.startup.Bootstrap "$#" start >> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
In log4j, ConsoleAppender by itself does not append to a File, only to System.out
However, I've modified your log4j properties to add a FileAppender and this config works, but of course this logs into a separate log file.
New config
# Set root logger level to DEBUG.
log4j.rootLogger=DEBUG, console, myFile
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ABSOLUTE} %-5p %c{1}]: %m%n
# myFile writes to file
log4j.appender.myFile=org.apache.log4j.RollingFileAppender
log4j.appender.myFile.File=logs/tomcatlog4j.log
log4j.appender.myFile.MaxFileSize=100KB
log4j.appender.myFile.layout=org.apache.log4j.PatternLayout
log4j.appender.myFile.layout.ConversionPattern==[%d{ABSOLUTE} %-5p %c{1}]: %m%n
Output
=[15:24:03,819 INFO A1]: In my.jsp
=[15:24:03,975 INFO A1]: Out of my.jsp
=[15:24:04,880 INFO A1]: In my.jsp
=[15:24:04,880 INFO A1]: Out of my.jsp
also see
How to log exceptions from a specific package deployed in tomcat
log select events into a separate file
https://serverfault.com/questions/201178/tomcat-5-5-how-to-redirect-the-logging-output-to-one-file-per-web-application
Did you checked, whether the log4j.properties file can be found from your application?
Maybe you can check, by setting a hardcoded file path like
-Dlog4j.configuration=file:///C:\Dev\log4j.properties
If the logs are written after these change, the relativ path to the log4j file is wrong.
If I look at the default logging config of tomcat 5.5 in logging.properties:
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
That looks to me as if stdout of web applications might be logged to files only for level INFO and above, regading that http://tomcat.apache.org/tomcat-5.5-doc/logging.html states that in tomcat JULI logging configuration loggers do not use parent's handlers when they are assigned their own handlers. Also the file should be prefixed localhost and not catalina. But then I do not understand how the output comes to your output window :/

Java Service Wrapper and log4j

I am using the Java Service Wrapper to create a Windows Service from a Java Program.
Everything works fine, except the wrapper does not log to a file (I'm using log4j). The logging works properly when the project does not run as a service.
This is not a Log4J problem, because I can log to console with success (this gets redirected to the wrapper's log file), but is not what I'm attempting to achieve.
Here's my wrapper's config file:
encoding=UTF-8
#include ../conf/wrapper-license.conf
wrapper.lang.folder=../lang
# Java Configuration.
wrapper.java.command=java
wrapper.java.command.loglevel=INFO
wrapper.logfile.rollmode=NONE
wrapper.java.mainclass=servicewrapper.MainServiceWrapper
wrapper.java.classpath.1=../lib/*.jar
wrapper.java.classpath.2=../lib/classes/*.jar
wrapper.java.library.path.1=../lib
wrapper.java.additional.auto_bits=TRUE
wrapper.app.parameter.1=servicewrapper.MainServiceWrapper
# Logging Configuration
wrapper.console.format=PM
wrapper.logfile=../logs/wrapper.log
# Service Configuration
wrapper.name=myproject
wrapper.check.deadlock=TRUE
wrapper.check.deadlock.interval=10
wrapper.check.deadlock.action=RESTART
wrapper.check.deadlock.output=FULL
wrapper.console.title=myproject
wrapper.ntservice.dependency.1=
wrapper.ntservice.starttype=AUTO_START
wrapper.ntservice.interactive=false
Also here's my Log4J config file:
log4j.rootCategory=INFO, R
log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL
log4j.logger.org.hibernate=ERROR
log4j.logger.org.hibernate.type=ERROR
log4j.logger.com.mchange.v2.c3p0=ERROR
log4j.logger.myproject=DEBUG
#------------------------------------------------------------------------------
#
# The following properties configure the console (stdout) appender.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] %p %c{1} %m%n
#------------------------------------------------------------------------------
#
# The following properties configure the Daily Rolling File appender.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.R = org.apache.log4j.RollingFileAppender
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.File = myproject.log.txt
log4j.appender.R.Append = true
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] %p %c{1} %m%n
I was not clear on the problem. To confirm. When you run as a service, log4j output that is directed to the console correctly gets written to the Wrapper's log file? ../logs/wrapper.log Correct?
So the problem is that your log4j output that is supposed to go to myproject.log.txt is not showing up. Is that the problem you are having?
If so then it actually is a log4j side issue as that would be entirely within the JVM.
From your configuration you are trying to write to .¥myproject.log.txt which would be in the same directory as the Wrapper binary. If it was an access problem to the file then I would expect some kind of log4j error in the console, which would then be visible in the wrapper.log file.
To put the log4j file in with the Wrapper's log file you would want to do:
log4j.appender.R.File = ../logs/myproject.log.txt
Please confirm which file you are not able to write to. Also what version of the Wrapper and Windows are you running?
Cheers,
Leif
A possible cause is that you forgot to set -Dlog4j.configuration={path to file} in your wrapper configuration. For example:
wrapper.java.additional.1=-Dlog4j.configuration=/home/logs.log

log4j append=false does not work for me... why?

I have the below configured for log4j which outputs a csv log file. Every time my program executes I wish to start this log file a fresh by overwriting not appending to the log file. I thought I could achieve this by using the append=false. I know that I have correctly set up log4j as other logs are outputting fine but these are daily rolling logs that are appending which is the desire affect.
Can anyone tell me why the append=false doesn't seem to work. Is there another setting I've missed?
Here's my config code:
#Image output
log4j.logger.fetch.FetchDirectHolidays=debug, S
log4j.appender.S=org.apache.log4j.FileAppender
log4j.appender.S.File=xml\\logs\\FetchDirectHolidays.csv
log4j.appender.S.append=false
# Keep one backup file
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern= %p , %m%n
What is wrong with my configuration?
I forgot to state that my application is scheduled and I have just read that the Append=false only clears the log file if the whole application is shutdown and restarted. This does not help as I need to clear this log file each time the internal processes executes.
Try
log4j.appender.S.Append=false
with a capital A for Append
# Define the root logger with appender file R
log4j.rootLogger = INFO, FILE,stdout
# Define the file appender (File)
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:/user/FileName.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# 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=[%5p ] - %m%n
# Direct log messages to stdout (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=[%5p ] - %m%n

How do I log from inside my web application in Tomcat 6

How do I log from within my web application deployed on Tomcat 6? Where should I expect the logging output to go (internal tomcat log files, or will another logfile be generated)? I see a ton of documentation but am having a hard time finding a direct answer to the above questions. Where should I expect the logging to show up (currently it is log4j is not generating a log file and it is not showing up in my console). I am trying to follow http://www.laliluna.de/articles/log4j-tutorial.html .
### 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{ABSOLUTE} %5p %c{1}:%L - %m%n
### file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=test.log
log4j.appender.file.threshold=info
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
In my application I define a log object:
private static org.apache.log4j.Logger log = Logger.getLogger(MyClass.class);
log.error("LOGGING!");
Thanks for the help.
2 things to try:
1: Change test.log to /tmp/test.log so you know exactly where the file is supossed to be.
2: Put your log4j.properties config file in your apache-tomcat-6.0.x/lib directory together with the log4j-1.2.15.jar file. And don't have any log4j files in your webapps/*/WEB-INF/lib
That's the way I am doing it, and its working for me. Here is a usefull snippet from my log4j.properties (Remember to do a mk /tmp/logs if you use this config)
log4j.rootLogger=debug, root
log4j.appender.root=org.apache.log4j.FileAppender
log4j.appender.root.layout = org.apache.log4j.PatternLayout
log4j.appender.root.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.root.file = /tmp/logs/root.log
log4j.appender.root.append = true
log4j.category.mside = DEBUG,msideAppender
log4j.category.javashare = DEBUG,msideAppender
log4j.additivity.mside = false
log4j.additivity.mside.msideAppender = false
log4j.additivity.javashare = false
#Define msideAppender.
log4j.appender.msideAppender = org.apache.log4j.RollingFileAppender
log4j.appender.msideAppender.MaxFileSize=10MB
log4j.appender.msideAppender.MaxBackupIndex=7
log4j.appender.msideAppender.file = /tmp/logs/mside.log
log4j.appender.msideAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.msideAppender.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.msideAppender.append = true
IIRC Tomcat v4/v5 sends standard output to the catalina.out file, so any log4j output using a console appender would go to that file as well. Not sure if this still the case with newer versions of Tomcat, though.
Your log4j configuration will be picked up by log4j if you put it into the classpath of your web application, e.g. in WEB-INF/classes/. Make sure that your log4j.jar is in WEB-INF/lib.
The output of the ConsoleAppender that you defined, which is logging on stdout, will go to ${CATALINA_BASE}/logs/catalina.out, as any Tomcat stdout output.
As to the RollingFileAppender, you should define the correct path. If you want your web application's logs to appear in Tomcat's logs directory, change the file for this appender to:
log4j.appender.file.File=${catalina.base}/logs/test.log
BTW, use the system property -Dlog4j.debug system property. That should tell you where the heck log4j is sending its output.
Also, if your tomcat install is in a *nix system, or if you are running on Windows with cygwin installed, you could use the find command to detect what files get changed right after you send a HTTP request to Tomcat (which you know should produce a logging output)
cd <your tomcat install>
ls -ltr `find . -type f -ls` | tail -10
That should show you the last 10 files that were updated or changed. It won't work if there are files in your app with spaces in their file names, though.
I have tried following way and make sure it works well:
put your own log4j.properties place at a path;
then update your catalina.sh under tomcat, add below similar line:
JAVA_OPTS="-Dlog4j.configuration=file:${yourownlog4jpath}"

Categories

Resources