Fill access-log with additional information like siteKey
Question
Is it possible to add additional information in access-log (siteKey, username, etc.)?
Answer
It isn't possible per default, but with a RenderFilter additional parameters could be set into the session/request, which can be reused in the access-log.
Means you need a module where you implement this filter, it could look like (example add siteKey):
Filter
Before Jahia 8.2
package org.jahia.module.test.filter;
import org.jahia.services.render.RenderContext;
import org.jahia.services.render.Resource;
import org.jahia.services.render.filter.AbstractFilter;
import org.jahia.services.render.filter.RenderChain;
public class SetSiteKeyFilter extends AbstractFilter {
@Override
public String execute(String previousOut, RenderContext renderContext, Resource resource, RenderChain chain) throws Exception {
String siteKey = "-";
if (renderContext.getSite() != null) {
siteKey = renderContext.getSite().getName();
}
renderContext.getRequest().setAttribute("siteKey", siteKey);
return previousOut;
}
}
As you can see in the execute method of this filter, I put in the request an attribute with name siteKey and the current sitekey if it is available.
The filter must be configured in spring in the same module, like:
<bean class="org.jahia.module.test.filter.SetSiteKeyFilter">
<property name="priority" value="3"/>
<property name="applyOnConfigurations" value="page"/>
</bean>
Means for every request on a jahia page this filter is called, important the priority must be lower than 16, I chose in the example 3.
With Jahia 8.2
Instead of declaring the previous source code through Spring, you must declare it through OSGI annotations. Here is a sample source code to do so.
Log configuration
And at least in the tomcat/conf/server.xml you have to add in the pattern "%{siteKey}r" like:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="access-log" suffix=".txt"
pattern="%{siteKey}r %h %l %u %t "%r" %s %b" />
In my case, the siteKey is on first position, so I get a log like:
- 127.0.0.1 - - [11/Feb/2021:13:23:41 +0100] "POST /gwt/contentManager.gwt?lang=en&site=6bd3bc8d-6afb-42d2-bb50-664ec6a6cade&workspace=default&windowId=1080600026 HTTP/1.1" 200 1572
digitall 127.0.0.1 - - [11/Feb/2021:13:23:43 +0100] "GET /cms/editframe/default/en/sites/digitall/home/test.html HTTP/1.1" 200 5666
- 127.0.0.1 - - [11/Feb/2021:13:23:43 +0100] "GET /modules/dx-base-demo-templates/javascript/custom/template.js HTTP/1.1" 200 361
- 127.0.0.1 - - [11/Feb/2021:13:23:43 +0100] "GET /modules/dx-base-demo-templates/javascript/parallax-slider.js HTTP/1.1" 200 515
As you can see, for the url /cms/editframe/default/en/sites/digitall/home/test.html the site digitall is correctly added to the access log, other resources which don't have a sitekey, are logged with a -