codesmuch
codesmuch

Reputation: 21

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

Upvotes: 2

Views: 7023

Answers (2)

Kiet Thanh Vo
Kiet Thanh Vo

Reputation: 399

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

Upvotes: 1

Leif Mortenson
Leif Mortenson

Reputation: 194

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

Upvotes: 1

Related Questions