Log4J does not log anything
Asked Answered
S

6

12

I just picked up an existing web application at work that is supposed to log its activity using Log4J. I've configured my workspace exactly as I was told to and everything else (db connection, authentication, etc...) works fine except that the nothing is being written to the log file. Other similar applications have no problem logging.

I've looked at the WebSphere console when the application starts up and there are no errors there that might indicate why Log4J isn't logging.

I mentioned this to another dev (who once worked on this application, but not for a while and is more out of date than I am on it) who remarked that it was very strange behaviour, but had no idea why it might fail to log and not report any errors.

I've gone over the config file and properties file and everything looks OK. I suspect that Log4J is never even reading the log4j.xml but I'm not certain of that. It's been a while since I've worked with Log4J - does anyone have some good tips on trouble-shooting this type of problem?

PS: There are instances of this application that are deployed to various test/QA/prod servers and these instances all log fine. It's only on local workstations that logging seems to silently fail.


Update: So it does seem to be a problem with the way the application is being deployed. I changed the classloader mode to "Parent Last" and I can see that the Log4J file is at least being read now. And the first action I attempt triggers a ClassNotFoundException saying that org.apache.commons.logging.impl.Log4jFactory cannot be found.


2nd Update: I've noticed something stranger... The application has two WAR projects - one of them is for the UI and the other is for some web services. The project that is for the UI is successfully logging its operations to the log file. The web service project is the one that fails with the ClassNotFoundException. Both of them have commons-logging.jar listed as a JavaEE module dependency, and neither of them have a project-specific logging configuration (all config files are in a Resources project).

A major difference is that that UI project includes some other in-house frameworks (pre-compiled as JARs) that might already include necessary logging configurations and maybe that's where the difference is.

I also tried to use the answer (a file named org.apache.commons.logging.LogFactory in the META-INF/services with one line containing: "org.apache.commons.logging.impl.Log4jFactory") from this question: Websphere all logs are going to SystemOut.log but it did not seem to help.

Scarberry answered 29/6, 2012 at 19:53 Comment(14)
What type of logger instance is it? As in RollingFileAppenderFloribunda
@Woot4Moo: It's a FileAppender.Scarberry
Can you post your config file? Are the config same across all the environments?Fultz
What is the local operating system versus prod/qa/test ? Is the location writable locally as declared in the properties file?Floribunda
do you build it differently locally then you do in the other environments? maybe it's an IDE problem.Parkland
If classpath settings are to load parent (container) classes first, check the WebSphere logs to see if your logging turns up in them.Tentacle
@Woot4Moo: AIX for servers vs. WinXP for my workstation. The target location is writable, and I have a different application here that does write its log to that directory.Scarberry
I would start introducing an error in the log4j configuration file so it explodes during initialization, just to validate that the file is being read.Amaral
Write the smallest possible unit test that invokes a function that references the logger. i.e. public void go() { logger.debug("Testing"}; Does this generate anything in the eclipse console, such as unable to attach appender? Or does it spit out "Testing"?Floribunda
@NathanHughes: Yes, that could be. Local deployment is through RAD, servers use Ant and some other scripts.Scarberry
@Soronthar: Good call. Errors in the file didn't cause any errors to be reported when restarting/republishing the application.Scarberry
@Scarberry Log4j is notorious for not reading from the config file that you think it is reading. (Or rather, poorly written applications are notorious for misusing log4j this way.) What I'd do is place breakpoints in the log4j configuration classes to see which one is invoked and where they're reading from.Hearthstone
Excelent! you found one of the weirdest source of errors in WAS. A quick fix would be to add commons-logging to your app.Amaral
@Soronthar: I've added commons-logging.jar to all projects as a dependency (as well as on the buildpath) that use logging, but in some cases, I now get a ClassNotFoundException for org.apache.commons.logging.impl.Log4jFactory.Scarberry
S
2

The most recent thing I changed that finally got logging working properly was changing the classloader mode to "PARENT_FIRST" and WAR classloader policy to "Application". The initial default configuration was "PARENT_FIRST"/"Module". I changed it to "PARENT_LAST"/"Application" on the advice of a co-worker who says logging works fine for them and this is the only change they have to make when they create a new sandbox for this application. I'm not sure why I had to go with "PARENT_FIRST"/"Application", but at least it works now.


UPDATE:

I tried setting up a new workspace and I had the same problem. It turns out you need "PARENT_FIRST"/"Application" AND a file named org.apache.commons.logging.LogFactory in the META-INF/services with one line containing: "org.apache.commons.logging.impl.Log4jFactory". Not having the file causes logging to fail (typically with a message saying that a Log4J cannot be found).

Scarberry answered 4/7, 2012 at 21:23 Comment(0)
P
15

See this answer: How to initialize log4j properly?

-Dlog4j.debug is very useful for problems like this

Palate answered 29/6, 2012 at 23:19 Comment(3)
Hmm I'll have to have a look at that on Monday.Scarberry
@Scarberry Monday should be coming any day nowAzide
@Azide Well as you can seen, after testing on some Monday, I found a different solution and marked that as the answer. ;)Scarberry
S
2

