4

I did an application that makes use of log4j. Everything works fine, however, when I make a jar of that application and attach it to another application the logging stops working (no log file is created). I think the problem is that this last application also includes another jar (besides mine) that already use log4j. By the way, this other jar is hadoop, and I think it is taking the log context. My log4j properties file is in classpath, as well as in the root of the jar:

log4j.logger.a.b.c=DEBUG, A1

log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File=my-log.log log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

What can I do to sort this out?

Thanks.

4

4 回答 4

6

Firstly, what I can think of is that may be HBase's or Hadoop's log4j.properties is shadowing your log4j.properties file that's in your resources directory as, HBase is using it's own log4j.properties file first already in the classpath.

In the log4j.properties used by HBase or Hadoop, you probably don't have your 'A1' appender set which can actually log to 'my-log.log'

Secondly, If you are not configuring log4j using PropertyConfigurator.configure(path to your customLog4j.properties); at the beginning of your application, then it's more likely that log4j.properties of the HBase or Hadoop is being picked up.

Finally, what I can suggest to experiment, if you already have not tried with PropertyConfigurator.configure(), is to rename your application specific log4j.properties file to something else say mylog4j.properties and try using that in PropertyConfigurator as PropertyConfigurator.configure(mylog4j.properties);

于 2013-11-13T19:45:52.700 回答
1

You say you are using Hadoop. Are you running this on an HDFS? If so Hadoop is configured to use the log4j properties file located on HDFS. So you would need to update that log file.

In general, log4j.properties contained within jar files are ignored. This is because it is the driving application that should have control of what logging it wants done regardless of what logging the libraries that it uses want. If I am creating an app I don't have a log file created for every library I use. I use a single log4j config file and from there can control the logging of all libraries.

于 2013-11-01T16:53:18.087 回答
0

as you said the application which you are creating has log4j, that doesnt matter when you are making it into jar, because jar wont have your log4j while devloping , once you convert it to jar. all you need to know aware is , that you need use logger same as the application which your jar will be added as dependency.

let us if it war and your jar will be part of it, then you jar will use the log4j property which war uses. so you need to check whether you use same log4j which your target application uses, and also check the log property file and it logger file, you may get your jar logs there may be, i mean the different log file location than what you have configure in you jar log4j file property

于 2013-11-14T08:19:27.063 回答
0

Using PropertyConfigurator.configure(); should solve your problem. Make sure that you use it while the application is loading the configuration.

于 2013-11-15T10:48:14.557 回答