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 Attachments
35749 Views
Average (4 Votes)
The average rating is 3.5 stars out of 5.
Comments
Threaded Replies Author Date
a) this is the best article I have seen so far... Dave Weitzel August 24, 2010 9:36 AM
Hi I am a newbie in liferay. I am using liferay... Vinu Neelambaran October 25, 2010 10:52 AM
Is it possible to add this configuration inside... David García González November 5, 2010 6:20 AM
Hey Dave and Vinu, I'm afraid that this is not... Thiago Leão Moreira June 8, 2011 1:15 PM
Hey David, Yes, it is possible but you have... Thiago Leão Moreira June 8, 2011 1:23 PM
Hi Thiago, How can we achieve what Dave Weitzel... Remis Lima Baima July 6, 2011 2:34 AM
For Liferay 6.x and for more information see :... Nicolas Forney July 20, 2011 9:05 AM
Hi, nice guide but it doesn't work for me. I... Riccardo Rotondo October 12, 2011 8:40 AM
The %d{ABSOLUTE} makes my env log only the... Bogdan Herte October 14, 2013 7:37 AM

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)?
Posted on 8/24/10 9:36 AM.
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?
Posted on 10/25/10 10:52 AM.
Is it possible to add this configuration inside several portlets instead of adding it inside the EXT environment?
Posted on 11/5/10 6:20 AM.
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.
Posted on 6/8/11 1:15 PM in reply to David García González.
Hey David,

Yes, it is possible but you have the limitation above.
Posted on 6/8/11 1:23 PM in reply to 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 :-)
Posted on 7/6/11 2:34 AM in reply to 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
Posted on 7/20/11 9:05 AM.
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
Posted on 10/12/11 8:40 AM in reply to 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.
Posted on 10/14/13 7:37 AM.