KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > logging > LoggerIdiom


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.logging;
9
10 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
11
12 /**
13  * This aspect shows how to implement logging modules using Log4j, 1.4 Logger etc.
14  * (currently showing the use of 1.4 Logger API).
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public class LoggerIdiom {
19
20     /**
21      * @Around("methodToLog && target(loggable)")
22      */

23     public Object JavaDoc log(StaticJoinPoint jp, Loggable loggable) throws Throwable JavaDoc {
24         loggable.getLog().log(Level.ALL, "entering " + jp.getSignature());
25         Object JavaDoc result = jp.proceed();
26         loggable.getLog().log(Level.ALL, "exiting " + jp.getSignature());
27         return result;
28     }
29
30     /**
31      * @Mixin(
32      * pointcut="loggableClasses",
33      * deploymentModel="perClass"
34      * )
35      */

36     public static class LoggableImpl implements Loggable {
37
38         private final Logger LOG;
39
40         public LoggableImpl(Class JavaDoc targetClass) {
41             LOG = Logger.getLogger(targetClass.getName());
42         }
43
44         public Logger getLog() {
45             return LOG;
46         }
47     }
48
49     public static interface Loggable {
50         Logger getLog();
51     }
52
53     // some backport of Java 1.4 logging so that the sample can run on 1.3
54
//TODO : else move to jdk15 samples
55
static class Level {
56         static final Level ALL = new Level();
57     }
58     static class Logger {
59         private static Logger SINGLETON = new Logger();
60         static Logger getLogger(String JavaDoc name) {
61             return SINGLETON;
62         }
63         void log(Level level, String JavaDoc m) {
64             System.out.println("examples.logging.Logger : " + m);
65         }
66     }
67 }
68
69
Popular Tags