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 archivos adjuntos
47318 Accesos
Promedio (5 Votos)
La valoración media es de 3.8 estrellas de 5.
Comentarios
Respuestas anidadas Autor Fecha
a) this is the best article I have seen so far... Dave Weitzel 24 de agosto de 2010 9:36
Hi I am a newbie in liferay. I am using liferay... Vinu Neelambaran 25 de octubre de 2010 10:52
Is it possible to add this configuration inside... David García González 5 de noviembre de 2010 6:20
Hey Dave and Vinu, I'm afraid that this is not... Thiago Leão Moreira 8 de junio de 2011 13:15
Hey David, Yes, it is possible but you have... Thiago Leão Moreira 8 de junio de 2011 13:23
Hi Thiago, How can we achieve what Dave Weitzel... Remis Lima Baima 6 de julio de 2011 2:34
For Liferay 6.x and for more information see :... Nicolas Forney 20 de julio de 2011 9:05
Hi, nice guide but it doesn't work for me. I... Riccardo Rotondo 12 de octubre de 2011 8:40
The %d{ABSOLUTE} makes my env log only the... Bogdan Herte 14 de octubre de 2013 7:37
Hi, Thanks For the Post,but this is not working... Meena Peddi 6 de agosto de 2015 3:34
Check here:... Remis Lima Baima 6 de agosto de 2015 3:59
Thanks for the response but Link is not working... Meena Peddi 7 de agosto de 2015 0:06
https://www.liferay.com/web/denis-signoretto/blog Remis Lima Baima 7 de agosto de 2015 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)?
Publicado el día 24/08/10 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?
Publicado el día 25/10/10 10:52.
Is it possible to add this configuration inside several portlets instead of adding it inside the EXT environment?
Publicado el día 5/11/10 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.
Publicado el día 8/06/11 13:15 en respuesta a David García González.
Hey David,

Yes, it is possible but you have the limitation above.
Publicado el día 8/06/11 13:23 en respuesta a 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 :-)
Publicado el día 6/07/11 2:34 en respuesta a 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
Publicado el día 20/07/11 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
Publicado el día 12/10/11 8:40 en respuesta a 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.
Publicado el día 14/10/13 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.
Publicado el día 6/08/15 3:34 en respuesta a Bogdan Herte.
Check here: https://www.liferay.com/web/denis-signoretto/blog/-/blogs/using-slf4j-and-lifera­y-logging-framework-in-custom-plugins
Publicado el día 6/08/15 3:59 en respuesta a Meena Peddi.
Thanks for the response but Link is not working emoticon
Showing the error like The entry could not be found.
Publicado el día 7/08/15 0:06 en respuesta a Remis Lima Baima.
https://www.liferay.com/web/denis-signoretto/blog
Publicado el día 7/08/15 1:29 en respuesta a Meena Peddi.