« Back to Configuring

Configuring Logs

Introduction #

Liferay uses the Apache Log4j library to perform all of it's logging operations. This allows for a very configurable setup with different levels of messages priority, from FATAL to DEBUG. It also allows to configure a different level of messages to be shown for every package or class within Liferay or any of its libraries.

The default configuration is set to only show errors for most classes and packages with a few of them set to show also warnings.

Where are the logs? #

By default, the logs are written to the standard output (CONSOLE in log4j's terminology) which means that they'll be outputted to the application server log file. Here are the location of the logs in some of the application servers supported by Liferay:

  • Tomcat: $TOMCAT/bin/catalina.out
  • Geronimo: $GERONIMO/var/log/geronimo.log
  • JBoss: $JBOSS/server/default/log/
  • Glassfish: $GLASSFISH/domains/domain1/logs/server.log

Customizing the Log4j configuration #

Using the Enterprise Admin Portlet #

The Admin Portlet available to portal administrators offers a UI to dinamically change the log levels for many of the packages of Liferay and its main libraries. To access this UI add the Admin Portlet to a portal page, go to the Server tab and click 'more' so that the subtabs are shown. Go to the 'Log levels' subtab and you should see a screen similar to the following:

By using the dropdown you can select a different log level for each of the paragraphs, from DEBUG to FATAL, or turn them off completely. Once you are finished click the save button to apply your changes. You can do this operation as many times as desired.

This changes are stored in memory, which means that they'll be lost when you reboot the server. If you wish to make changes to the configuration in a way that they are preserved after reboots or that can be maintained accross different installations read the following sections.

Creating a custom configuration file: portal-log4j-ext.xml #

The logging configuration of Liferay is stored in a file called portal-log4j.xml. It's possible to override all or certain parts of that configuration by creating another file called portal-log4j-ext.xml and deploying it to the appropriate place. This section details the steps necessary to achieve this.

The first step is to get a copy of the original configuration file. Depending on our environment the portal-log4j.xml can be found in:

  • Liferay's sources: portal-impl/classes/META-INF/
  • Tomcat bundle: inside $TOMCAT/WEB-INF/lib/portal-impl.jar in META-INF/
  • Other bundles: look for portal-impl.jar, the file will be inside its META-INF directory once unzipped

Note for JBoss users: JBoss includes its own Log4j configuration that may override Liferay's configuration. The JBoss Log4j configuration file can be found in $JBOSS/server/default/conf/log4j.xml. Read the JBoss documentation for details.

Make a copy of that file and rename it as portal-log4j-ext.xml. It is recommended that you only keep in that file those entries that have been modified, delete the other entries, since they'll be loaded from portal-log4j.xml.

Let's see an example of how we could change the log level of a library, for example, Hibernate. By default the log level of this library is ERROR. Let's change it to INFO. Look in the file for the following entry:

  <category name="org.hibernate">
    <priority value="ERROR" />
  </category>

A portal-log4j-ext.xml file that changes only this log level would be:

 <?xml version="1.0"?>
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <category name="org.hibernate">
    <priority value="INFO" />
  </category>
 </log4j:configuration>

Once you've done the necessary changes deploy the file. The file has to be deployed inside a directory called META-INF to a directory that is in the classpath. Here are some recommended locations, depending on the application server:

  • Tomcat 5.x: $TOMCAT/common/classes/META-INF
    • Note: To this directory, you also need to copy log4j.dtd from the same location where you obtained the portal-log4j.xml file. Else, you will encounter errors about the fact that log4j.dtd cannot be resolved.
  • Tomcat 6.x: $TOMCAT_HOME/lib/META-INF
    • Note: As in Tomcat 5.x, you also need to copy log4j.dtd.
  • Geronimo: One possibility to do is is by adding a .jar file containing the META-INF/log4j.dtd (as with Tomcat) and the META-INF/portal-log4j-ext.xml to Geronimo's $GERONIMO_HOME/lib directory. In a hurry, just adding the directory and files to one of the existing .jar files (i.e. log4j-1.x.jar) in the directory is also possible; this way, you do not have to create your own jar for it.
  • Tomcat 7.2.3: $TOMCAT_HOME/webapps/ROOT/WEB-INF/classes/META-INF
    • Note: Copy log4j.dtd as well as portal-log4j-ext.xml, replacing webapps/ROOT with the Liferay-Installation directory.
  • Glassfish 3.1.2: domain-dir/lib/classes/META-INF/portal-log4j-ext.xml
  • Other application servers: TBD

Note: If you are using an extension environment, it can only store and deploy for you a portal-log4j-ext.xml file. See Development in the ext environment for detailed instructions. When using the ext environment the portal-log4j-ext.xml file will be automatically included in your installation when using the deploy ant targets.

Appending logs to a file #

Customize the log4j.properties file to append the logs to a file: log4j.appender.CONSOLE=org.apache.log4j.FileAppender

And also specify where the file will be located: log4j.appender.CONSOLE.File=E:/Liferay/5.1/liferay-portal-tomcat-5.5-5.1.0/logs/test.log

Technical information #

Log4 initialization #

The initialization of Log4j is performed in the InitAction.run() method which is executed when the application server starts up. This method is invoked when the MainServlet class is loaded because of the following code that can be find within it:

 static {
   InitUtil.init();
 }

The InitUtil class is just a helper class that ends up invoking InitAction.run().

The InitAction class loads two log4j configuration files, portal-log4j.xml and portal-log4j-ext.xml with the second one overriding the configuration of the first. If any of this files is missing they are silently ignored. This is the code that performs this operation:

  if (GetterUtil.getBoolean(SystemProperties.get("log4j.configure.on.startup"), true) && !ServerDetector.isSun()) {
  
    ClassLoader classLoader = getClass().getClassLoader();
  
    Log4JUtil.configureLog4J(classLoader.getResource("META-INF/portal-log4j.xml"));
    Log4JUtil.configureLog4J(classLoader.getResource("META-INF/portal-log4j-ext.xml"));
  }

Note: This code may have slightly changed since this article was written or may be different for your version. Make sure you download Liferay's sources and look at your specific version of the mentioned files for accurate information.

Troubleshooting #

If you have setup a portal-log4j-ext.xml file but your changes are not being applied there are two possible causes:

  • The file has an incorrect syntax
  • The file is not being found

If you can't see any information in the logs to help you determine the cause, the best way to find out is to do some debugging:

  1. Configure your IDE to be able to connect to a remote Liferay instance for debugging or to be able to run Liferay within it
  2. Set up a breakpoint in the line where the ClassLoader is defined (see code fragment above)
  3. Run Liferay with remote debugging enabled or within the IDE
  4. If using remote debugging connect to Liferay very quickly after starting it (within the first 2 secs). The Log4J configuration happens very early in the startup. If the debugger does not stop in the breakpoint, stop Liferay and try again.
  5. Execute step-by-step and enter the configureLog4J method for the portal-log4j-ext.xml file to find out if the file is found and if it's read correctly
0 Attachments
111518 Views
Average (3 Votes)
The average rating is 4.33333333333333 stars out of 5.
Comments
Threaded Replies Author Date
Related to this, I have created an article... Juan Fernández December 15, 2009 2:03 AM
"Appending logs" to a file doesn't work with 5.2? Chris Whittle August 9, 2010 1:52 PM
are you able to make it work? Nagendra Kumar Busam September 14, 2010 9:00 AM
Neither with 6.0.5. Henrique Fernandes December 17, 2010 4:26 AM
Sorry, file and console appenders are working... Henrique Fernandes December 20, 2010 1:27 AM
for Tomcat 6.x, why we should put put the... Jay XU September 7, 2010 2:26 AM
No, that's not working for me. In Liferay 6,... Dan Smith September 21, 2010 9:16 AM
That's fine with:... Dan Smith September 28, 2010 8:23 AM
I could not make it work Liferay v6.0 with... Alberto Gallardo October 6, 2010 10:02 AM
What works for me with Liferay 6.0.4 on a... Andrea Caltroni November 4, 2010 6:04 AM
Hi, I have set in the Admin portlet the log... vikash kumar chaurasia December 6, 2010 10:36 PM
See... Henrique Fernandes December 15, 2010 8:06 AM
Log4j Working with Liferay 6.0.5, for multiple... Prakash Khanchandani February 23, 2011 7:41 AM
The above technique by Prakash worked for me.... Rick Umali May 5, 2011 12:23 PM
Excellent article thank you! Nicolas Forney July 20, 2011 9:12 AM
Hi, nice guide. Unfortunately in Glassfish it... Riccardo Rotondo October 12, 2011 6:47 AM
Nice article. Thanks. What worked for me was... Wole Adetiba December 7, 2011 12:28 AM
For users of Liferay 6.x, the following link to... Ben Carson June 13, 2012 1:05 PM
@Prakash Khanchandani I want to debug Liferay... Sunil Rai July 18, 2012 5:20 AM
I have do below configurations: 1. Get... python shi February 27, 2013 12:15 AM
Awesome!!!, I was facing the issue... Nelson Aguayo February 11, 2014 8:45 AM

Related to this, I have created an article called "How to create custom loggin" to explain how to do this for your own portlets.

Here's the link: http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/How+to+create+custom­+logging
Posted on 12/15/09 2:03 AM.
"Appending logs" to a file doesn't work with 5.2?
Posted on 8/9/10 1:52 PM.
for Tomcat 6.x, why we should put put the config file under $TOMCAT_HOME/lib, we should not put any config file here

why don't you put it under webapps/ROOT/WEB-INF/classes/META-INF
Posted on 9/7/10 2:26 AM.
are you able to make it work?
Posted on 9/14/10 9:00 AM in reply to Chris Whittle.
No, that's not working for me.

In Liferay 6, the InitUtil.class refers to META-INF\portal-log4j-ext.xml,
so I put the modified portal-log4j-ext.xml into
$TOMCAT_HOME\webapps\ROOT\WEB-INF\classes\META-INF\


I tried first:
$TOMCAT_HOME\lib\

without more success. Do you have an idea?

Thanks.
Posted on 9/21/10 9:16 AM.
That's fine with:
$TOMCAT_HOME\webapps\ROOT\WEB-INF\classes\META-INF\portal-log4j-ext.xml
Posted on 9/28/10 8:23 AM in reply to Dan Smith.
I could not make it work Liferay v6.0 with glassfish. I'm only using portlets plugin. Did anyone get it?

I have found quite frustrated that liferay creates a log4j.jar and a log4j.properties for every portlet that it deploys. This combination does not work properly in glassfish v.2.1.1, as these files already exists for the liferay module: Depending on what ClassLoader loads the log4j, a <<"ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable>> is thrown.
Posted on 10/6/10 10:02 AM.
What works for me with Liferay 6.0.4 on a custom portlet using Spring and Hibernate is just using a log4j.properties configuration file and putting it in WEB-INF/classes of the portlet.

I removed the portal-log4j-ext.xml and log4j.dtd from <LIFERAY_TOMCAT_HOME>/webapps/ROOT/WEB-INF/classes/META-INF/
(which was not working) and I have no configuration in Control Panel --> Server Administration --> Log Levels for my classes.

In the classes I instantiate the logger with:

static Logger logger = Logger.getLogger(YourClass.class);
Posted on 11/4/10 6:04 AM in reply to Alberto Gallardo.
Hi,

I have set in the Admin portlet the log level as DEBUG for one of the package used in my portlet. However, I am not getting the debug level messages in the log generated?

I am able to get the INFO and ERROR and other (except DEBUG) level logs.

Can anybody suggest me.

For detailed description plz go to the URL:
http://www.liferay.com/de/community/forums/-/message_boards/message/6661489
Posted on 12/6/10 10:36 PM in reply to Andrea Caltroni.
See http://www.liferay.com/community/forums/-/message_boards/message/6814315/maximiz­ed?_19_preview=false#_19_message_6814315
Posted on 12/15/10 8:06 AM in reply to Alberto Gallardo.
Neither with 6.0.5.
Posted on 12/17/10 4:26 AM in reply to Chris Whittle.
Sorry, file and console appenders are working with the portal-log4j-ext.xml extension under $TOMCAT_HOME\webapps\ROOT\WEB-INF\classes\META-INF\.
Posted on 12/20/10 1:27 AM in reply to Henrique Fernandes.
Log4j Working with Liferay 6.0.5, for multiple appenders
The following worked for me:
1) I placed the two files portal-log4j-ext.xml and log4j.dtd inside the $TOMCAT/lib/META-INF
2) the code inside the portal-log4j-ext.xml

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="MY_APPENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.home}/logs/myAppender.log"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="----- %n%d{yyyy/MM/dd HH:mm:ss} %p%n%l [%x][%t] %n%m%n" />
</layout>
</appender>

