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.sshd; 16 : 17 : import static com.google.gerrit.sshd.SshLog.P_ACCOUNT_ID; 18 : import static com.google.gerrit.sshd.SshLog.P_AGENT; 19 : import static com.google.gerrit.sshd.SshLog.P_EXEC; 20 : import static com.google.gerrit.sshd.SshLog.P_MEMORY; 21 : import static com.google.gerrit.sshd.SshLog.P_MESSAGE; 22 : import static com.google.gerrit.sshd.SshLog.P_SESSION; 23 : import static com.google.gerrit.sshd.SshLog.P_STATUS; 24 : import static com.google.gerrit.sshd.SshLog.P_TOTAL_CPU; 25 : import static com.google.gerrit.sshd.SshLog.P_USER_CPU; 26 : import static com.google.gerrit.sshd.SshLog.P_USER_NAME; 27 : import static com.google.gerrit.sshd.SshLog.P_WAIT; 28 : 29 : import com.google.gerrit.util.logging.LogTimestampFormatter; 30 : import org.apache.log4j.Layout; 31 : import org.apache.log4j.spi.LoggingEvent; 32 : import org.eclipse.jgit.util.QuotedString; 33 : 34 : public final class SshLogLayout extends Layout { 35 : private final LogTimestampFormatter timestampFormatter; 36 : 37 5 : public SshLogLayout() { 38 5 : timestampFormatter = new LogTimestampFormatter(); 39 5 : } 40 : 41 : @Override 42 : public String format(LoggingEvent event) { 43 4 : final StringBuilder buf = new StringBuilder(128); 44 : 45 4 : buf.append('['); 46 4 : buf.append(timestampFormatter.format(event.getTimeStamp())); 47 4 : buf.append(']'); 48 : 49 4 : req(P_SESSION, buf, event); 50 : 51 4 : buf.append(' '); 52 4 : buf.append('['); 53 4 : buf.append(event.getThreadName()); 54 4 : buf.append(']'); 55 : 56 4 : req(P_USER_NAME, buf, event); 57 4 : req(P_ACCOUNT_ID, buf, event); 58 : 59 4 : buf.append(' '); 60 4 : buf.append(event.getMessage()); 61 : 62 4 : String msg = (String) event.getMessage(); 63 4 : if (!(msg.startsWith("LOGIN") || msg.equals("LOGOUT"))) { 64 3 : req(P_WAIT, buf, event); 65 3 : req(P_EXEC, buf, event); 66 3 : req(P_MESSAGE, buf, event); 67 3 : req(P_STATUS, buf, event); 68 3 : req(P_AGENT, buf, event); 69 3 : req(P_TOTAL_CPU, buf, event); 70 3 : req(P_USER_CPU, buf, event); 71 3 : req(P_MEMORY, buf, event); 72 : } 73 : 74 4 : buf.append('\n'); 75 4 : return buf.toString(); 76 : } 77 : 78 : private void req(String key, StringBuilder buf, LoggingEvent event) { 79 4 : Object val = event.getMDC(key); 80 4 : buf.append(' '); 81 4 : if (val != null) { 82 4 : String s = val.toString(); 83 4 : if (0 <= s.indexOf(' ')) { 84 2 : buf.append(QuotedString.BOURNE.quote(s)); 85 : } else { 86 4 : buf.append(val); 87 : } 88 4 : } else { 89 3 : buf.append('-'); 90 : } 91 4 : } 92 : 93 : @Override 94 : public boolean ignoresThrowable() { 95 4 : return true; 96 : } 97 : 98 : @Override 99 0 : public void activateOptions() {} 100 : }