1 | /* |
2 | * @(#) $Id: SessionLog.java 264677 2005-08-30 02:44:35Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.util; |
20 | |
21 | import java.util.logging.Level; |
22 | import java.util.logging.Logger; |
23 | |
24 | import org.apache.mina.common.Session; |
25 | import org.apache.mina.io.IoSession; |
26 | import org.apache.mina.protocol.ProtocolSession; |
27 | |
28 | /** |
29 | * Call {@link #getLogger(Session)}, {@link #log(Level,Session, String)}, and |
30 | * {@link #log(Level,Session, String, Throwable)} to log protocol-specific messages. |
31 | * <p> |
32 | * Set {@link #PREFIX} and {@link #LOGGER} session attributes |
33 | * to override prefix string, logger, and log level. |
34 | * |
35 | * @author The Apache Directory Project (dev@directory.apache.org) |
36 | * @author Trustin Lee (trustin@apache.org) |
37 | * @version $Rev: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $ |
38 | * |
39 | */ |
40 | public class SessionLog { |
41 | /** |
42 | * Session attribute key: prefix string |
43 | */ |
44 | public static final String PREFIX = SessionLog.class.getName() + ".prefix"; |
45 | |
46 | /** |
47 | * Session attribute key: {@link Logger} |
48 | */ |
49 | public static final String LOGGER = SessionLog.class.getName() + ".logger"; |
50 | |
51 | public static Logger getLogger( Session session ) |
52 | { |
53 | |
54 | Logger log = (Logger) session.getAttribute( LOGGER ); |
55 | if( log == null ) |
56 | { |
57 | log = Logger.getLogger( getClassName( session ) ); |
58 | String prefix = ( String ) session.getAttribute( PREFIX ); |
59 | if( prefix == null ) |
60 | { |
61 | prefix = "[" + session.getRemoteAddress() + "] "; |
62 | session.setAttribute( PREFIX, prefix ); |
63 | } |
64 | |
65 | session.setAttribute( LOGGER, log ); |
66 | } |
67 | |
68 | return log; |
69 | } |
70 | |
71 | private static String getClassName( Session session ) |
72 | { |
73 | if( session instanceof IoSession ) |
74 | return ( ( IoSession ) session ).getHandler().getClass().getName(); |
75 | else |
76 | return ( ( ProtocolSession ) session ).getHandler().getClass().getName(); |
77 | } |
78 | |
79 | public static void log( Level level, Session session, String message ) |
80 | { |
81 | Logger log = getLogger( session ); |
82 | if( log.isLoggable( level ) ) |
83 | { |
84 | log.log( level, String.valueOf( session.getAttribute( PREFIX ) ) + message ); |
85 | } |
86 | } |
87 | |
88 | public static void log( Level level, Session session, String message, Throwable cause ) |
89 | { |
90 | Logger log = getLogger( session ); |
91 | if( log.isLoggable( level ) ) |
92 | { |
93 | log.log( level, String.valueOf( session.getAttribute( PREFIX ) ) + message, cause ); |
94 | } |
95 | } |
96 | } |