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
Related
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"
I'm running Mac OS X 10. I just did brew install zookeeper.
Then I created /usr/local/etc/zookeeper/zoo.cfg based on /usr/local/etc/zookeeper/zoo_sample.cfg.
Then zkServer start works just fine.
But, when trying to connect to Zookeeper from Clojure, which uses the Zookeeper Java client, I get this error:
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My log4j.properties file:
log4j.rootCategory=WARN, zklog
log4j.appender.zklog = org.apache.log4j.FileAppender
log4j.appender.zklog.File = /usr/local/var/log/zookeeper/zookeeper.log
log4j.appender.zklog.Append = true
log4j.appender.zklog.layout = org.apache.log4j.PatternLayout
log4j.appender.zklog.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
So, my questions are:
What is a reasonable log4j configuration for my situation?
What could homebrew do, out of the box, to prevent this warning from happening?
To be clear, there are two log4j.properties files involved. One is created by Homebrew and gets written to /usr/local/etc/zookeeper/log4j.properties. This file is not the cause of the error message above.
The other log4j.properties file is particular to your (my) application. So, to answer part 1 of the question, create a log4j.properties file on the classpath of the Clojure app, such as in the src directory.
log4j.rootLogger=WARN, A1
log4j.logger.user=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p %c: %m%n
log4j.logger.org.apache.zookeeper=WARN
I built this by starting with https://github.com/clojure/tools.logging and adding the last line.
To answer part 2, Homebrew does not and should not have anything to do with how a Clojure application sets up its logging.
I have created a very simple application, where I am trying to use the Log4J, but my application is not logging any log.
Can anyone please tell me how can I debug the same as my log4j started or not?
I have kept the file in classes folder of WEB-INF/classes
Thanks
following is my log4j.properties
log4j.rootLogger=debug, stdout, ABC
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.ABC=org.apache.log4j.RollingFileAppender
log4j.appender.ABC.File=D://abc//dams_workflow_application.log
log4j.appender.ABC.MaxFileSize=3000KB
# Keep one backup file
log4j.appender.ABC.MaxBackupIndex=10
log4j.appender.ABC.layout=org.apache.log4j.PatternLayout
log4j.appender.ABC.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
following is the sample line for using log 4j..
Logger log = Logger.getLogger("ABC");
log.info("my message");
If you're writing a web application, you can view the Log4J Default Initialization Under Tomcat. Even it states Tomcat, the same applies in other Containers/Web application servers.
Console will print this if log4j is not started :
log4j:WARN No log4j configuration information found.
log4j:WARN Changed non-configured level from DEBUG to ERROR.
log4j:WARN The log4j system is not properly configured!
You could have a look at Log4j - Looking for a good 'Getting started' tutorial or blog
to get started.
Regards,
Stéphane
Make sure the log4j jar is in the lib directory for your webapp or in the lib directory in tomcat.
Log4j doesn't seem to work properly for me unless i put the log4j properties file in the default package in the webapp's source directory within the project.
All of my log4j properties files typically look similar to the following
# Define a logger called "processLog" using the FileAppender implementation of the
# Log interface
log4j.appender.processLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.processLog.File.DateFormat='.'yyyy-ww
# Define the output location
log4j.appender.processLog.File=C:/logs/myapp.log
# Define what the output is going to look like for your log
log4j.appender.processLog.layout=org.apache.log4j.PatternLayout
log4j.appender.processLog.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5p %c{1}: Line#%L - %m%n
# log4j.rootLogger specifies the default logging level and output location.
# The first parameter is the level (debug > info > warn > error > fatal).
log4j.rootLogger=INFO, processLog
That's how I do it anyway. There may be a better way but this one always works for me.
I am using Windows 2008 R2 and Apache Tomcat 5.5, for your information.
STDOUT and STDERR can be automatically logged through Apache Tomcat properties, via Logging tab -> Redirect Stdout and Redirect Stderror textboxes.
But I want to control this through log4j.
I'm trying to leverage ConsoleAppender and the TimeAndSizeRollingAppender class to rollover what would normally be controlled by Apache Tomcat's innate logging.
Basically, however Tomcat redirects stdout and stderr to a file, I want to do the same thing using log4j and the log4j.properties file.
I did some digging on this site and others like this one ( log4j redirect stdout to DailyRollingFileAppender) and this one ( http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender#comment-749), but I am running into a few fundamental issues:
How do I compile any of these classes? What should be the classpath? I tried compiling the class from the sysgears link but it returned 7 errors such as unable to find symbol Class Logger and symbol Class OutputStream.
After compiling, specifically from the sysgears link, how can I use the class? It states to use the class, just write:
System.setErr(new PrintStream(new LoggingOutputStream(
System.err, Logger.getLogger("outLog"), Level.ERROR)));
Great, but where do I write it? In a separate file? Tomcat Properties?
I would appreciate any help.
Thank you very much for your time.
Update:
Here is my full log4j.properties file:
log4j.rootLogger=INFO, CATALINA, LOCALHOST, MANAGER, HOST-MANAGER, ADMIN, CONSOLE
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.CATALINA.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat- 5.5.28/logs/catalina.log
log4j.appender.CATALINA.Threshold=DEBUG
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.MaxFileSize=20KB
log4j.appender.CATALINA.DateRollEnforced=true
log4j.appender.CATALINA.MaxRollFileCount=100
log4j.appender.CATALINA.ScavengeInterval=-1
log4j.appender.CATALINA.BufferedIO=false
log4j.appender.CATALINA.CompressionAlgorithm=ZIP
log4j.appender.CATALINA.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n
log4j.appender.LOCALHOST=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.LOCALHOST.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/localhost.log
log4j.appender.LOCALHOST.Threshold=DEBUG
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.LOCALHOST.layout=org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.MaxFileSize=20KB
log4j.appender.LOCALHOST.DateRollEnforced=true
log4j.appender.LOCALHOST.MaxRollFileCount=100
log4j.appender.LOCALHOST.ScavengeInterval=-1
log4j.appender.LOCALHOST.BufferedIO=false
log4j.appender.LOCALHOST.CompressionAlgorithm=ZIP
log4j.appender.LOCALHOST.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n
log4j.appender.MANAGER=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.MANAGER.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/manager.log
log4j.appender.MANAGER.Threshold=DEBUG
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.MANAGER.MaxFileSize=20KB
log4j.appender.MANAGER.DateRollEnforced=true
log4j.appender.MANAGER.MaxRollFileCount=100
log4j.appender.MANAGER.ScavengeInterval=-1
log4j.appender.MANAGER.BufferedIO=false
log4j.appender.MANAGER.CompressionAlgorithm=ZIP
log4j.appender.MANAGER.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.HOST-MANAGER.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/host-manager.log
log4j.appender.HOST-MANAGER.Threshold=DEBUG
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.HOST-MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.MaxFileSize=20KB
log4j.appender.HOST-MANAGER.DateRollEnforced=true
log4j.appender.HOST-MANAGER.MaxRollFileCount=100
log4j.appender.HOST-MANAGER.ScavengeInterval=-1
log4j.appender.HOST-MANAGER.BufferedIO=false
log4j.appender.HOST-MANAGER.CompressionAlgorithm=ZIP
log4j.appender.HOST-MANAGER.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n
log4j.appender.ADMIN=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.ADMIN.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/admin.log
log4j.appender.ADMIN.Threshold=DEBUG
log4j.appender.ADMIN.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.ADMIN.layout=org.apache.log4j.PatternLayout
log4j.appender.ADMIN.MaxFileSize=20KB
log4j.appender.ADMIN.DateRollEnforced=true
log4j.appender.ADMIN.MaxRollFileCount=100
log4j.appender.ADMIN.ScavengeInterval=-1
log4j.appender.ADMIN.BufferedIO=false
log4j.appender.ADMIN.CompressionAlgorithm=ZIP
log4j.appender.ADMIN.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
INFO, HOST-MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/admin]=\
INFO, ADMIN
1) How do I compile any of these classes? What should be the classpath? I tried compiling the class from the sysgears link but it returned 7 errors such as unable to find symbol Class Logger and symbol Class OutputStream.
You need to have the log4j jar on your classpath, and import the correct classes at the top of your file. Something like,
import java.io.PrintStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;
you can achieve this with an appender:
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n
Then use this appender with your logger, for example:
log4j.rootLogger = DEBUG, stdout
Of course you can use the Target above different, maybe with System.err
Hope it helps.
Cheers,
Christian
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}"