Sie können, tomcat Quellcode durchlaufen, und Sie werden den Schlüssel finden existiert in AccessLogValve.java:
Für Tomcat 8:
public void log(CharArrayWriter message) {
this.rotate();
if (this.checkExists) {
synchronized(this) {
if (this.currentLogFile != null && !this.currentLogFile.exists()) {
try {
this.close(false);
} catch (Throwable var8) {
ExceptionUtils.handleThrowable(var8);
log.info(sm.getString("accessLogValve.closeFail"), var8);
}
this.dateStamp = this.fileDateFormatter.format(new Date(System.currentTimeMillis()));
this.open();
}
}
}
try {
synchronized(this) {
if (this.writer != null) {
message.writeTo(this.writer);
this.writer.println("");
if (!this.buffered) {
this.writer.flush();
}
}
}
} catch (IOException var7) {
log.warn(sm.getString("accessLogValve.writeFail", new Object[]{message.toString()}), var7);
}
}
Sie sollten an, dass ein Protokoll nehmen Sie dann wird wissen, wie man konfiguriert.
Dann lassen Sie uns beginnen, sollten Sie eine Klasse erstellen, erstreckt ValveBase implementiert AccessLog, wie:
public class LeKafkaAccesslogValve extends ValveBase implements AccessLog {
private String topic;
private String bootstrapServers;
// If set to zero then the producer will not wait for any acknowledgment from the server at all.
private String acks;
private String producerSize ;
private String properties;
private List<Producer<byte[], byte[]>> producerList;
private AtomicInteger producerIndex = new AtomicInteger(0);
private int timeoutMillis;
private boolean enabled = true;
private String pattern;
private AccessLogElement accessLogElement;
private String localeName;
private Locale locale = Locale.getDefault();
@Override
public void log(Request request, Response response, long l) {
if (producerList != null && getEnabled() && getState().isAvailable() && null != this.accessLogElement) {
try {
getNextProducer().send(new ProducerRecord<byte[], byte[]>(topic, this.accessLogElement.buildLog(request,response,time,this).getBytes(StandardCharsets.UTF_8))).get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.error('accesslog in kafka exception', e);
}
}
}
@Override
public void setRequestAttributesEnabled(boolean b) {
//some other code if you would like
}
@Override
public boolean getRequestAttributesEnabled() {
//some other code if you would like
return false;
}
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
//some other code if you would like
}
}
Dann sollten Sie Ihre eigene Konfiguration zu server.xml hinzufügen, wie:
<Valve className='com.xxx.lekafkavalve.LeKafkaAccesslogValve' enabled='true' topic='info' pattern='%{yyyy-MM-dd HH:mm:ss}t||info||AccessValve||Tomcat||%A||%a||%r||%s||%D' bootstrapServers='kafkaaddress' producerSize='5' properties='acks=0||producer.size=3'/>
Danke Jang, lass mich das versuchen. –