[logback-user] Aspect & NDC

José Juan Montiel josejuan.montiel at eurobits.es
Fri Feb 4 08:56:58 CET 2011


Hi,

i'm implementing a loggin via AspectJ in a Maven project.

My aspect class is something like this...


public aspect TraceMethodCalls {

    static Marker ASPECTJ_TRACE_ENTER =
MarkerFactory.getMarker("ASPECTJ_TRACE_ENTER");

    final Logger logger = LoggerFactory.getLogger(TraceMethodCalls.class);

    TraceMethodCalls() {
    }

    pointcut traceMethods()
        //give me all method calls of every class with every visibility
        : (execution(* *.*(..))
        //give me also constructor calls
        || execution(*.new(..)))
        && !execution(* toString(..)) && !execution(* hashCode(..))
        //stop recursion don't get method calls in this aspect class itself
        && !within(TraceMethodCalls);

    //advice before: do something before method is really executed
    before() : traceMethods() {
        if (logger.isDebugEnabled()) {
            //get info about captured method and log it
            Signature sig = thisJoinPointStaticPart.getSignature();

            logger.trace(ASPECTJ_TRACE_ENTER, "---> [" + sig.toShortString()
+ "]");

        }
    }

    //advice before: do something before method is really executed
    after() returning (Object o): traceMethods() {
        if (logger.isDebugEnabled()) {
            //get info about captured method and log it
            Signature sig = thisJoinPointStaticPart.getSignature();

            logger.trace(ASPECTJ_TRACE_ENTER, "<--- [" + sig.toShortString()
+ "]");

        }
    }

and logback appender


   <appender name="ASPECTJ_TRACE_ENTER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator
class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
                  <expression>
                    (marker != null) &amp;&amp;
(marker.contains("ASPECTJ_TRACE_ENTER"))
                  </expression>
            </evaluator>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <prudent>${prudent}</prudent>

        <!-- full stacktrace para las excepciones -->
        <encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%date %-5level - cn=%contextName - [%thread] -
%message%n</Pattern>
        </encoder>

        <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

<FileNamePattern>${USER_HOME}/${backupDir}aspectj.enter_exit.%d${patronRolling}</FileNamePattern>

            <MaxHistory>${maxHistory}</MaxHistory>
        </rollingPolicy>
      </appender>

I see that that support to NDC is disabled in logback, how could i implement
a TAB of this messages without NCD?

--->Method1
------>Method2
--------->Method3
<---------Method3
<------Method2
<---Method1


Thanks.

-- 
Jose Juan Montiel Martinez
Eurobits Technologies
Calle Musgo 3, 1ª Planta
28023 Madrid
T +34-917080300
F +34-913077480
josejuan.montiel at eurobits.es
http://www.eurobits.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/logback-user/attachments/20110204/3bc2d99d/attachment.html>


More information about the Logback-user mailing list