/servers/jain-slee/core/spi/src/main/java/org/mobicents/slee/container/MobicentsLogFilter.java

http://mobicents.googlecode.com/ · Java · 170 lines · 105 code · 26 blank · 39 comment · 29 complexity · ab195e9db73e6ebc51ba80e7945c1e14 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.slee.container;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.logging.Filter;
  26. import java.util.logging.Formatter;
  27. import java.util.logging.Level;
  28. import java.util.logging.LogRecord;
  29. import java.util.logging.SimpleFormatter;
  30. import org.apache.log4j.Logger;
  31. /**
  32. * This filter is used to convert java.util.logging messages from the JUL
  33. * to Log4J messages.
  34. *
  35. * @author Stan Silvert
  36. * @author Pete Muir
  37. * @author Alexandre Mendonca
  38. */
  39. public class MobicentsLogFilter implements Filter
  40. {
  41. // cache Logger instances. Logger.getLogger() is known to be slow.
  42. // See http://www.qos.ch/logging/thinkAgain.jsp
  43. private Map<String, Logger> loggerCache = new HashMap<String, Logger>();
  44. private Formatter formatter = new SimpleFormatter();
  45. /**
  46. * If the message should be logged, convert the JDK 1.4
  47. * LogRecord to a Log4J message.
  48. *
  49. * @return <code>false</code> because JDK 1.4 logging should not happen
  50. * if this filter is active.
  51. */
  52. public boolean isLoggable(LogRecord record)
  53. {
  54. Logger logger = getLogger(record);
  55. if (record.getThrown() != null)
  56. {
  57. logWithThrowable(logger, record);
  58. }
  59. else
  60. {
  61. logWithoutThrowable(logger, record);
  62. }
  63. return false;
  64. }
  65. private void logWithThrowable(Logger logger, LogRecord record)
  66. {
  67. int loggedLevel = record.getLevel().intValue();
  68. Object message = formatter.formatMessage(record);
  69. Throwable throwable = record.getThrown();
  70. if (loggedLevel == Level.SEVERE.intValue())
  71. {
  72. logger.error(message, throwable);
  73. return;
  74. }
  75. if (loggedLevel == Level.WARNING.intValue())
  76. {
  77. logger.warn(message, throwable);
  78. return;
  79. }
  80. if ((loggedLevel == Level.INFO.intValue()) ||
  81. (loggedLevel == Level.CONFIG.intValue()))
  82. {
  83. logger.info(message, throwable);
  84. return;
  85. }
  86. if (loggedLevel == Level.FINE.intValue())
  87. {
  88. logger.debug(message, throwable);
  89. return;
  90. }
  91. if ((loggedLevel == Level.FINER.intValue()) ||
  92. (loggedLevel == Level.FINEST.intValue()))
  93. {
  94. logger.trace(message, throwable);
  95. return;
  96. }
  97. logger.info(message, throwable);
  98. }
  99. private void logWithoutThrowable(Logger logger, LogRecord record)
  100. {
  101. int loggedLevel = record.getLevel().intValue();
  102. Object message = formatter.formatMessage(record);
  103. if (loggedLevel == Level.SEVERE.intValue())
  104. {
  105. logger.error(message);
  106. return;
  107. }
  108. if (loggedLevel == Level.WARNING.intValue())
  109. {
  110. logger.warn(message);
  111. return;
  112. }
  113. if ((loggedLevel == Level.INFO.intValue()) ||
  114. (loggedLevel == Level.CONFIG.intValue()))
  115. {
  116. logger.info(message);
  117. return;
  118. }
  119. if (loggedLevel == Level.FINE.intValue())
  120. {
  121. logger.debug(message);
  122. return;
  123. }
  124. if ((loggedLevel == Level.FINER.intValue()) ||
  125. (loggedLevel == Level.FINEST.intValue()))
  126. {
  127. logger.trace(message);
  128. return;
  129. }
  130. logger.info(message);
  131. }
  132. // get the Log4J logger corresponding to the java.util.logger.LogRecord
  133. private Logger getLogger(LogRecord record)
  134. {
  135. String loggerName = record.getLoggerName();
  136. Logger logger = loggerCache.get(loggerName);
  137. if (logger == null)
  138. {
  139. logger = Logger.getLogger(loggerName);
  140. loggerCache.put(loggerName, logger);
  141. }
  142. return logger;
  143. }
  144. }