View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program 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
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.logging;
    21
    22  import java.io.StringWriter;
    23  import java.lang.reflect.Field;
    24
    25  import org.apache.log4j.Layout;
    26  import org.apache.log4j.Level;
    27  import org.apache.log4j.SimpleLayout;
    28  import org.apache.log4j.WriterAppender;
    29
    30  public class InMemoryAppender {
    31      private StringWriter stringWriter = new StringWriter();
    32      private WriterAppender appender = new WriterAppender(new SimpleLayout(), stringWriter);
    33      private final org.apache.log4j.Logger log4jLogger;
    34      private final Level level;
    35      private Layout layout;
    36
    37      public InMemoryAppender(Logger logger) {
    38          this(logger, new SimpleLayout());
    39      }
    40
    41      public InMemoryAppender(Logger logger, Layout layout) {
    42          this(logger, Level.ALL, layout);
    43      }
    44
    45      private InMemoryAppender(Logger logger, Level level, Layout layout) {
    46          this.level = level;
    47          this.layout = layout;
    48          log4jLogger = org.apache.log4j.Logger.getLogger(this.getClass());
    49          changeLogger(logger, log4jLogger);
    50          reset();
    51
    52      }
    53
    54      private void changeLogger(Logger logger, org.apache.log4j.Logger log4jLogger) {
    55          Field loggerField = findLoggerField(logger);
    56          try {
    57              loggerField.setAccessible(true);
    58              loggerField.set(logger, log4jLogger);
    59          } catch (Exception e) {
    60              throw new RuntimeException(e);
    61          }
    62      }
    63
    64      private Field findLoggerField(Logger logger) {
    65          try {
    66              return logger.getClass().getDeclaredField("logger");
    67          } catch (Exception e) {
    68              throw new RuntimeException("The field [logger] must be present for testing", e);
    69          }
    70      }
    71
    72      public void removeAppender() {
    73          org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(this.getClass());
    74          log4jLogger.removeAppender(appender);
    75      }
    76
    77      public String toString() {
    78          return stringWriter.toString();
    79      }
    80
    81      public void reset() {
    82          stringWriter = new StringWriter();
    83          appender = new WriterAppender(layout, stringWriter);
    84          log4jLogger.addAppender(appender);
    85          log4jLogger.setLevel(level);
    86      }
    87  }