You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Magnolia CMS provided you with default audit logging service where some major actions such as login, logout, activate and deactivate actions are logged. Follow the same principle with some additional integrations here, I would like to introduce an approach on how to implement some extra logging information with minimal affect on existing code base.

Overview

An example use case is that we would like to log some info after execution of info.magnolia.importexport.command.JcrImportCommand where current implementation didn't provide any logging out of the box. We just don't want to alter existing code to include few lines of code, rebuild the whole bundle and redeploy them that takes us too much time and effort. Here we can find an easy way to achieve that with an affordable effort.

By utilizing AOP - Load time weaving, we just have to implement our Before or After advice for some information in this case.

Implementation

Add AspectJ libraries to your Maven project

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.9.1</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.1</version>
</dependency>

Implement your weaver config

Put your aop.xml under src/main/resource folder in development mode or right under WEB-INF/classes in production mode, notice underlying lines below:

Implement your Aspect

In this example, I'm using info.magnolia.audit.AuditLogger as our aspect file AuditLogger.java, just notice the advice below:

Enabling Load-Time Weaving

AspectJ load-time weaving can be enabled using AspectJ agent that can get involved in the class loading process and weave any types before they are defined in the VM. We specify the javaagent option to the JVM -javaagent:pathto/aspectjweaver.jar or using Maven plugin to configure the javaagent :

1
2
3
4
5
6
7
8
9
10
11
12
13
14

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <argLine>
            -javaagent:"${settings.localRepository}"/org/aspectj/
            aspectjweaver/${aspectj.version}/
            aspectjweaver-${aspectj.version}.jar
        </argLine>
        <useSystemClassLoader>true</useSystemClassLoader>
        <forkMode>always</forkMode>
    </configuration>
</plugin>

Verify your setup

Few lines of log should be printed out in your default log:


Further settings will be provided later. Hope this helps!


  • No labels