The most recent thing I changed that finally got logging working properly was changing the classloader mode to "PARENT_FIRST" and WAR classloader policy to "Application". The initial default configuration was "PARENT_FIRST"/"Module". I changed it to "PARENT_LAST"/"Application" on the advice of a co-worker who says logging works fine for them and this is the only change they have to make when they create a new sandbox for this application. I'm not sure why I had to go with "PARENT_FIRST"/"Application", but at least it works now.


UPDATE:

I tried setting up a new workspace and I had the same problem. It turns out you need "PARENT_FIRST"/"Application" AND a file named org.apache.commons.logging.LogFactory in the META-INF/services with one line containing: "org.apache.commons.logging.impl.Log4jFactory". Not having the file causes logging to fail (typically with a message saying that a Log4J cannot be found).

Scarberry answered 4/7, 2012 at 21:23 Comment(0)
M
1

just my two cents - i had something like this happening - but in my case i could see some output from my logging calls. it was just apparent that the configuration was picked up from somewhere else, and the one i was changing didn't have any impact.

after turning on -Dlog4j.debug=true as suggested here, it was evident that log4j was picking up a file named log4j.xml situated inside my tomcat's work directory. either it was a leftover from other stuff i was doing, or it was generated somehow from some log4j configuration in one of my misconfigured libraries.

i haven't thought of erasing the contents of work directory (could have tried.. [edit: i tried and it didn't work]) - the only thing i though of was to pass hardcoded reference to my properties file through -Dlog4j.configuration=log4j.properties (didn't want to use absolute path) (it happened that i was using a properties file and not an xml) - and it worked.

[edit: well, it didn't work for the server configuration. I finally found what was wrong - some home-cooked libraries that I was including as JARs into my project, had their own log4j.xml and log4j.properties files, which were apparently read/found earlier than my own properties file - the rightest thing was to delete these redundant property files from JARs]

Marchese answered 26/11, 2019 at 8:29 Comment(0)
T
0

I realize this isn't your exact same symptom, but there are known issues with log4j if your application (or anything it uses) uses Commons Logging. See if this question/answer is relevant.

Toler answered 2/7, 2012 at 13:30 Comment(0)
I
0

not able to create log file, i have used logback.xml file in spring webservice application and deployed into websphere server...But when i have used log4j.properties file it is creating log file . i have given proper dependency proper for log4j and slf4j.. logback.xml file

<file>C:/abc/myLogFile.log</file>
<append>true</append>
<encoder>
  <pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder></appender>

`

dependency:

<dependency>
    <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback.version}</version>
    </dependency>
Illumination answered 25/8, 2014 at 9:39 Comment(0)
T
0

I had a problem where log4j was not showing anything in one of my projects. Turned out I had added a leading space in the classname in the log4j2.xml file. Log4j does a dictionary lookup by classname. So any leading or trailing space in the classname would render that particular entry invalid.

Thessa answered 9/6, 2015 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.