Sunday, March 18, 2012

Configure log4j in weblogic server


Hello Everyone,

Today we will discuss about Configure log4j in weblogic server :

Step 1. Copy log4j-1.2.9.jar & wllog4j.jar under domain_root/lib folder.

Step 2. Create log4j.xml and drop it under domain_root folder. If you have log4j.xml inside the application(EAR/WAR), I would recommend to move outside EAR/WAR as
changing severity (log level) would require re-deployment. To avoid this, we can move log4j.xml into domain root folder but again the server needs to be bounced.
But there is a fix.

Step 3. Login to weblogic server console. Go to Servers -> Admin Server-> Logging.
Click on advanced mode. Change the logging implementation from JDK to Log4J. 


Click save.

Step 4. Activate changes. Re-start the admin server.

This should enable log4j and should write logging into log file mentioned in log4j.xml or properties file.

Hang on! There is a catch here. Say now I want to change the log level from DEBUG into WARN or ERROR. 


How do i do? 

We go to log4j.xml and change the level.
Now how does this change take into effect? It needs bouncing of the server.
I have given below sample log4j.xml for your reference...

log4j.xml:
 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM
"log4j.dtd">
<log4j:configuration>
 <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern"
            value="%d [%t] %-5p %c - %m%n"/>
   </layout>
 </appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
   <param name="File" value="/log/myApp.log"/>
   <param name="Append" value="true"/>
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d{ISO8601} %t %-5p %c{2} - %m%n"/>
   </layout>
 </appender>
 <logger name="org.apache">
   <level value="WARN"/>
 </logger>
 <logger name="org.springframework">
   <level value="WARN"/>
 </logger>
 <root>
   <level value="DEBUG"/>
   <appender-ref ref="FILE"/>
<!--   <appender-ref ref="CONSOLE"/> -->
 </root>
</log4j:configuration>
I have disabled log4j to write into weblogic console by commenting out. If you want it enable den you can do it by uncommenting

I hope it will help you in configuring log4j in weblogic server. And would also like to thank google and peoples who shares their views on the same.

Enjoy working in Middleware.....

Regards,
Ajinkya Vichare


ajinkya-vichare.blogspot.com

7 comments:

  1. Hello!

    I'm trying to do this procedure for some time, but I'm still not able to write in the file that I specified in log4j.properties.

    I've done all that you said in this post, but it still not working.

    I have a Java function for logging:
    Log log = LogFactory.getLog("com.org.test.TestingLogger");
    log.info("Hello, Log4j");

    I've put commons-logging-1.1.1.jar, log4j-1.2.12.jar, wlcommons-logging.jar and wllog4j.jar in DOMAIN_HOME/lib folder and I've defined a properties file that I put in DOMAIN_FOLDER:

    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.Threshold=DEBUG
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS z} %-5p [%c] %m%n

    # LOGFILE is set to be a File appender using a PatternLayout.
    log4j.appender.Testing=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.Testing.File=d:/log/testing.log
    log4j.appender.Testing.Append=true
    log4j.appender.Testing.Threshold=DEBUG
    log4j.appender.Testing.layout=org.apache.log4j.PatternLayout
    log4j.appender.Testing.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS z} %-5p [%c] %m%n

    log4j.logger.com.org.test.TestingLogger=DEBUG, Testing

    After creating the properties file, I'd changed the setDomainEnv.cmd:

    set LOG4J_CONFIG_FILE=D:\Middleware\SOASuite11gR1PS\user_projects\domains\SOAOSBPS4DevDomain\log4j.properties

    set EXTRA_JAVA_PROPERTIES= %EXTRA_JAVA_PROPERTIES% -Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl

    set EXTRA_JAVA_PROPERTIES= %EXTRA_JAVA_PROPERTIES% -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

    set EXTRA_JAVA_PROPERTIES=%EXTRA_JAVA_PROPERTIES% -Dweblogic.log.Log4jLoggingEnabled=true

    set EXTRA_JAVA_PROPERTIES=%EXTRA_JAVA_PROPERTIES% -Dlog4j.debug=true

    When I run the service with this configuration nothing is writen on the file and it appears some log in console log, but only for INFO and FATAL and not in the format I specified.

    Do you know what can be wrong?

    ReplyDelete
    Replies
    1. will look into the same and revert you ASAP...

      Regards,
      Ajinkya

      Delete
    2. Hi,

      For your issue kindly look into following Metalink id :[ID 1088135.1]

      I think there is some configuration setting issue.

      So check with given metalink ID and let me know your result.

      Regards.
      Ajinkya

      Delete
  2. Hello Ajinkya,

    the problem is that this function is in a Java Project that I invoke in a Java Callout in OSB 11g, it's not an ear.

    Regards,

    Lara Fernandes.

    ReplyDelete
    Replies
    1. @ LARA,

      Any luck i your error, kindly let us know solution or observations for the same.

      So that we also get more n more understanding on the same.

      Above post cover log4j configuration for application in Weblogic application server.

      Regards,
      Ajinkya

      Delete
  3. Thanks Ajinkya

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete