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.

Moyenne (5 Voter)
L'estimation moyenne est de 3.8 étoiles sur 5.
Commentaires
Réponses Auteur Données
a) this is the best article I have seen so far... Dave Weitzel 24 août 2010 09:36
Hi I am a newbie in liferay. I am using liferay... Vinu Neelambaran 25 octobre 2010 10:52
Is it possible to add this configuration inside... David García González 5 novembre 2010 06:20
Hey Dave and Vinu, I'm afraid that this is not... Thiago Leão Moreira 8 juin 2011 13:15
Hey David, Yes, it is possible but you have... Thiago Leão Moreira 8 juin 2011 13:23
Hi Thiago, How can we achieve what Dave Weitzel... Remis Lima Baima 6 juillet 2011 02:34
For Liferay 6.x and for more information see :... Nicolas Forney 20 juillet 2011 09:05
Hi, nice guide but it doesn't work for me. I... Riccardo Rotondo 12 octobre 2011 08:40
The %d{ABSOLUTE} makes my env log only the... Bogdan Herte 14 octobre 2013 07:37
Hi, Thanks For the Post,but this is not working... Meena Peddi 6 août 2015 03:34
Check here:... Remis Lima Baima 6 août 2015 03:59
Thanks for the response but Link is not working... Meena Peddi 7 août 2015 00:06
https://www.liferay.com/web/denis-signoretto/blog Remis Lima Baima 7 août 2015 01: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)?
Publié le 24/08/10 09: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?
Publié le 25/10/10 10:52.
Is it possible to add this configuration inside several portlets instead of adding it inside the EXT environment?
Publié le 05/11/10 06: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.
Publié le 08/06/11 13:15 en réponse à David García González.
Hey David,

Yes, it is possible but you have the limitation above.
Publié le 08/06/11 13:23 en réponse à 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 :-)
Publié le 06/07/11 02:34 en réponse à 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
Publié le 20/07/11 09: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
Publié le 12/10/11 08:40 en réponse à 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.
Publié le 14/10/13 07: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.
Publié le 06/08/15 03:34 en réponse à Bogdan Herte.
Check here: https://www.liferay.com/web/denis-signoretto/blog/-/blogs/using-slf4j-and-lifera­y-logging-framework-in-custom-plugins
Publié le 06/08/15 03:59 en réponse à Meena Peddi.
Thanks for the response but Link is not working emoticon
Showing the error like The entry could not be found.
Publié le 07/08/15 00:06 en réponse à Remis Lima Baima.
https://www.liferay.com/web/denis-signoretto/blog
Publié le 07/08/15 01:29 en réponse à Meena Peddi.