5

I am bored of adding manual logs for debugging each and every method that I write.

I came to know about @Loggable annotation of jcabi but am not successful in implementing that and your help is highly appreciated.

Below is the code that I have tried.

import com.jcabi.aspects.Loggable;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Jcabi {

    @Loggable
    private static String checkJcabi(String stringToPrint) {
        log.info("Print Successfull");
        return stringToPrint;
    }

    public static void main(String[] args) {
        checkJcabi("Hello World!");
    }

}

IDE console prints this:

[main] INFO com.amazon.optimus.cpamutil.utils.Jcabi - Print Successfull

This is the log for the log.info() I have added in the method and there is no log for the @Loggable annotation something like this (below) as mentioned in this post

[INFO] com.example.Foo #power(2, 10): 1024 in 12μs
[INFO] com.example.Foo #power(3, 3): 27 in 4μs

Below are the dependencies packages that I use:

JCabiAspects = 1.0;
AspectJ = 6.0;
Slf4j = 1.7;
Slf4j_Simple = 1.7;

Let me know if you need more details. Thanks.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
santhosh kumar
  • 1,981
  • 1
  • 9
  • 28

2 Answers2

0

You need to do weaving of your binaries, as explained here: http://aspects.jcabi.com/example-weaving.html

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 1
    How can that be done when you run your code not from maven but from an IDE like eclipse/Intellij through a test runner class like a JUnit class? – Abhijeet Vaikar Jul 16 '18 at 09:45
0

You need to do the binary weaving. In the post that you linked, it says so and it also says that you can use the jcabi-maven-plugin to do the weaving for you, given that you are using maven.

Just add a few dependencies to your classpath and configure jcabi-maven-plugin for aspect weaving (get their latest versions in Maven Central)

<project>
<dependencies>
    <dependency>
      <groupId>com.jcabi</groupId>
      <artifactId>jcabi-aspects</artifactId>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>ajc</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

If you are using gradle, you can use this gradle-aspectj-binary plugin

Imaduddin
  • 25
  • 1
  • 6