1 | // Copyright (c) 2002 Dustin Sallings <dustin@spy.net> |
2 | |
3 | package net.spy.memcached.compat.log; |
4 | |
5 | /** |
6 | * Logging implementation using |
7 | * <a href="http://jakarta.apache.org/log4j/docs/">log4j</a>. |
8 | */ |
9 | public class Log4JLogger extends AbstractLogger { |
10 | |
11 | // Can't really import this without confusion as there's another thing |
12 | // by this name in here. |
13 | private final org.apache.log4j.Logger l4jLogger; |
14 | |
15 | /** |
16 | * Get an instance of Log4JLogger. |
17 | */ |
18 | public Log4JLogger(String name) { |
19 | super(name); |
20 | |
21 | // Get the log4j logger instance. |
22 | l4jLogger=org.apache.log4j.Logger.getLogger(name); |
23 | } |
24 | |
25 | /** |
26 | * True if the underlying logger would allow debug messages through. |
27 | */ |
28 | @Override |
29 | public boolean isDebugEnabled() { |
30 | return(l4jLogger.isDebugEnabled()); |
31 | } |
32 | |
33 | /** |
34 | * True if the underlying logger would allow info messages through. |
35 | */ |
36 | @Override |
37 | public boolean isInfoEnabled() { |
38 | return(l4jLogger.isInfoEnabled()); |
39 | } |
40 | |
41 | /** |
42 | * Wrapper around log4j. |
43 | * |
44 | * @param level net.spy.compat.log.AbstractLogger level. |
45 | * @param message object message |
46 | * @param e optional throwable |
47 | */ |
48 | @Override |
49 | public void log(Level level, Object message, Throwable e) { |
50 | org.apache.log4j.Level pLevel=org.apache.log4j.Level.DEBUG; |
51 | |
52 | switch(level == null ? Level.FATAL : level) { |
53 | case DEBUG: |
54 | pLevel=org.apache.log4j.Level.DEBUG; |
55 | break; |
56 | case INFO: |
57 | pLevel=org.apache.log4j.Level.INFO; |
58 | break; |
59 | case WARN: |
60 | pLevel=org.apache.log4j.Level.WARN; |
61 | break; |
62 | case ERROR: |
63 | pLevel=org.apache.log4j.Level.ERROR; |
64 | break; |
65 | case FATAL: |
66 | pLevel=org.apache.log4j.Level.FATAL; |
67 | break; |
68 | default: |
69 | // I don't know what this is, so consider it fatal |
70 | pLevel=org.apache.log4j.Level.FATAL; |
71 | l4jLogger.log("net.spy.compat.log.AbstractLogger", pLevel, |
72 | "Unhandled log level: " + level |
73 | + " for the following message", null); |
74 | } |
75 | |
76 | l4jLogger.log("net.spy.compat.log.AbstractLogger", pLevel, message, e); |
77 | } |
78 | |
79 | } |