Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed2b3aa12ee340721a3d7c0c32ef817f2a6359cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2008, 2010 VMware Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   VMware Inc. - initial contribution
 *******************************************************************************/

package org.eclipse.virgo.medic.log;

import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An aspect that will advise any method with entry and exit trace logging.
 * 
 * <strong>Concurrent Semantics</strong><br />
 * 
 * Threadsafe
 * 
 */
public aspect EntryExitTrace pertypewithin(*) {
	
	private volatile Logger logger;

    pointcut medic() : within(org.eclipse.virgo.medic..*);
    
    pointcut logback() : within(ch.qos.logback..*) || within(org.slf4j.impl..*);
    
    pointcut infoCandidate() : execution(public * *(..)) && !medic() && !logback();
    
    pointcut debugCandidate() : execution(!public !private * *(..)) && !medic() && !logback();
    
    pointcut traceCandidate() : execution(private * *(..)) && !medic() && !logback();
    
    before() : infoCandidate() {
        getLogger(thisJoinPointStaticPart).info("{} {}", ">", getSignature(thisJoinPointStaticPart));
    }

    after() returning : infoCandidate()  {
        getLogger(thisJoinPointStaticPart).info("{} {}", "<", getSignature(thisJoinPointStaticPart));
    }

    after() throwing(Throwable t) : infoCandidate()  {
        Logger logger = getLogger(thisJoinPointStaticPart);
        if (logger.isInfoEnabled()) {
            logger.info(String.format("< %s", getSignature(thisJoinPointStaticPart)), t);
        }
    }

    before() : debugCandidate() {
        getLogger(thisJoinPointStaticPart).debug("{} {}", ">", getSignature(thisJoinPointStaticPart));
    }

    after() returning : debugCandidate()  {
        getLogger(thisJoinPointStaticPart).debug("{} {}", "<", getSignature(thisJoinPointStaticPart));
    }

    after() throwing(Throwable t) : debugCandidate()  {
        Logger logger = getLogger(thisJoinPointStaticPart);
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("< %s", getSignature(thisJoinPointStaticPart)), t);
        }
    }
    
    before() : traceCandidate() {
        getLogger(thisJoinPointStaticPart).trace("{} {}", ">", getSignature(thisJoinPointStaticPart));
    }

    after() returning : traceCandidate()  {
        getLogger(thisJoinPointStaticPart).trace("{} {}", "<", getSignature(thisJoinPointStaticPart));
    }

    after() throwing(Throwable t) : traceCandidate()  {
        Logger logger = getLogger(thisJoinPointStaticPart);
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("< %s", getSignature(thisJoinPointStaticPart)), t);
        }
    }

    private Logger getLogger(JoinPoint.StaticPart sp) {
    	if (this.logger == null) {
    		this.logger = LoggerFactory.getLogger(sp.getSignature().getDeclaringType());
    	}
    	return this.logger;
    }

    private String getSignature(JoinPoint.StaticPart sp) {
        return sp.getSignature().toLongString();
    }
}

Back to the top