dmay
dmay

Reputation: 1315

Logging whole flow of application for easy reading

In my web application(Spring based), i have simple layered architecture as Service->Manager->Dao->database. For logging purpose i want to log request coming to Service and then exit from Service at one go so that it is easy to debug issues. Otherwise logs contain various output from different threads intermingle with each other which is not easy to read. Is it possible with existing logging framework like log4j.

Upvotes: 1

Views: 190

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

You can use Spring AOP to implement such logs. Here's an example:

@Aspect
public class LoggingAspect {

    private static final Logger LOG = LoggerFactory.getLogger(LoggingAspect.class);

    @Pointcut("call(* com.yourcompany.*.*(..))")
    public void serviceMethod() {
    }

    @Before("serviceMethod()")
    public void logMethodCalls(final JoinPoint joinPoint) {
    if (LOG.isDebugEnabled())
            LOG.debug("Calling method {} with args {}",
                joinPoint.getSignature(), joinPoint.getArgs());
    }

}

Just wire it as a Spring Bean:

<bean class="com.somepackage.LoggingAspect" />
<aop:aspectj-autoproxy/>

and calls to public methods of Spring beans in the matched packages should be logged.

Upvotes: 1

Andrey Adamovich
Andrey Adamovich

Reputation: 20663

It is possible with any logging framework. You can use AOP to create a "logging" aspect around your service methods. Here is some example.

Upvotes: 1

Related Questions