使用Log4j的AspectJ日志 [英] AspectJ logs with Log4j

查看:92
本文介绍了使用Log4j的AspectJ日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将AspectJ日志记录实施到模块以更好地对其进行分析.我的应用程序已经在使用log4J框架.
如果我在单独的应用程序中运行AspectJ类,则可以正常工作,但将其与应用程序集成时则无法工作.这是我做的
applicationContext.xml

I am trying to implement AspectJ logging to module to analyze it better. My application is already using log4J framework.
If I run AspectJ class in a separate application it works fine but it doesn't work when I integrate it with application. Here what I have done
applicationContext.xml

<aop:aspectj-autoproxy/>
<bean id="logAspectj" class="com.test.aspect.LoggingAspect"/>

LoggingAspect类

LoggingAspect class

@Component
@Aspect
public class LoggingAspect {

    private final Log log = LogFactory.getLog(this.getClass());

    @Around("execution(public void com.db.TestMarshaller.saveDoc(..))")
    public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {

            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            Object retVal = joinPoint.proceed();

            stopWatch.stop();

            StringBuffer logMessage = new StringBuffer();
            logMessage.append(joinPoint.getTarget().getClass().getName());
            logMessage.append(".");
            log.info(logMessage.toString());
            return retVal;
    }

}

注意:此处TestMarshaller不会通过Spring公开.

Log4j是否有一些特定的AspectJ设置?

Is there some specific AspectJ setting for Log4j?

推荐答案

由于您的com.db.TestMarshaller不是春季托管的"(您的上下文中没有该类型的Spring bean),因此使用的spring命名空间将创建一个可以调用您的Aspect的代理.

since your com.db.TestMarshaller is not "spring managed" (there is no Spring bean of that type in your context), the used spring namespace will NOT create a proxy which calls your Aspect.

在这种情况下,您将不得不使用AspectJ编译器(编译时编织)编译源代码,或者在加载类时使用Aspectj运行时来修改字节码(加载时编织).

In this case you'll either have to compile your sources with the AspectJ Compiler (Compile Time Weaving) or use the aspectj runtime to modify your bytecode when your class is loaded (Load Time Weaving).

这实际上取决于您的用例,如果您的Aspect是业务逻辑的一部分并且在运行时不能更改,请使用Aspectj编译器,因为它只会执行一次.如果您需要一个更具动态性的方面的应用程序,请改用加载时间编织.

It really depends on your use case which way you go - if your Aspect is part of your business logic and can not change during runtime, use the aspectj compiler, since it will only be done once. I case you need a more dynamic application of your aspect, use load time weaving instead.

希望这会有所帮助, 乔钦(Jochen)

Hope this helps, Jochen

这篇关于使用Log4j的AspectJ日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