<logger name="com.my.base.package">
<level value="INFO" />
<appender-ref ref="MY_APPENDER" />
</logger>

</log4j:configuration>

I have not used the <root> tag, as it will still be used to print to the Console and the above will print to the log file inside $TOMCAT/logs/.

we can also use <root> instead of <logger>:
<root>
<priority>INFO<priority>
<appender-ref ref="CONSOLE" /> <!-- This being the default appender inside portal-log4j.xml -->
<appender-ref ref="MY_APPENDER" />
</root>

My environment: Liferay 6.0.5 and tomcat-6.0.26.
Don't know if this is the best way. Hope this helps.
Thanks Chris Whittle for giving the hint in your other post
Posted on 2/23/11 7:41 AM.
The above technique by Prakash worked for me. Thanks for posting this!
Posted on 5/5/11 12:23 PM in reply to Prakash Khanchandani.
Excellent article thank you!
Posted on 7/20/11 9:12 AM in reply to Rick Umali.
Hi, nice guide.
Unfortunately in Glassfish it works in a different way. I'm deploying a portlet by netbeans but I'm not able to set the logger level at debug. Any suggestions ? Thanks in advance.
Posted on 10/12/11 6:47 AM in reply to Nicolas Forney.
Nice article. Thanks. What worked for me was having the portal-log4j-ext.xml extension under $TOMCAT_HOME\webapps\ROOT\WEB-INF\classes\META-INF\.
Posted on 12/7/11 12:28 AM in reply to Riccardo Rotondo.
For users of Liferay 6.x, the following link to the Liferay User's Guide should get you up and running.
http://www.liferay.com/documentation/liferay-portal/6.1/user-guide/-/ai/­liferay-s-logging-syst-5
So much easier than the steps listed in this wiki. Just create a Log object in your code and a 'category' in the server console.
Posted on 6/13/12 1:05 PM in reply to Wole Adetiba.
@Prakash Khanchandani I want to debug Liferay 6.0.6 CE LDAP for the following:

