Foren

Best practice to set log4j log level dynamically per portlet

Paul Cahill, geändert vor 6 Jahren.

Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
What is the best way to set the log4j log level to 'debug' for a running portlet? Below is the sample log4j properties file that I'm using for my portlet. I was thinking of using "monitorInterval=30" to refresh the configuration from the log4j.properties file, but I can't figure out where to edit the log4j.properties file to set the log level to debug (or whatever level I want). My goal is to have one log file per portlet where the log level can be changed if needed without redeploying anything.

Thanks

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/sample-portlet.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.logger.org.springframework=INFO
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
If you use the Liferay logging facilities, you can just change in the control panel and it takes effect immediately.










Come meet me at the 2017 LSNA!
Paul Cahill, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
Thanks David. I did get this to work in the manner you suggested by using:

private static Log log = LogFactoryUtil.getLog(MyViewController.class);

Is there a way to have this work without specifying the Liferay classes specifically? In other words instead of using com.liferay.portal.kernel.log.Log, can I use log4j or slf4j which may make my portlet more portable.

Thanks for the help.
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Portable? Portlets can only go to other portlet containers, typically Liferay in our case?

I'm sure we're probably dancing around different things here, i.e. you are probably asking how to promote your liferay logging for a project from environ to environ, but it's just not 100% certain that is actually where you're going...








Come meet me at the 2017 LSNA!
Paul Cahill, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
Right...we're trying to be portlet container agnostic.
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
That's just the thing, I guess, they shouldn't be.

You will want debug to be on, but only in debug. In production, you would only want warn & error. Test will often have debug enabled, but in QA you may actually do a mix.

If the response is "no we want debug on in all environments" then you're kind of expecting the wrong things in production. Debug logging is a performance hit, it will typically leak security details, it has a big filesystem impact, ... All of these things are fine in lower environments or even production temporarily to resolve a problem, but it is never meant to be an ongoing solution in each environment, especially production.









Come meet me at the 2017 LSNA!
Paul Cahill, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
I appreciate the feedback. Let me know if you'd rather me email you directly on any of this.

When you say "That's just the thing, I guess, they shouldn't be." Can you elaborate on that?

In terms of changing the log levels, one of your last points is what we're looking to do - turn on debug logging temporarily to resolve an issue in production.
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Paul Cahill:
In terms of changing the log levels, one of your last points is what we're looking to do - turn on debug logging temporarily to resolve an issue in production.


A temporary change would not be done by deploying a log4j config file, that represents a permanent change.

Maybe I'm still missing something here. If you want to turn on temporarily, the control panel can be used for that, but for some reason that isn't clear you don't want to use the control panel but some sort of more permanent deployment artifact?








Come meet me at the 2017 LSNA!
Paul Cahill, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
I think we're almost there...

I am able to set the logging level using the control panel, but only if I use the Liferay logger in my code:

private static com.liferay.portal.kernel.log.Log log = LogFactoryUtil.getLog(MyViewController.class);

If I use log4j in my code, I can't set the logging level using the control panel:

private static final org.apache.log4j.Logger log = Logger.getLogger(DEAAuditViewController.class);

Is it possible to use log4j and be able to set the log level via the control panel?
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Oh, now I see.

No, because when you use log4j specifically you actually have a separate log manager from log4j; a separate one for each time you use log4j. The portal has no visibility on the separate log managers and cannot configure them.

That's pretty much the whole reason for the Liferay logging facility, a single log manager that Liferay manages and therefore the control panel can configure log levels.

So basically if you commit to using log4j, you commit to managing your log levels yourself.






Come meet me at the 2017 LSNA!
Paul Cahill, geändert vor 6 Jahren.

RE: Best practice to set log4j log level dynamically per portlet

New Member Beiträge: 12 Beitrittsdatum: 02.09.16 Neueste Beiträge
Got it. Thanks again David.