Page tree
Skip to end of metadata
Go to start of metadata

Java Logging

Logging with Spring AOP (before/after) - work I did in ONAP -

Logback in spring boot -

Logging With Spring AOP

Prototyped AOP advice - minimal client changes - just an aspect bean and annotations required

import javax.servlet.http.HttpServletRequest;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
public class ApplicationService implements ApplicationServiceLocal {
    public Boolean health(HttpServletRequest servletRequest) {
    	Boolean health = true;
    	// TODO: check database
    	// Log outside the AOP framework - to simulate existing component logs between the ENTRY/EXIT markers
    	LoggerFactory.getLogger(this.getClass()).info("Running /health");
    	return health;

Aspect References

package cloud.obrienlabs.demo.logging;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import cloud.obrienlabs.logging.LogAdapter;
import org.slf4j.LoggerFactory;

public class LoggingAspect {
    @Before("execution(* cloud.obrienlabs.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Object servletRequest = args[0];
        LogAdapter.HttpServletRequestAdapter requestAdapter = 
                new LogAdapter.HttpServletRequestAdapter((HttpServletRequest)servletRequest);
        final LogAdapter adapter = new LogAdapter(
        try {
        } finally {

    @After("execution(* cloud.obrienlabs.*.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        final LogAdapter adapter = new LogAdapter(

Logging Results

Use Case: Single REST call - with ENTRY/EXIT Markers around in-method log

The key here is that you get logs for free - the entry/exit lines are generated - the line in the middle is from java application code.

ELK Stack

  • No labels