com.liferay.portal.security.auth.LDAPAuth
com.liferay.portal.security.ldap.PortalLDAPUtil

But its not working at my end. Can you please guide me for the same.

Thanks,
Sunil
Posted on 7/18/12 5:20 AM in reply to Ben Carson.
I have do below configurations:
1. Get portal-log4j.xml from Liferay's sources: portal-impl/classes/META-INF/, rename it as portal-log4j-ext.xml. Change it to
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="DMSServiceLog" class="com.icil.fw.logging.ICILRollingFileAppender">
<param name="File" value="${catalina.home}/logs/dms_log/dms.log"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}(%F:%L)] [%X{username}] - %m%n" />
</layout>
</appender>
<logger name="com.icil.dms.portlet">
<level value="INFO" />
<appender-ref ref="DMSServiceLog" />
</logger>
</log4j:configuration>

2. Put file portal-log4j-ext.xml, log4j.dtd to TOMCAT_HOME/webapps/ROOT/WEB-INF/classes/META-INF.

3. restar tomcat6, but throw exception:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassCastException: com.icil.fw.logging.ICILRollingFileAppender cannot be cast to org.apache.log4j.Appender
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:248)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176­)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.jav­a:191)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurato­r.java:523)
at org.apache.log4j.xml.DOMConfigurator.parseCategory(DOMConfigurator.java:436)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:999)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:867)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:815)
at com.liferay.util.log4j.Log4JUtil.configureLog4J(Log4JUtil.java:79)
at com.liferay.util.log4j.Log4JUtil.configureLog4J(Log4JUtil.java:58)
at com.liferay.portal.util.InitUtil.init(InitUtil.java:94)
at com.liferay.portal.spring.context.PortalContextLoaderListener.contextInitialized­(PortalContextLoaderListener.java:134)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
­at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
a­t org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at­ sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav­a:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

My class com.icil.fw.logging.ICILRollingFileAppender is extends RollingFileAppender, but stilll throw excetpion: java.lang.ClassCastException: com.icil.fw.logging.ICILRollingFileAppender cannot be cast to org.apache.log4j.Appender
What is the reason?
Posted on 2/27/13 12:15 AM in reply to Prakash Khanchandani.
Awesome!!!, I was facing the issue https://issues.liferay.com/browse/LPS-36627
with liferay 6.2, now I apply the file portal-log4j-ext.xml inside the portal-impl.jar (portal-impl/META-INF/)

<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<category name="portal-web.docroot.errors.404_jsp">
<priority value="DEBUG" />
</category>
</log4j:configuration>


Thanks you!
Posted on 2/11/14 8:45 AM.