Enable sshd_log and httpd_log to use log4j config

At the moment log4j configuration is a bit inconsistent: it is
possible to configure error_log and gc_log using file provided
with log4j.configuration system property, however httpd_log and
sshd_log configuration is hard-coded in corresponding classes
(HttpLog and SshLog).

Make logs configuration more consistent by using log4j for all
logs. If log4j.configuration variable is not set, log behavior
remains intact and code-level log configuration will be used.

In case of log4.configuration points to log4j.properties file the
hard-coded configuration will be ommited and log4j.properties will
be used instead.

In addition shouldConfigureLogSystem() call moved from ErrorLogFile
to LogUtil in com.google.gerrit.util package and removed deprecated
reference to log4j LogManager.DEFAULT_CONFIGURATION_KEY so that
gerrit common does not require log4j.

Two inner MyLayout classes (one from SshLog and one from HttpLog)
have been extracted to separate files and renamed accordingly to
enable to use them from log4j.properties file if required. At the
moment those are extracted as is - refactoring will follow.

Change-Id: Icb1a3787b98392a9df9bb2afa4dc857f8cfada3a
Signed-off-by: Eryk Szymanski <eryksz@gmail.com>
This commit is contained in:
Eryk Szymanski
2013-11-06 17:56:49 +01:00
committed by Shawn Pearce
parent 23e1a2e824
commit 66252ed8ea
10 changed files with 356 additions and 269 deletions

View File

@@ -0,0 +1,153 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.sshd;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.eclipse.jgit.util.QuotedString;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public final class SshLogLayout extends Layout {
private static final String P_SESSION = "session";
private static final String P_USER_NAME = "userName";
private static final String P_ACCOUNT_ID = "accountId";
private static final String P_WAIT = "queueWaitTime";
private static final String P_EXEC = "executionTime";
private static final String P_STATUS = "status";
private final Calendar calendar;
private long lastTimeMillis;
private final char[] lastTimeString = new char[20];
private final char[] timeZone;
public SshLogLayout() {
final TimeZone tz = TimeZone.getDefault();
calendar = Calendar.getInstance(tz);
final SimpleDateFormat sdf = new SimpleDateFormat("Z");
sdf.setTimeZone(tz);
timeZone = sdf.format(new Date()).toCharArray();
}
@Override
public String format(LoggingEvent event) {
final StringBuffer buf = new StringBuffer(128);
buf.append('[');
formatDate(event.getTimeStamp(), buf);
buf.append(' ');
buf.append(timeZone);
buf.append(']');
req(P_SESSION, buf, event);
req(P_USER_NAME, buf, event);
req(P_ACCOUNT_ID, buf, event);
buf.append(' ');
buf.append(event.getMessage());
opt(P_WAIT, buf, event);
opt(P_EXEC, buf, event);
opt(P_STATUS, buf, event);
buf.append('\n');
return buf.toString();
}
private void formatDate(final long now, final StringBuffer sbuf) {
final int millis = (int) (now % 1000);
final long rounded = now - millis;
if (rounded != lastTimeMillis) {
synchronized (calendar) {
final int start = sbuf.length();
calendar.setTimeInMillis(rounded);
sbuf.append(calendar.get(Calendar.YEAR));
sbuf.append('-');
final int month = calendar.get(Calendar.MONTH) + 1;
if (month < 10) sbuf.append('0');
sbuf.append(month);
sbuf.append('-');
final int day = calendar.get(Calendar.DAY_OF_MONTH);
if (day < 10) sbuf.append('0');
sbuf.append(day);
sbuf.append(' ');
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour < 10) sbuf.append('0');
sbuf.append(hour);
sbuf.append(':');
final int mins = calendar.get(Calendar.MINUTE);
if (mins < 10) sbuf.append('0');
sbuf.append(mins);
sbuf.append(':');
final int secs = calendar.get(Calendar.SECOND);
if (secs < 10) sbuf.append('0');
sbuf.append(secs);
sbuf.append(',');
sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
lastTimeMillis = rounded;
}
} else {
sbuf.append(lastTimeString);
}
if (millis < 100) {
sbuf.append('0');
}
if (millis < 10) {
sbuf.append('0');
}
sbuf.append(millis);
}
private void req(String key, StringBuffer buf, LoggingEvent event) {
Object val = event.getMDC(key);
buf.append(' ');
if (val != null) {
String s = val.toString();
if (0 <= s.indexOf(' ')) {
buf.append(QuotedString.BOURNE.quote(s));
} else {
buf.append(val);
}
} else {
buf.append('-');
}
}
private void opt(String key, StringBuffer buf, LoggingEvent event) {
Object val = event.getMDC(key);
if (val != null) {
buf.append(' ');
buf.append(val);
}
}
@Override
public boolean ignoresThrowable() {
return true;
}
@Override
public void activateOptions() {
}
}