« 返回到 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 附件
204578 查看
平均 (3 票)
满分为 5,平均得分为 4.33333333333333。
评论
讨论主题回复 作者 日期
Related to this, I have created an article... Juan Fernández 2009年12月15日 上午2:03
"Appending logs" to a file doesn't work with 5.2? Chris Whittle 2010年8月9日 下午1:52
are you able to make it work? Nagendra Kumar Busam 2010年9月14日 上午9:00
Neither with 6.0.5. Henrique Fernandes 2010年12月17日 上午4:26
Sorry, file and console appenders are working... Henrique Fernandes 2010年12月20日 上午1:27
for Tomcat 6.x, why we should put put the... Jay XU 2010年9月7日 上午2:26
No, that's not working for me. In Liferay 6,... Dan Smith 2010年9月21日 上午9:16
That's fine with:... Dan Smith 2010年9月28日 上午8:23
I could not make it work Liferay v6.0 with... Alberto Gallardo 2010年10月6日 上午10:02
What works for me with Liferay 6.0.4 on a... Andrea Caltroni 2010年11月4日 上午6:04
Hi, I have set in the Admin portlet the log... vikash kumar chaurasia 2010年12月6日 下午10:36
See... Henrique Fernandes 2010年12月15日 上午8:06
Log4j Working with Liferay 6.0.5, for multiple... Prakash Khanchandani 2011年2月23日 上午7:41
The above technique by Prakash worked for me.... Rick Umali 2011年5月5日 下午12:23
Excellent article thank you! Nicolas Forney 2011年7月20日 上午9:12
Hi, nice guide. Unfortunately in Glassfish it... Riccardo Rotondo 2011年10月12日 上午6:47
Nice article. Thanks. What worked for me was... Wole Adetiba 2011年12月7日 上午12:28
For users of Liferay 6.x, the following link to... Ben Carson 2012年6月13日 下午1:05
@Prakash Khanchandani I want to debug Liferay... Sunil Rai 2012年7月18日 上午5:20
I have do below configurations: 1. Get... python shi 2013年2月27日 上午12:15
Awesome!!!, I was facing the issue... Nelson Aguayo 2014年2月11日 上午8:45

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
在 09-12-15 上午2:03 发帖。
"Appending logs" to a file doesn't work with 5.2?
在 10-8-9 下午1:52 发帖。
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
在 10-9-7 上午2:26 发帖。
are you able to make it work?
在 10-9-14 上午9:00 发帖以回复 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.
在 10-9-21 上午9:16 发帖。
That's fine with:
$TOMCAT_HOME\webapps\ROOT\WEB-INF\classes\META-INF\portal-log4j-ext.xml
在 10-9-28 上午8:23 发帖以回复 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.
在 10-10-6 上午10:02 发帖。
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);
在 10-11-4 上午6:04 发帖以回复 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
在 10-12-6 下午10:36 发帖以回复 Andrea Caltroni
See http://www.liferay.com/community/forums/-/message_boards/message/6814315/maximiz­ed?_19_preview=false#_19_message_6814315
在 10-12-15 上午8:06 发帖以回复 Alberto Gallardo
在 10-12-17 上午4:26 发帖以回复 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\.
在 10-12-20 上午1:27 发帖以回复 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
在 11-2-23 上午7:41 发帖。
The above technique by Prakash worked for me. Thanks for posting this!
在 11-5-5 下午12:23 发帖以回复 Prakash Khanchandani
Excellent article thank you!
在 11-7-20 上午9:12 发帖以回复 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.
在 11-10-12 上午6:47 发帖以回复 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\.
在 11-12-7 上午12:28 发帖以回复 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.
在 12-6-13 下午1:05 发帖以回复 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
在 12-7-18 上午5:20 发帖以回复 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?
在 13-2-27 上午12:15 发帖以回复 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!
在 14-2-11 上午8:45 发帖。