Page tree

Michael O'Brien

Skip to end of metadata
Go to start of metadata


Java Logging

Logging with Spring AOP (before/after) - work I did in ONAP - https://wiki.onap.org/display/DW/Logging+Developer+Guide

Logback in spring boot - https://www.baeldung.com/spring-boot-logginghttps://www.mkyong.com/logging/log4j-hello-world-example/


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;
@Service("daoFacade")
public class ApplicationService implements ApplicationServiceLocal {
    @Override
    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;

@Aspect
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(
                LoggerFactory.getLogger(joinPoint.getTarget().getClass()));
        try {
            adapter.entering(requestAdapter);
        } finally {
        }
    }

    @After("execution(* cloud.obrienlabs.*.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        final LogAdapter adapter = new LogAdapter(
                LoggerFactory.getLogger(joinPoint.getTarget().getClass()));
        adapter.exiting();
    }

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