留言板
Using classes form portal-impl.jar
Hi,
I'm creating a new portlet using Liferay 6.1.1 CE GA2 version, and I'm compiling it with Maven 3.0.4. I need to create a new instance of the "com.liferay.portal.log.Log4jLogFactoryImpl" class from my portlet, and this class just belongs to "portal-impl" library.
As Mika Koivisto says in his blog, "portal-impl" should never be added as dependency to a Liferay plugin:
http://www.liferay.com/web/mika.koivisto/blog/-/blogs/4375832
So, how could I reference this class without using this dependency?
Thanks.
I'm creating a new portlet using Liferay 6.1.1 CE GA2 version, and I'm compiling it with Maven 3.0.4. I need to create a new instance of the "com.liferay.portal.log.Log4jLogFactoryImpl" class from my portlet, and this class just belongs to "portal-impl" library.
As Mika Koivisto says in his blog, "portal-impl" should never be added as dependency to a Liferay plugin:
http://www.liferay.com/web/mika.koivisto/blog/-/blogs/4375832
So, how could I reference this class without using this dependency?
Thanks.
David H Nebinger,修改在11 年前。
RE: Using classes form portal-impl.jar
Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
You shouldn't be using it at all.
Instead, use the LogFactoryUtil class to get a logger:
Instead, use the LogFactoryUtil class to get a logger:
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
...
private static final Log logger = LogFactoryUtil.getLog(MyClass.class);
Hi,
Sorry, I should explain what I'm trying to do. I'm making a portlet which is able to add, dynamically, new loggers to Log4J system. These loggers can be configured, specifing the file where it writes, the layout, the maximum size of the file...
This is the example code:
DOMConfigurator domConfigurator = new DOMConfigurator();
domConfigurator.doConfigure(reader, LogManager.getLoggerRepository());
Log4JUtil.setLevel(loggerName, loggerLevel, false);
LogFactoryUtil.setLogFactory(new com.liferay.portal.log.Log4jLogFactoryImpl());
Variables:
- reader: where xml configuration logger is stored.
- loggerName, loggerLevel: no need to explain.
So, that's why I need to have access to the "Log4jLogFactoryImpl" class.
Thanks.
Sorry, I should explain what I'm trying to do. I'm making a portlet which is able to add, dynamically, new loggers to Log4J system. These loggers can be configured, specifing the file where it writes, the layout, the maximum size of the file...
This is the example code:
DOMConfigurator domConfigurator = new DOMConfigurator();
domConfigurator.doConfigure(reader, LogManager.getLoggerRepository());
Log4JUtil.setLevel(loggerName, loggerLevel, false);
LogFactoryUtil.setLogFactory(new com.liferay.portal.log.Log4jLogFactoryImpl());
Variables:
- reader: where xml configuration logger is stored.
- loggerName, loggerLevel: no need to explain.
So, that's why I need to have access to the "Log4jLogFactoryImpl" class.
Thanks.
Sushil Kumar Saini,修改在11 年前。
RE: Using classes form portal-impl.jar
Regular Member 帖子: 104 加入日期: 11-7-27 最近的帖子
Hi Pepe,
For the functionality which you want to achieve, I think, you will have to configure your portlet to use the Portal Webapp Class Loader. In the tomcat you can do this by doing the following configuration in the context.xml
Cheers
Sushil Saini
For the functionality which you want to achieve, I think, you will have to configure your portlet to use the Portal Webapp Class Loader. In the tomcat you can do this by doing the following configuration in the context.xml
<context>
<loader loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader" />
</context>
Cheers
Sushil Saini
Sushil Kumar Saini,修改在11 年前。
RE: Using classes form portal-impl.jar
Regular Member 帖子: 104 加入日期: 11-7-27 最近的帖子
Go to the following link to see the complete detail.
http://www.liferay.com/community/wiki/-/wiki/Main/How+to+access+the+full+Liferay+API+from+a+portlet+application
http://www.liferay.com/community/wiki/-/wiki/Main/How+to+access+the+full+Liferay+API+from+a+portlet+application
Hi,
Thanks for your answers, but I'm not using Tomcat. My server is JBoss AS 7.1.2. Do you know how could I make it with this server?
Thanks again.
Thanks for your answers, but I'm not using Tomcat. My server is JBoss AS 7.1.2. Do you know how could I make it with this server?
Thanks again.
David H Nebinger,修改在11 年前。
RE: Using classes form portal-impl.jar
Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
I think the crux of the problem here is that you're trying to use the logging framework for something other than logging...
Logging is meant to push information relevant to a developer to a persistent store in order to help them diagnose issues.
A developer has no need for an ability to dynamically add loggers to the logging system, they are always statically defined and rarely (if ever) changed.
But if you're trying to use the logging framework for some other purpose (i.e. some sort of crazy auditing scheme), then I think what you're doing is trying to pound a square peg into a round hole...
However, since you seem totally set on this, the answer for your problem is to use reflection on the LogFactoryUtil class:
From here you can test to see if it is the Log4jLogFactoryImpl that you're looking for.
Since the liferayLogFactory class is actually in another class loader, you may not be able to cast and use it directly. You may need to use reflection exclusively to invoke methods on the returned value of logFactory.get(null) instead of trying to cast it to some type in your own class loader.
Logging is meant to push information relevant to a developer to a persistent store in order to help them diagnose issues.
A developer has no need for an ability to dynamically add loggers to the logging system, they are always statically defined and rarely (if ever) changed.
But if you're trying to use the logging framework for some other purpose (i.e. some sort of crazy auditing scheme), then I think what you're doing is trying to pound a square peg into a round hole...
However, since you seem totally set on this, the answer for your problem is to use reflection on the LogFactoryUtil class:
Field logFactory = LogFactoryUtil.class.getDeclaredField("_logFactory");
logFactory.setAccessible(true);
LogFactory liferayLogFactory = (LogFactory) logFactory.get(null);
From here you can test to see if it is the Log4jLogFactoryImpl that you're looking for.
Since the liferayLogFactory class is actually in another class loader, you may not be able to cast and use it directly. You may need to use reflection exclusively to invoke methods on the returned value of logFactory.get(null) instead of trying to cast it to some type in your own class loader.
Thanks David. You're very helpful.
Sushil Kumar Saini,修改在11 年前。
RE: Using classes form portal-impl.jar
Regular Member 帖子: 104 加入日期: 11-7-27 最近的帖子
Hi Pepe,.... Its good if your problem is solved ....
But if you want to user single class loader mechanism .. you can go to the link which i have provided... It tells you how to setup single class loader in jboss server.
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+API+from+Portlet#section-Access+Liferay+API+from+Portlet-JBoss-Tomcat
But if you want to user single class loader mechanism .. you can go to the link which i have provided... It tells you how to setup single class loader in jboss server.
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+API+from+Portlet#section-Access+Liferay+API+from+Portlet-JBoss-Tomcat