-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLoggingHelpers.java
More file actions
77 lines (70 loc) · 2.44 KB
/
LoggingHelpers.java
File metadata and controls
77 lines (70 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Internal helper for timing-based diagnostic logging.
*/
final class LoggingHelpers {
private LoggingHelpers() {
// Utility class
}
/**
* Formats elapsed time as a human-readable duration string.
*
* @param startNanos
* the start time from {@link System#nanoTime()}
* @return formatted duration (e.g. "PT0.123S")
*/
static String formatElapsed(long startNanos) {
long elapsedNanos = System.nanoTime() - startNanos;
long millis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
return String.format("PT%d.%03dS", millis / 1000, millis % 1000);
}
/**
* Logs a timing message at the given level if the logger accepts it.
*
* @param logger
* the logger to use
* @param level
* the log level
* @param message
* the message template
* @param startNanos
* the start time from {@link System#nanoTime()}
*/
static void logTiming(Logger logger, Level level, String message, long startNanos) {
if (!logger.isLoggable(level)) {
return;
}
logger.log(level, message.replace("{Elapsed}", formatElapsed(startNanos)));
}
/**
* Logs a timing message at the given level with an exception.
*
* @param logger
* the logger to use
* @param level
* the log level
* @param exception
* the exception, may be {@code null}
* @param message
* the message template
* @param startNanos
* the start time from {@link System#nanoTime()}
*/
static void logTiming(Logger logger, Level level, Throwable exception, String message, long startNanos) {
if (!logger.isLoggable(level)) {
return;
}
String formatted = message.replace("{Elapsed}", formatElapsed(startNanos));
if (exception != null) {
logger.log(level, formatted, exception);
} else {
logger.log(level, formatted);
}
}
}