Coverage Report - com.thindeck.dynamo.Drain
 
Classes in this File Line Coverage Branch Coverage Complexity
Drain
81%
18/22
30%
3/10
1.2
 
 1  
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the thindeck.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.thindeck.dynamo;
 31  
 
 32  
 import java.util.concurrent.ConcurrentHashMap;
 33  
 import java.util.concurrent.ConcurrentMap;
 34  
 import lombok.EqualsAndHashCode;
 35  
 import lombok.ToString;
 36  
 import org.apache.log4j.AppenderSkeleton;
 37  
 import org.apache.log4j.Logger;
 38  
 import org.apache.log4j.PatternLayout;
 39  
 import org.apache.log4j.spi.LoggingEvent;
 40  
 
 41  
 /**
 42  
  * Logs from LOG4J.
 43  
  *
 44  
  * @author Yegor Bugayenko (yegor@teamed.io)
 45  
  * @version $Id$
 46  
  */
 47  0
 @ToString
 48  1
 @EqualsAndHashCode(callSuper = true)
 49  
 final class Drain extends AppenderSkeleton {
 50  
 
 51  
     /**
 52  
      * Instance of it.
 53  
      */
 54  1
     public static final Drain INSTANCE = new Drain();
 55  
 
 56  
     /**
 57  
      * Logs per threads.
 58  
      */
 59  
     private final transient ConcurrentMap<ThreadGroup, StringBuffer> buffers;
 60  
 
 61  
     /**
 62  
      * Ctor.
 63  
      */
 64  
     private Drain() {
 65  1
         super();
 66  1
         this.buffers = new ConcurrentHashMap<>(0);
 67  1
         final Logger root = Logger.getRootLogger();
 68  1
         root.addAppender(this);
 69  1
         this.setLayout(new PatternLayout("%c %m\n"));
 70  1
         com.jcabi.log.Logger.info(
 71  
             Drain.class, "drain configured for %s", root
 72  
         );
 73  1
     }
 74  
 
 75  
     /**
 76  
      * Get full log for thread group and clean it.
 77  
      * @return Log
 78  
      */
 79  
     public String fetch() {
 80  2
         final ThreadGroup group = Thread.currentThread().getThreadGroup();
 81  2
         final StringBuffer buffer = this.buffers.remove(group);
 82  
         final String text;
 83  2
         if (buffer == null) {
 84  0
             text = "";
 85  
         } else {
 86  2
             text = buffer.toString();
 87  
         }
 88  2
         return text;
 89  
     }
 90  
 
 91  
     @Override
 92  
     public void close() {
 93  
         // nothing
 94  0
     }
 95  
 
 96  
     @Override
 97  
     public boolean requiresLayout() {
 98  0
         return true;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public void append(final LoggingEvent event) {
 103  3
         final ThreadGroup group = Thread.currentThread().getThreadGroup();
 104  3
         this.buffers.putIfAbsent(group, new StringBuffer(0));
 105  3
         this.buffers.get(group).append(this.layout.format(event));
 106  3
     }
 107  
 
 108  
 }