public class LoggingStopwatch extends CloseableStopwatch
CloseableStopwatch
meant for logging stopwatch
data.
See the static factory methods for simplified object creation.
Usage example: The following shows the LoggingStopwatch used inside a try-with-resource statement. The factory methods description describes further details.
// Create new stopwatch using simplifying factory method and log try (LoggingStopwatch stopwatch = LoggingStopwatch.early("Process steps", LOGGER::info)) { // Do step 1 ... // Create new checkpoint and log stopwatch.checkpoint("Step 1 done"); // Do step 2 ... // Stop stopwatch using try-with-resource (close method) and log }
Stopwatch.Checkpoint
Constructor and Description |
---|
LoggingStopwatch(String name,
Consumer<? super LoggingStopwatch> logger)
Implementation of
CloseableStopwatch meant for logging stopwatch
data. |
Modifier and Type | Method and Description |
---|---|
Stopwatch.Checkpoint |
checkpoint(String name)
Adds a new checkpoint referenced by
name . |
void |
close() |
static LoggingStopwatch |
early(String name,
Consumer<Supplier<String>> logger)
Creates a
LoggingStopwatch that logs on every logging event. |
static LoggingStopwatch |
earlyWithString(String name,
Consumer<String> logger)
Creates a
LoggingStopwatch that logs on every logging event. |
Consumer<? super LoggingStopwatch> |
getLogger()
Logging operation that consumes a
LoggingStopwatch
This operation is called after starting and stopping the stopwatch and after
each checkpoint creation. |
String |
getName()
Stopwatches name
|
static LoggingStopwatch |
late(String name,
Consumer<Supplier<String>> logger)
Creates a
LoggingStopwatch that does not log on every logging
event. |
static LoggingStopwatch |
lateWithString(String name,
Consumer<String> logger)
Creates a
LoggingStopwatch that does not log on every logging
event. |
String |
toString() |
getStopInstant, isStopped, sinceLast, sinceStart
getCheckpoints, getLastCheckpoint, getLastInstant, getStartInstant, stream, waitFor, waitFor
public LoggingStopwatch(String name, Consumer<? super LoggingStopwatch> logger)
CloseableStopwatch
meant for logging stopwatch
data.
Logging operation that consumes a LoggingStopwatch
logger
is
a logging operation that consumes a LoggingStopwatch
. It is called
after starting and stopping the stopwatch and after each checkpoint creation.
See the static factory methods for simplified object creation.
name
- the stopwatches namelogger
- the logging operationpublic static LoggingStopwatch earlyWithString(String name, Consumer<String> logger)
LoggingStopwatch
that logs on every logging event.
This logs an already constructed message. Use
early(String, Consumer)
for common logging frameworks instead.
name
- the stopwatches namelogger
- the string logging operationLoggingStopwatch
that logs on every logging eventpublic static LoggingStopwatch lateWithString(String name, Consumer<String> logger)
LoggingStopwatch
that does not log on every logging
event. Instead stopwatch data is logged after stopping the stopwatch to
reduce the impact of logging from measured times.
This logs an already constructed message. Use late(String, Consumer)
for common logging frameworks instead.
name
- the stopwatches namelogger
- the string logging operationLoggingStopwatch
that logs on every logging eventpublic static LoggingStopwatch early(String name, Consumer<Supplier<String>> logger)
LoggingStopwatch
that logs on every logging event.
Instead of earlyWithString(String, Consumer)
this logs a still to be
constructed message.
name
- the stopwatches namelogger
- the string supplier logging operationLoggingStopwatch
that logs on every logging eventpublic static LoggingStopwatch late(String name, Consumer<Supplier<String>> logger)
LoggingStopwatch
that does not log on every logging
event. Instead stopwatch data is logged after stopping the stopwatch to
reduce the impact of logging from measured times.
Instead of lateWithString(String, Consumer)
this logs a still to be
constructed message.
name
- the stopwatches namelogger
- the string supplier logging operationLoggingStopwatch
that logs on every logging eventpublic Stopwatch.Checkpoint checkpoint(String name)
name
.
name
does not need to be unique. Multiple checkpoints with the same
name might exist.
checkpoint
in class CloseableStopwatch
name
- name to reference the checkpointpublic void close()
close
in interface AutoCloseable
close
in class CloseableStopwatch
public Consumer<? super LoggingStopwatch> getLogger()
LoggingStopwatch
This operation is called after starting and stopping the stopwatch and after each checkpoint creation.
public String getName()
@NonNull public String toString()
toString
in class CloseableStopwatch
Copyright © 2025. All rights reserved.