1 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 36 public class SessionLog { 37 40 public static final String PREFIX = SessionLog.class.getName() + ".prefix"; 41 42 45 public static final String LOGGER = SessionLog.class.getName() + ".logger"; 46 47 private static Class getClass(IoSession session) { 48 return session.getHandler().getClass(); 49 } 50 51 public static void debug(IoSession session, String 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 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 message, Throwable 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 prefix = (String ) 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 |