Fórum

How to log additional data - user name and ip

thumbnail
Andrzej Drozdowski, modificado 11 Anos atrás.

How to log additional data - user name and ip

Junior Member Postagens: 27 Data de Entrada: 03/09/10 Postagens Recentes
Salvete!

Do you know the way how to add additional data to logs for example current user name, IP address.
Is it possible to use org.apache.log4j.Logger with org.apache.log4j.MDC
Something like this:
MDC.put("ip", PortalUtil.getHttpServletRequest(request).getRemoteAddr() );
MDC.put("user", request.getRemoteUser());

With appender configuration:
<appender name="MY_APPENDER" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%X{ip}][%X{user}][%c{1}:%L] %m%n">
		</layout>
	</appender>

I have tried to add this to configuration in portal-log4j-ext.xml but it does not work.

Liferay 6.1.1 on Tomcat

Lectori Benevolo Salutem
A.D.
Ruben Martin Pozo, modificado 11 Anos atrás.

RE: How to log additional data - user name and ip

New Member Postagens: 6 Data de Entrada: 07/12/12 Postagens Recentes
We have the same problem here

The problem is related to Liferay's classloaders. It seems to be two different classloaders loading the same log4j.jar library. One is used when your code is calling MDC methods. The other one is used when you're actually logging. There are two different instances of MDC loaded by two different classloaders.

It seems that Liferay is copying the log4j.jar into the WEB-INF/lib folder of the WAR when the application is deployed. We haven't completely understood that yet but that seems to be the root of the problem.
thumbnail
David H Nebinger, modificado 11 Anos atrás.

RE: How to log additional data - user name and ip

Liferay Legend Postagens: 14914 Data de Entrada: 02/09/06 Postagens Recentes
You should not be using log4j directly in your code. Instead you should be using Liferay's LogFactoryUtil.getLog() method.

This will get you out of the class loader issue.

To answer the OP's question, you can define a servlet filter for the ROOT application that would set the necessary values in the log4j MDC. Note that the change would go into portal-log4j-ext.properties in ROOT/WEB-INF/classes folder.
Ruben Martin Pozo, modificado 11 Anos atrás.

RE: How to log additional data - user name and ip

New Member Postagens: 6 Data de Entrada: 07/12/12 Postagens Recentes
If I understand what you're saying we should modify the web.xml of the ROOT application to add our own filter. Is that correct?

Isn't there a better way to do this?. Modifying this file in every node of the production cluster doesn't seem very elegant. Could we use a hook or an extension?
thumbnail
David H Nebinger, modificado 11 Anos atrás.

RE: How to log additional data - user name and ip

Liferay Legend Postagens: 14914 Data de Entrada: 02/09/06 Postagens Recentes
Check out the audit filter. It will define a thread local w/ auditing data. The thread local will have visibility to any code running within the same thread (i.e. any code that is executed in response to an incoming server request).
Eric Smith, modificado 9 Anos atrás.

RE: How to log additional data - user name and ip

Junior Member Postagens: 66 Data de Entrada: 28/08/12 Postagens Recentes
I need to get some custom information showing up in each log entry (going to be userID). I'm having problems getting anything to show up. I've followed your steps, but I'm missing something because it's not working for me. I've done the following and my information isn't showing up in my console logs:

- Deployed a custom filter (hook) with an MDC.put("logID", "my log ID"); in doFilter()
- Added [%X{logID}] to portal-log4j-ext.xml in my extension, both console and file appenders
ex. <param name="ConversionPattern" value="[%X{logID}] %d{yyyy-MM-dd - HH:mm:ss.SSSS} %-5p [%t][%c{1}:%L] %m%n" />
- I'm using LogFactoryUtil.getLog() in my hook that customizes the search portlet
- I have System.out.println() in multiple locations so my filter and search hook are definitely being called
- However I still get this in my console log, where {logID} is empty:
[] 2014-07-18 - 13:56:48.0915 INFO [http-bio-9090-exec-19][GetAssetCount:85] User: 314301, Searching: bob

Any idea what I'm doing wrong? I assume that the custom filter and the logger are on different threads, but I don't know how to fix the problem.

Thanks.
thumbnail
Steffen Schuler, modificado 8 Anos atrás.

RE: How to log additional data - user name and ip

Regular Member Postagens: 137 Data de Entrada: 12/05/08 Postagens Recentes
In 6.2EE-GA1-SP14 this works if you provide the slf4j-log4j12-172.jar and remove the util-slf4j.jar from your liferay-plugin-package.properties... it's another liferay limitation.
...
portal-dependency-jars=\
slf4j-api.jar,\
util-slf4j.jar
...

The mess is described here:
https://issues.liferay.com/browse/LPS-44900
https://www.liferay.com/de/web/denis-signoretto/blog/-/blogs/using-slf4j-and-liferay-logging-framework-in-custom-plugins#_33_message_35379678
thumbnail
Sushil Kumar Saini, modificado 11 Anos atrás.

RE: How to log additional data - user name and ip

Regular Member Postagens: 104 Data de Entrada: 27/07/11 Postagens Recentes
Hi Andrzej,

I have also tried the similar configuration for printing the jsession id in the logs. For me it is working fine for file appender but is not working for console appender.


MDC.put("jsessionid", session.getId() );




log4j.rootLogger=INFO, CATALINA, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yy-MM-dd HH:mm:ss.SSS}] [%p] &lt;%t&gt; [%X{jsessionId}] [%c] %m%n

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
#log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
#log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CATALINA.layout.ConversionPattern=[%d{yy-MM-dd HH:mm:ss.SSS}] [%p] &lt;%t&gt; [%X{jsessionId}] [%c] %m%n



Regards
Sushil Saini
Ravi Shah, modificado 10 Anos atrás.

RE: How to log additional data - user name and ip

Junior Member Postagens: 59 Data de Entrada: 02/01/14 Postagens Recentes
Hi Sushil,

I am also trying to configure the same. For me also it is not working in Case of printing it in File.

I am using Liferay 6.0.6.

Here is my code for portal-log4j-ext.xml :
<appender name="REST" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.base}/logs/rest.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] %X{email} %X{deviceId} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.StringMatchFilter">
<param name="StringToMatch" value="RESTLOG" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>


And am trying to access it using org.apache.log4j.MDC :

MDC.put("email", email);
MDC.put("deviceId", deviceId);


But every time it is printing blank values. Please let me know if there is any solution available ...
Mohammed Shamshuddin, modificado 9 Anos atrás.

RE: How to log additional data - user name and ip

New Member Postagens: 4 Data de Entrada: 25/10/11 Postagens Recentes
Hi ,

I am also looking out for solution. Please let me know if any one implemented it successfully. ( I am using Liferay 6.0.11 / 6.0.5)

I am going to print userId in every log statement. But it seems when I use MDC.put("userId",userId) in service pre events hook and removing that user id from service post event hook . But in the actual log it is not printing , it is just empty.

Don't know what exactly the issues is.

For logging I am using Liferay's LogFactoryUtil.getLog(class).

Your suggestions are highly appreciable.