1 | // Copyright (c) 2002 Dustin Sallings <dustin@spy.net> |
2 | |
3 | package net.spy.memcached.compat.log; |
4 | |
5 | /** |
6 | * Logging implementation using the sun logger. |
7 | */ |
8 | public class SunLogger extends AbstractLogger { |
9 | |
10 | // Can't really import this without confusion as there's another thing |
11 | // by this name in here. |
12 | private final java.util.logging.Logger sunLogger; |
13 | |
14 | /** |
15 | * Get an instance of SunLogger. |
16 | */ |
17 | public SunLogger(String name) { |
18 | super(name); |
19 | |
20 | // Get the sun logger instance. |
21 | sunLogger=java.util.logging.Logger.getLogger(name); |
22 | } |
23 | |
24 | /** |
25 | * True if the underlying logger would allow Level.FINE through. |
26 | */ |
27 | @Override |
28 | public boolean isDebugEnabled() { |
29 | return(sunLogger.isLoggable(java.util.logging.Level.FINE)); |
30 | } |
31 | |
32 | /** |
33 | * True if the underlying logger would allow Level.INFO through. |
34 | */ |
35 | @Override |
36 | public boolean isInfoEnabled() { |
37 | return(sunLogger.isLoggable(java.util.logging.Level.INFO)); |
38 | } |
39 | |
40 | /** |
41 | * Wrapper around sun logger. |
42 | * |
43 | * @param level net.spy.compat.log.AbstractLogger level. |
44 | * @param message object message |
45 | * @param e optional throwable |
46 | */ |
47 | @Override |
48 | public void log(Level level, Object message, Throwable e) { |
49 | java.util.logging.Level sLevel=java.util.logging.Level.SEVERE; |
50 | |
51 | switch(level == null ? Level.FATAL : level) { |
52 | case DEBUG: |
53 | sLevel=java.util.logging.Level.FINE; |
54 | break; |
55 | case INFO: |
56 | sLevel=java.util.logging.Level.INFO; |
57 | break; |
58 | case WARN: |
59 | sLevel=java.util.logging.Level.WARNING; |
60 | break; |
61 | case ERROR: |
62 | sLevel=java.util.logging.Level.SEVERE; |
63 | break; |
64 | case FATAL: |
65 | sLevel=java.util.logging.Level.SEVERE; |
66 | break; |
67 | default: |
68 | // I don't know what this is, so consider it fatal |
69 | sLevel=java.util.logging.Level.SEVERE; |
70 | sunLogger.log(sLevel, "Unhandled log level: " + level |
71 | + " for the following message"); |
72 | } |
73 | |
74 | // Figure out who was logging. |
75 | Throwable t=new Throwable(); |
76 | StackTraceElement[] ste=t.getStackTrace(); |
77 | StackTraceElement logRequestor=null; |
78 | String alclass=AbstractLogger.class.getName(); |
79 | for(int i=0; i<ste.length && logRequestor==null; i++) { |
80 | if(ste[i].getClassName().equals(alclass)) { |
81 | // Make sure there's another stack frame. |
82 | if(i+1<ste.length) { |
83 | logRequestor=ste[i+1]; |
84 | if(logRequestor.getClassName().equals(alclass)) { |
85 | logRequestor=null; |
86 | } // Also AbstractLogger |
87 | } // Found something that wasn't abstract logger |
88 | } // check for abstract logger |
89 | } |
90 | |
91 | // See if we could figure out who was doing the original logging, |
92 | // if we could, we want to include a useful class and method name |
93 | if(logRequestor!=null) { |
94 | if(e != null) { |
95 | sunLogger.logp(sLevel, logRequestor.getClassName(), |
96 | logRequestor.getMethodName(), message.toString(), e); |
97 | } else { |
98 | sunLogger.logp(sLevel, logRequestor.getClassName(), |
99 | logRequestor.getMethodName(), message.toString()); |
100 | } |
101 | } else { |
102 | if(e != null) { |
103 | sunLogger.log(sLevel, message.toString(), e); |
104 | } else { |
105 | sunLogger.log(sLevel, message.toString()); |
106 | } |
107 | } |
108 | } |
109 | |
110 | } |