KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > util > SessionLog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.util;
21
22 import org.apache.mina.common.IoSession;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Provides utility methods to log protocol-specific messages.
28  * <p>
29  * Set {@link #PREFIX} and {@link #LOGGER} session attributes
30  * to override prefix string and logger.
31  *
32  * @author The Apache Directory Project (mina-dev@directory.apache.org)
33  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
34  *
35  */

36 public class SessionLog {
37     /**
38      * Session attribute key: prefix string
39      */

40     public static final String JavaDoc PREFIX = SessionLog.class.getName() + ".prefix";
41
42     /**
43      * Session attribute key: {@link Logger}
44      */

45     public static final String JavaDoc LOGGER = SessionLog.class.getName() + ".logger";
46
47     private static Class JavaDoc getClass(IoSession session) {
48         return session.getHandler().getClass();
49     }
50
51     public static void debug(IoSession session, String JavaDoc message) {
52         Logger log = getLogger(session);
53         if (log.isDebugEnabled()) {
54             log.debug(String.valueOf(session.getAttribute(PREFIX)) + message);
55         }
56     }
57
58     public static void debug(IoSession session, String JavaDoc message, Throwable JavaDoc cause) {
59         Logger log = getLogger(session);
60         if (log.isDebugEnabled()) {
61             log.debug(String.valueOf(session.getAttribute(PREFIX)) + message,
62                     cause);
63         }
64     }
65
66     public static void info(IoSession session, String JavaDoc message) {
67         Logger log = getLogger(session);
68         if (log.isInfoEnabled()) {
69             log.info(String.valueOf(session.getAttribute(PREFIX)) + message);
70         }
71     }
72
73     public static void info(IoSession session, String JavaDoc message, Throwable JavaDoc cause) {
74         Logger log = getLogger(session);
75         if (log.isInfoEnabled()) {
76             log.info(String.valueOf(session.getAttribute(PREFIX)) + message,
77                     cause);
78         }
79     }
80
81     public static void warn(IoSession session, String JavaDoc message) {
82         Logger log = getLogger(session);
83         if (log.isWarnEnabled()) {
84             log.warn(String.valueOf(session.getAttribute(PREFIX)) + message);
85         }
86     }
87
88     public static void warn(IoSession session, String JavaDoc message, Throwable JavaDoc cause) {
89         Logger log = getLogger(session);
90         if (log.isWarnEnabled()) {
91             log.warn(String.valueOf(session.getAttribute(PREFIX)) + message,
92                     cause);
93         }
94     }
95
96     public static void error(IoSession session, String JavaDoc message) {
97         Logger log = getLogger(session);
98         if (log.isErrorEnabled()) {
99             log.error(String.valueOf(session.getAttribute(PREFIX)) + message);
100         }
101     }
102
103     public static void error(IoSession session, String JavaDoc message, Throwable JavaDoc cause) {
104         Logger log = getLogger(session);
105         if (log.isErrorEnabled()) {
106             log.error(String.valueOf(session.getAttribute(PREFIX)) + message,
107                     cause);
108         }
109     }
110
111     public static boolean isDebugEnabled(IoSession session) {
112         return getLogger(session).isDebugEnabled();
113     }
114
115     public static boolean isInfoEnabled(IoSession session) {
116         return getLogger(session).isInfoEnabled();
117     }
118
119     public static boolean isWarnEnabled(IoSession session) {
120         return getLogger(session).isWarnEnabled();
121     }
122
123     public static boolean isErrorEnabled(IoSession session) {
124         return getLogger(session).isErrorEnabled();
125     }
126
127     private static Logger getLogger(IoSession session) {
128         Logger log = (Logger) session.getAttribute(LOGGER);
129         if (log == null) {
130             log = LoggerFactory.getLogger(getClass(session));
131             String JavaDoc prefix = (String JavaDoc) session.getAttribute(PREFIX);
132             if (prefix == null) {
133                 prefix = "[" + session.getRemoteAddress() + "] ";
134                 session.setAttribute(PREFIX, prefix);
135             }
136
137             session.setAttribute(LOGGER, log);
138         }
139
140         return log;
141     }
142 }
143
Popular Tags