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 Anhänge
56699 Angesehen
Durchschnitt (5 Stimmen)
Die durchschnittliche Bewertung ist 3.8 von max. 5 Sternen.
Kommentare
Antworten im Thread Autor Datum
a) this is the best article I have seen so far... Dave Weitzel 24. August 2010 09:36
Hi I am a newbie in liferay. I am using liferay... Vinu Neelambaran 25. Oktober 2010 10:52
Is it possible to add this configuration inside... David García González 5. November 2010 06:20
Hey Dave and Vinu, I'm afraid that this is not... Thiago Leão Moreira 8. Juni 2011 13:15
Hey David, Yes, it is possible but you have... Thiago Leão Moreira 8. Juni 2011 13:23
Hi Thiago, How can we achieve what Dave Weitzel... Remis Lima Baima 6. Juli 2011 02:34
For Liferay 6.x and for more information see :... Nicolas Forney 20. Juli 2011 09:05
Hi, nice guide but it doesn't work for me. I... Riccardo Rotondo 12. Oktober 2011 08:40
The %d{ABSOLUTE} makes my env log only the... Bogdan Herte 14. Oktober 2013 07:37
Hi, Thanks For the Post,but this is not working... Meena Peddi 6. August 2015 03:34
Check here:... Remis Lima Baima 6. August 2015 03:59
Thanks for the response but Link is not working... Meena Peddi 7. August 2015 00:06
https://www.liferay.com/web/denis-signoretto/blog Remis Lima Baima 7. August 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)?
Gepostet am 24.08.10 09:36.