Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.http.jetty; 16 : 17 : import com.google.gerrit.util.logging.LogTimestampFormatter; 18 : import org.apache.log4j.Layout; 19 : import org.apache.log4j.spi.LoggingEvent; 20 : 21 : public final class HttpLogLayout extends Layout { 22 : private final LogTimestampFormatter timestampFormatter; 23 : 24 15 : public HttpLogLayout() { 25 15 : timestampFormatter = new LogTimestampFormatter(); 26 15 : } 27 : 28 : @Override 29 : public String format(LoggingEvent event) { 30 4 : final StringBuilder buf = new StringBuilder(128); 31 : 32 4 : opt(buf, event, HttpLog.P_HOST); 33 : 34 4 : buf.append(' '); 35 4 : buf.append('['); 36 4 : buf.append(event.getThreadName()); 37 4 : buf.append(']'); 38 : 39 4 : buf.append(' '); 40 4 : buf.append('-'); // identd on client system (never requested) 41 : 42 4 : buf.append(' '); 43 4 : opt(buf, event, HttpLog.P_USER); 44 : 45 4 : buf.append(' '); 46 4 : buf.append('['); 47 4 : buf.append(timestampFormatter.format(event.getTimeStamp())); 48 4 : buf.append(']'); 49 : 50 4 : buf.append(' '); 51 4 : buf.append('"'); 52 4 : buf.append(event.getMDC(HttpLog.P_METHOD)); 53 4 : buf.append(' '); 54 4 : buf.append(event.getMDC(HttpLog.P_RESOURCE)); 55 4 : buf.append(' '); 56 4 : buf.append(event.getMDC(HttpLog.P_PROTOCOL)); 57 4 : buf.append('"'); 58 : 59 4 : buf.append(' '); 60 4 : buf.append(event.getMDC(HttpLog.P_STATUS)); 61 : 62 4 : buf.append(' '); 63 4 : opt(buf, event, HttpLog.P_CONTENT_LENGTH); 64 : 65 4 : buf.append(' '); 66 4 : opt(buf, event, HttpLog.P_LATENCY); 67 : 68 4 : buf.append(' '); 69 4 : dq_opt(buf, event, HttpLog.P_REFERER); 70 : 71 4 : buf.append(' '); 72 4 : dq_opt(buf, event, HttpLog.P_USER_AGENT); 73 : 74 4 : buf.append(' '); 75 4 : opt(buf, event, HttpLog.P_CPU_TOTAL); 76 : 77 4 : buf.append(' '); 78 4 : opt(buf, event, HttpLog.P_CPU_USER); 79 : 80 4 : buf.append(' '); 81 4 : opt(buf, event, HttpLog.P_MEMORY); 82 : 83 4 : buf.append(' '); 84 4 : dq_opt(buf, event, HttpLog.P_COMMAND_STATUS); 85 : 86 4 : buf.append('\n'); 87 4 : return buf.toString(); 88 : } 89 : 90 : private void opt(StringBuilder buf, LoggingEvent event, String key) { 91 4 : String val = (String) event.getMDC(key); 92 4 : if (val == null) { 93 4 : buf.append('-'); 94 : } else { 95 4 : buf.append(val); 96 : } 97 4 : } 98 : 99 : private void dq_opt(StringBuilder buf, LoggingEvent event, String key) { 100 4 : String val = (String) event.getMDC(key); 101 4 : if (val == null) { 102 4 : buf.append('-'); 103 : } else { 104 4 : buf.append('"'); 105 4 : buf.append(val); 106 4 : buf.append('"'); 107 : } 108 4 : } 109 : 110 : @Override 111 : public boolean ignoresThrowable() { 112 4 : return true; 113 : } 114 : 115 : @Override 116 0 : public void activateOptions() {} 117 : }