Skip to content

Commit

Permalink
Re-factor ElapsedTimeElement to use a customizable Style
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherSchultz committed Apr 19, 2024
1 parent cbc2b35 commit d3482c3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
52 changes: 40 additions & 12 deletions java/org/apache/catalina/valves/AbstractAccessLogValve.java
Original file line number Diff line number Diff line change
Expand Up @@ -1307,29 +1307,57 @@ public void addElement(CharArrayWriter buf, Date date, Request request, Response
* write time taken to process the request - %D, %T
*/
protected static class ElapsedTimeElement implements AccessLogElement {
private final boolean micros;
private final boolean millis;
enum Style {
SECONDS {
@Override
public void append(CharArrayWriter buf, long time) {
buf.append(Long.toString(TimeUnit.NANOSECONDS.toSeconds(time)));
}
},
MILLISECONDS {
@Override
public void append(CharArrayWriter buf, long time) {
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMillis(time)));
}
},
MICROSECONDS {
@Override
public void append(CharArrayWriter buf, long time) {
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMicros(time)));
}
};

/**
* Append the time to the buffer in the appropriate format.
*
* @param buf The buffer to append to.
* @param time The time to log in nanoseconds.
*/
public abstract void append(CharArrayWriter buf, long time);
}
private final Style style;

/**
* Create a new ElapsedTimeElement that will log the time in the specified style.
*
* @param style The elapsed-time style to use.
*/
public ElapsedTimeElement(Style style) {
this.style = style;
}

/**
* @param micros <code>true</code>, write time in microseconds - %D
* @param millis <code>true</code>, write time in milliseconds, if both arguments are <code>false</code>, write
* time in seconds - %T
*/
public ElapsedTimeElement(boolean micros, boolean millis) {
this.micros = micros;
this.millis = millis;
this(micros ? Style.MICROSECONDS : millis ? Style.MILLISECONDS : Style.SECONDS);
}

@Override
public void addElement(CharArrayWriter buf, Date date, Request request, Response response, long time) {
if (micros) {
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMicros(time)));
} else if (millis) {
buf.append(Long.toString(TimeUnit.NANOSECONDS.toMillis(time)));
} else {
// second
buf.append(Long.toString(TimeUnit.NANOSECONDS.toSeconds(time)));
}
style.append(buf, time);
}
}

Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
dispatch is now performed rather than completing the request using the
error page mechanism. (markt)
</fix>
<add>
Re-factor ElapsedTimeElement in AbstractAccessLogValve to use a customizable
style. (schultz)
</add>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit d3482c3

Please sign in to comment.