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.
It is possible with any logging framework. You can use AOP to create a "logging" aspect around your service methods. Here is some example.
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.
精彩评论