Review Board 1.7.22


FLUME-1818. Add layout support to log4jappender.

Review Request #8845 - Created Jan. 6, 2013 and submitted

Hari Shreedharan
FLUME-1818
Reviewers
Flume
flume-git
Added layout support to log4jappender.
Existing tests pass, added new test to test this feature.
flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
Revision 083f5d1 New Change
[20] 27 lines
[+20]
28
import org.apache.flume.api.RpcClient;
28
import org.apache.flume.api.RpcClient;
29
import org.apache.flume.api.RpcClientFactory;
29
import org.apache.flume.api.RpcClientFactory;
30
import org.apache.flume.event.EventBuilder;
30
import org.apache.flume.event.EventBuilder;
31

    
   
31

   
32
import org.apache.log4j.AppenderSkeleton;
32
import org.apache.log4j.AppenderSkeleton;

    
   
33
import org.apache.log4j.Layout;
33
import org.apache.log4j.helpers.LogLog;
34
import org.apache.log4j.helpers.LogLog;
34
import org.apache.log4j.spi.LoggingEvent;
35
import org.apache.log4j.spi.LoggingEvent;
35

    
   
36

   
36
/**
37
/**
37
 *
38
 *
[+20] [20] 81 lines
[+20] [+] public synchronized void append(LoggingEvent event) throws FlumeException{
119
    //Log4jAvroHeaders.LOG_LEVEL.toString()))
120
    //Log4jAvroHeaders.LOG_LEVEL.toString()))
120
    hdrs.put(Log4jAvroHeaders.LOG_LEVEL.toString(),
121
    hdrs.put(Log4jAvroHeaders.LOG_LEVEL.toString(),
121
        String.valueOf(event.getLevel().toInt()));
122
        String.valueOf(event.getLevel().toInt()));
122
    hdrs.put(Log4jAvroHeaders.MESSAGE_ENCODING.toString(), "UTF8");
123
    hdrs.put(Log4jAvroHeaders.MESSAGE_ENCODING.toString(), "UTF8");
123

    
   
124

   
124
    Event flumeEvent = EventBuilder.withBody(event.getMessage().toString(),
125
    String message = null;
125
        Charset.forName("UTF8"), hdrs);
126
    if(this.layout != null) {

    
   
127
      message = this.layout.format(event);

    
   
128
    } else {

    
   
129
      message = event.getMessage().toString();

    
   
130
    }

    
   
131

   

    
   
132
    Event flumeEvent = EventBuilder.withBody(

    
   
133
        message, Charset.forName("UTF8"), hdrs);
126

    
   
134

   
127
    try {
135
    try {
128
      rpcClient.append(flumeEvent);
136
      rpcClient.append(flumeEvent);
129
    } catch (EventDeliveryException e) {
137
    } catch (EventDeliveryException e) {
130
      String msg = "Flume append() failed.";
138
      String msg = "Flume append() failed.";
[+20] [20] 20 lines
[+20] [+] public synchronized void close() throws FlumeException{
151
    }
159
    }
152
  }
160
  }
153

    
   
161

   
154
  @Override
162
  @Override
155
  public boolean requiresLayout() {
163
  public boolean requiresLayout() {
156
    return false;
164
    // This method is named quite incorrectly in the interface. It should

    
   
165
    // probably be called canUseLayout or something. According to the docs,

    
   
166
    // even if the appender can work without a layout, if it can work with one,

    
   
167
    // this method must return true.

    
   
168
    return true;
157
  }
169
  }
158

    
   
170

   
159
  /**
171
  /**
160
   * Set the first flume hop hostname.
172
   * Set the first flume hop hostname.
161
   * @param hostname The first hop where the client should connect to.
173
   * @param hostname The first hop where the client should connect to.
[+20] [20] 7 lines
[+20] [+] public void setHostname(String hostname){
169
   * @param port The port to connect on the host.
181
   * @param port The port to connect on the host.
170
   */
182
   */
171
  public void setPort(int port){
183
  public void setPort(int port){
172
    this.port = port;
184
    this.port = port;
173
  }
185
  }
174

    
   

   
175
  /**
186
  /**
176
   * Activate the options set using <tt>setPort()</tt>
187
   * Activate the options set using <tt>setPort()</tt>
177
   * and <tt>setHostname()</tt>
188
   * and <tt>setHostname()</tt>
178
   * @throws FlumeException if the <tt>hostname</tt> and
189
   * @throws FlumeException if the <tt>hostname</tt> and
179
   *  <tt>port</tt> combination is invalid.
190
   *  <tt>port</tt> combination is invalid.
[+20] [20] 6 lines
[+20] [+] public void activateOptions() throws FlumeException{
186
      String errormsg = "RPC client creation failed! " +
197
      String errormsg = "RPC client creation failed! " +
187
          e.getMessage();
198
          e.getMessage();
188
      LogLog.error(errormsg);
199
      LogLog.error(errormsg);
189
      throw e;
200
      throw e;
190
    }
201
    }

    
   
202
    if(layout != null) {

    
   
203
      layout.activateOptions();

    
   
204
    }
191
  }
205
  }
192

    
   
206

   
193
  /**
207
  /**
194
   * Make it easy to reconnect on failure
208
   * Make it easy to reconnect on failure
195
   * @throws FlumeException
209
   * @throws FlumeException
196
   */
210
   */
197
  private void reconnect() throws FlumeException {
211
  private void reconnect() throws FlumeException {
198
    close();
212
    close();
199
    activateOptions();
213
    activateOptions();
200
  }
214
  }
201
}
215
}
202

    
   
216

   
flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
Revision 68d95fb New Change
 
  1. flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java: Loading...
  2. flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java: Loading...