In many cases we need to set log levels at package level. We can do this by defining package level log by following property in log4j.properties file
log4j.logger.<your package> = DEBUG|INFO|OFF|WARN...
Liferay provides convenience way to define logging level at package through configuration. Means without restarting the sever, we can add/modify it.
To understand it properly, we first will create custom Liferay portlet. Please refer my previous blog Creating custom Liferay MVC portlet to create custom Liferay portlet. Give the name of portlet project as “check-log-level” (‘-portlet‘ will be appended by wizard while creating portlet).
Also give the package name like “com.test.log.level” and class name as “CustomLogLevelTest.java”.
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.
package com.test.log.level;
import java.io.IOException; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.util.bridges.mvc.MVCPortlet; public class CustomLogLevelTest extends MVCPortlet{ private final Log log = LogFactoryUtil.getLog(CustomLogLevelTest.class); //Default Render Method. public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { log.info("This is info Logger..."); log.debug("This is Debug Logger..."); log.error("This is error Logger ..."); super.doView(renderRequest, renderResponse); } }
I have created 3 logger, one of each level (info, debug and error). By default, info and error level logger will execute. Build and deploy this portlet. Create new public page named Log Test in community Nilang. Refer my previous blog Create Web Content in Liferay to know how to create community and public page.
Place this portlet on this page (Log Test) and observe the log. you will find that only info and error level logger get executed as per below screenshot.
Set log level from control panel
Now we will change the logger level from control panel.
- Go to Control Panel.
- Click on Server Administration from Server section in control panel.
- Click on Log Levels (second link) as shown in below screen.
- You will notice that, there are various Categories and Log level defined.
- We will now add new category.
- Click on Add Category link
- Give the java package. In our case its com.test.log.level
- Set log level from drop down. We will first set INFO.
- Go back to the Nilang community and refresh the page Log Test.
- Now you will observe the following loggers in log.
The logger levels follow the following order.
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
It means,
- if we set Log level to WARN then only the WARN, ERROR and FATAL level logs will be displayed.
- If we set log level to ERROR then only the ERROR and FATAL will be displed.
and like that it will show masseges with all log levels below the current preset one.
Summing Up
- Liferay provides convenience way to set log at different package level.
- Logs can be defined from Liferay control panel.
- For more information about logs in liferay, you can reach out to this link.
You can create more package and set different log level and verify it. Feel free you raise any questions in comment section
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.
Download Source
Hi ,
Nice Arcticle!
May i know how to add custom log level to the server Administrtion
Thanks ,
Rohitha.
Hi,
nice article! But do you know how to remove a category?
Hi,
Thanks for writing here. Can you please clarify what you are looking for ?
Regards
Nilang
Thanks!
Would it be possible to use custom log4j.properties file for our portlet?
Hi,
Welcome to Tech blog and thanks for appreciation.
Yes. Its possible to write custom log4j.properties where you can point specific file and use that appender for specific java class. I am planning to write separate blog for the same.
Regards
Nilang
file is being created…but messeges are not written in file ….wt should we modify…in liferay..