Log4j2Appender.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.dianping.cat.log4j;
  2. import java.io.PrintWriter;
  3. import java.io.Serializable;
  4. import java.io.StringWriter;
  5. import org.apache.logging.log4j.Level;
  6. import org.apache.logging.log4j.core.Filter;
  7. import org.apache.logging.log4j.core.Layout;
  8. import org.apache.logging.log4j.core.LogEvent;
  9. import org.apache.logging.log4j.core.appender.AbstractAppender;
  10. import org.apache.logging.log4j.core.appender.AppenderLoggingException;
  11. import org.apache.logging.log4j.core.config.plugins.Plugin;
  12. import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
  13. import org.apache.logging.log4j.core.config.plugins.PluginElement;
  14. import org.apache.logging.log4j.core.config.plugins.PluginFactory;
  15. import org.apache.logging.log4j.core.impl.ThrowableProxy;
  16. import org.apache.logging.log4j.core.layout.PatternLayout;
  17. import com.dianping.cat.Cat;
  18. @Plugin(name = "CatAppender", category = "Core", elementType = "appender", printObject = true)
  19. public class Log4j2Appender extends AbstractAppender {
  20. protected Log4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
  21. final boolean ignoreExceptions) {
  22. super(name, filter, layout, ignoreExceptions);
  23. }
  24. @Override
  25. public void append(LogEvent event) {
  26. try {
  27. boolean isTraceMode = Cat.getManager().isTraceMode();
  28. Level level = event.getLevel();
  29. if (level.isMoreSpecificThan(Level.ERROR)) {
  30. logError(event);
  31. } else if (isTraceMode) {
  32. logTrace(event);
  33. }
  34. } catch (Exception ex) {
  35. if (!ignoreExceptions()) {
  36. throw new AppenderLoggingException(ex);
  37. }
  38. }
  39. }
  40. @PluginFactory
  41. public static Log4j2Appender createAppender(@PluginAttribute("name") String name,
  42. @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter,
  43. @PluginAttribute("otherAttribute") String otherAttribute) {
  44. if (name == null) {
  45. LOGGER.error("No name provided for Log4j2Appender");
  46. return null;
  47. }
  48. if (layout == null) {
  49. layout = PatternLayout.createDefaultLayout();
  50. }
  51. return new Log4j2Appender(name, filter, layout, true);
  52. }
  53. private void logError(LogEvent event) {
  54. ThrowableProxy info = event.getThrownProxy();
  55. if (info != null) {
  56. Throwable exception = info.getThrowable();
  57. Object message = event.getMessage();
  58. if (message != null) {
  59. Cat.logError(((Message) message).getFormattedMessage(), exception);
  60. } else {
  61. Cat.logError(exception);
  62. }
  63. }
  64. }
  65. private void logTrace(LogEvent event) {
  66. String type = "Log4j2";
  67. String name = event.getLevel().toString();
  68. Object message = event.getMessage();
  69. String data;
  70. if (message instanceof Throwable) {
  71. data = buildExceptionStack((Throwable) message);
  72. } else {
  73. data = event.getMessage().toString();
  74. }
  75. ThrowableProxy info = event.getThrownProxy();
  76. if (info != null) {
  77. data = data + '\n' + buildExceptionStack(info.getThrowable());
  78. }
  79. Cat.logTrace(type, name, "0", data);
  80. }
  81. private String buildExceptionStack(Throwable exception) {
  82. if (exception != null) {
  83. StringWriter writer = new StringWriter(2048);
  84. exception.printStackTrace(new PrintWriter(writer));
  85. return writer.toString();
  86. } else {
  87. return "";
  88. }
  89. }
  90. }