How to create custom logging

Introduction#

This article's goal is to explain how can you create a custom logging system for your portlets, this is: create a system that stores log messages in the system output.

I will do this explaination with a sample class called "com.ext.my_portlet.MyCustomClass", and I'll use it as if I were developing in the EXT environment.

Steps#

1.- Copy the file called "log4j.dtd" from the core to ext-impl/src/META-INF

2.- Create a file named "portal-log4j-ext.xml" in ext-impl/src/META-INF with a new category for your custom class.

The content would be something like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="[[http://jakarta.apache.org/log4j/">|http://jakarta.apache.org/log4j/">]]
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n" />
		</layout>
	</appender>
	<category name="com.ext.my_portlet.MyCustomClass">
		<priority value="INFO" />
	</category>
</log4j:configuration>

3.- In your custom Java class, create a log variable using the LogFactoryUtil:

private static Log _log = LogFactoryUtil.getLog(MyCustomClass.class);

4.- In the section of your code you want to write a log message, make a call to the corresponding method.

Note 1: In this step you have to choose which log level you like to use. There are several levels of priority:

  • debug
  • warn
  • info
  • fatal
  • error
  • trace

Note 2: in the control panel you'll be able to configure the log level for your custom class.

For my example, I'll use INFO level.

You'll have to add the following code to write a log message:

 if(_log.isInfoEnabled()){
	_log.info("This is an INFO level log message");
 } 

Limitations#

Liferay does not provide a way to declare different appenders.

0 附件
54932 查看
平均 (5 票)
满分为 5,平均得分为 3.8。
评论
讨论主题回复 作者 日期
a) this is the best article I have seen so far... Dave Weitzel 2010年8月24日 上午9:36
Hi I am a newbie in liferay. I am using liferay... Vinu Neelambaran 2010年10月25日 上午10:52
Is it possible to add this configuration inside... David García González 2010年11月5日 上午6:20
Hey Dave and Vinu, I'm afraid that this is not... Thiago Leão Moreira 2011年6月8日 下午1:15
Hey David, Yes, it is possible but you have... Thiago Leão Moreira 2011年6月8日 下午1:23
Hi Thiago, How can we achieve what Dave Weitzel... Remis Lima Baima 2011年7月6日 上午2:34
For Liferay 6.x and for more information see :... Nicolas Forney 2011年7月20日 上午9:05
Hi, nice guide but it doesn't work for me. I... Riccardo Rotondo 2011年10月12日 上午8:40
The %d{ABSOLUTE} makes my env log only the... Bogdan Herte 2013年10月14日 上午7:37
Hi, Thanks For the Post,but this is not working... Meena Peddi 2015年8月6日 上午3:34
Check here:... Remis Lima Baima 2015年8月6日 上午3:59
Thanks for the response but Link is not working... Meena Peddi 2015年8月7日 上午12:06
https://www.liferay.com/web/denis-signoretto/blog Remis Lima Baima 2015年8月7日 上午1:29

a) this is the best article I have seen so far on exactly what to do to set this up......

however....

b) How do I define a different output log file - so all errors from my portlets and ext/hooks can be written to the same file which will only have tracing from my code using hopefully as little of my code as possible.
(eg only one portal-log4j-ext.xml file with one configuration entry)?
在 10-8-24 上午9:36 发帖。
Hi I am a newbie in liferay. I am using liferay portal 6.0.4 bundled with Tomcat.I would like to create a separate log file for my classes. Can anybody tell me how I can create my custom log file and configure log4j in liferay 6.0.4?
在 10-10-25 上午10:52 发帖。
Is it possible to add this configuration inside several portlets instead of adding it inside the EXT environment?
在 10-11-5 上午6:20 发帖。
Hey Dave and Vinu,

I'm afraid that this is not possible using only the portal-log4j-ext.xml file. When Liferay does read this file it discards the appenders information... in others words it just take in account the categories, unfortunately.
在 11-6-8 下午1:15 发帖以回复 David García González
Hey David,

Yes, it is possible but you have the limitation above.
在 11-6-8 下午1:23 发帖以回复 Thiago Leão Moreira
Hi Thiago,
How can we achieve what Dave Weitzel wants (i.e. define a different output log file)?
To solve that we currently use log4j directly in our portlets (i.e. add log4j.jar + configure log4j.xml + change all code to use org.apache.log4j.Logger). But I personally always prefer Liferay built in solutions. Do you (or anyone else :-) have a better idea?
Obrigado :-)
在 11-7-6 上午2:34 发帖以回复 Thiago Leão Moreira
For Liferay 6.x and for more information see :
http://www.liferay.com/community/wiki/-/wiki/Main/How+to+configure+the+logs+in+­Liferay
在 11-7-20 上午9:05 发帖。
Hi, nice guide but it doesn't work for me.
I created the two files in the src directory but my portlet still work in info mode (i tried to set to DEBUG)


RRmacbproemoticonOGS riccardorotondo$ ll src/java/
total 40
-rw-r--r-- 1 riccardorotondo staff 1169 Sep 28 14:31 hibernate.cfg.xml
-rw-r--r-- 1 riccardorotondo staff 601 Sep 13 16:44 hibernate.reveng.xml
drwxr-xr-x 3 riccardorotondo staff 102 Sep 13 10:54 it
-rw-r--r-- 1 riccardorotondo staff 4913 Oct 12 17:29 log4j.dtd
-rw-r--r-- 1 riccardorotondo staff 574 Oct 12 17:30 portal-log4j-ext.xml

Thanks for your help.
Bye
在 11-10-12 上午8:40 发帖以回复 Nicolas Forney
The %d{ABSOLUTE} makes my env log only the hour, not the year, month and day. Which is a huge problem on a production env. I changed to %d{yyyy/MM/dd HH:mm:ss} and after the restart it got back to the previous version.
在 13-10-14 上午7:37 发帖。
Hi,
Thanks For the Post,but this is not working for me.
I am able to see the new category in the contro-panel but the logs are not appending to the file.My application is using commons logging.I have tried by using log4j-jcl-2.3 but no luck.
Please help me.
在 15-8-6 上午3:34 发帖以回复 Bogdan Herte
Check here: https://www.liferay.com/web/denis-signoretto/blog/-/blogs/using-slf4j-and-lifera­y-logging-framework-in-custom-plugins
在 15-8-6 上午3:59 发帖以回复 Meena Peddi
Thanks for the response but Link is not working emoticon
Showing the error like The entry could not be found.
在 15-8-7 上午12:06 发帖以回复 Remis Lima Baima
https://www.liferay.com/web/denis-signoretto/blog
在 15-8-7 上午1:29 发帖以回复 Meena Peddi