/java/server/tags/0_8_0/src/org/red5/logging/Red5LoggerFactory.java

https://github.com/mondain/red5 · Java · 119 lines · 34 code · 15 blank · 70 comment · 6 complexity · 3768e9ec6013b38c802a371fb8a2a356 MD5 · raw file

  1. package org.red5.logging;
  2. /*
  3. * RED5 Open Source Flash Server - http://www.osflash.org/red5
  4. *
  5. * Copyright (c) 2006-2009 by respective authors (see below). All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2.1 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  14. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License along
  17. * with this library; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.slf4j.impl.StaticLoggerBinder;
  23. import ch.qos.logback.classic.LoggerContext;
  24. import ch.qos.logback.classic.selector.ContextSelector;
  25. /**
  26. * LoggerFactory to simplify requests for Logger instances within
  27. * Red5 applications. This class is expected to be run only once per
  28. * logger request and is optimized as such.
  29. *
  30. * @author Paul Gregoire (mondain@gmail.com)
  31. */
  32. public class Red5LoggerFactory {
  33. @SuppressWarnings("unchecked")
  34. public static Logger getLogger(Class clazz) {
  35. //determine the red5 app name or servlet context name
  36. String contextName = null;
  37. /* TODO: For a future day, the context or application will be determined
  38. //get a reference to our caller
  39. Class caller = Reflection.getCallerClass(2);
  40. //System.err.printf("Caller class: %s classloader: %s\n", caller, caller.getClassLoader());
  41. try {
  42. //check to see if we've been called by a servlet
  43. Class sub = caller.asSubclass(Servlet.class);
  44. //System.err.println("Caller is a Servlet");
  45. //Method[] methods = caller.getMethods();
  46. //for (Method meth : methods) {
  47. // System.err.printf("Method: %s\n", meth.getName());
  48. //}
  49. Method getContext = caller.getMethod("getServletContext", new Class[0]);
  50. //System.err.printf("got context method - %s\n", getContext);
  51. ServletContext context = (ServletContext) getContext.invoke(caller, null);
  52. System.err.printf("invoked context\n");
  53. contextName = context.getServletContextName();
  54. //System.err.printf("Servlet context name: %s\n", contextName);
  55. Method getContextName = context.getClass().getMethod("getServletContextName", new Class[0]);
  56. System.err.printf("got context name\n");
  57. Object ctxName = getContextName.invoke(null, new Object[0]);
  58. System.err.printf("Servlet context result: %s\n", ctxName);
  59. if (ctxName != null && ctxName instanceof String) {
  60. contextName = ctxName.toString();
  61. }
  62. } catch (Exception ex) {
  63. //ex.printStackTrace();
  64. }
  65. */
  66. return getLogger(clazz, contextName);
  67. }
  68. @SuppressWarnings("unchecked")
  69. public static Logger getLogger(Class clazz, String contextName) {
  70. Logger logger = null;
  71. try {
  72. //check for logback
  73. Class cs = Class.forName("ch.qos.logback.classic.selector.ContextSelector");
  74. //trigger an exception if the class doesnt actually exist
  75. cs.getDeclaredMethods();
  76. //get the context selector
  77. ContextSelector selector = StaticLoggerBinder.getSingleton().getContextSelector();
  78. //get the context for the given context name or default if null
  79. LoggerContext ctx = null;
  80. if (contextName != null && contextName.length() > 0) {
  81. ctx = selector.getLoggerContext(contextName);
  82. }
  83. // and if we get here, fall back to the default context
  84. if (ctx == null) {
  85. ctx = selector.getLoggerContext();
  86. }
  87. //debug
  88. //StatusPrinter.print(ctx);
  89. //get the logger from the context or default context
  90. logger = ((ctx != null) ? ctx.getLogger(clazz) : selector.getDefaultLoggerContext().getLogger(clazz));
  91. } catch (Exception e) {
  92. //no logback, use whatever logger is in-place
  93. logger = LoggerFactory.getLogger(clazz);
  94. e.printStackTrace();
  95. }
  96. return logger;
  97. }
  98. }