
How to create custom logging
Table of Contents [-]
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.