rendered paste body
public class TestLoggin {
public static void main(String[] args) {
PropertyConfigurator.configureAndWatch("log4j.properties",500);
Log.security.info("Access denied: Username [ abonte ] ...");
Log.performance.info("Log4j is too slow...");
Log.business.fatal("Something went wrong ...");
plop("coin");
plop(5L,new Date());
}
public static void plop(String meuh) {}
public static void plop(long l, Date d) {}
}
[business] TRACE - >> void com.bleh.TestLoggin.main(String[]) with [[Ljava.lang.String;@fe1feb]
[security] INFO - Access denied: Username [ abonte ] ...
[performance] INFO - Log4j is too slow...
[business] FATAL - Something went wrong ...
[business] TRACE - >> void com.bleh.TestLoggin.plop(String) with [coin]
[performance] INFO - void com.bleh.TestLoggin.plop(String) Duration 0 ms
[business] TRACE - << void com.bleh.TestLoggin.plop(String) with [coin]
[business] TRACE - >> void com.bleh.TestLoggin.plop(long, Date) with [5, Wed May 30 14:23:34 CEST 2007]
[performance] INFO - void com.bleh.TestLoggin.plop(long, Date) Duration 0 ms
[business] TRACE - << void com.bleh.TestLoggin.plop(long, Date) with [5, Wed May 30 14:23:34 CEST 2007]
[performance] INFO - void com.bleh.TestLoggin.main(String[]) Duration 15 ms
[business] TRACE - << void com.bleh.TestLoggin.main(String[]) with [[Ljava.lang.String;@fe1feb]
public aspect TraceAspect {
pointcut publicMethods() : execution(public * **..*(..))
&& !within(TraceAspect)
&& !within(BenchmarkAspect);
pointcut loggableCalls() : publicMethods();
before() : loggableCalls() {
Log.business.trace(">> " + buildLogInfo(thisJoinPoint));
}
after() : loggableCalls() {
Log.business.trace("<< " + buildLogInfo(thisJoinPoint));
}
private String buildLogInfo(JoinPoint joinPoint) {
Object [] args = joinPoint.getArgs() ;
return new StringBuilder(joinPoint.getSignature().toString())
.append(" with ")
.append( args.length == 0 ? "no args" : Arrays.asList(joinPoint.getArgs()).toString() ).toString();
}
}
public aspect BenchmarkAspect {
private static Logger logger = Logger.getLogger("bench");
pointcut publicMethods() : execution(public * **..*(..));
Object around() : publicMethods() {
final long start = System.currentTimeMillis();
try {
return proceed();
} finally {
final long end = System.currentTimeMillis();
Log.performance.info(thisJoinPoint.getSignature().toString()+"\tDuration "+(end-start)+" ms") ;
}
}
}