Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -39,14 +40,13 @@ public class LoggingFormatter extends Formatter {
private static final String TIME_IN_MILLIS = "timeInMillis";
private static final String MESSAGE = "message";
private static final String EXCEPTION = "exception";
private static final String DATE_FORMAT = "MM-dd-yyyy'T'HH:mm:ss.SSSZZ";

// For ApiException
private static final String RESPONSE_CODE = "code";
private static final String RESPONSE_HEADERS = "headers";
private static final String RESPONSE_BODY = "body";

private final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

@Override
public String format(LogRecord record) {
Expand Down Expand Up @@ -107,7 +107,7 @@ public String format(LogRecord record) {
String level = record.getLevel().getLocalizedName();
Map<String, Object> map = new LinkedHashMap<>();
long rawTime = record.getMillis();
final String dateString = dateFormat.format(new Date(rawTime));
final String dateString = DATE_FORMAT.format(OffsetDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault()));
long thread = Thread.currentThread().getId();
Fiber fiber = Fiber.getCurrentIfSet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package oracle.kubernetes.operator.rest;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jakarta.ws.rs.container.ContainerRequestContext;
Expand All @@ -24,12 +24,10 @@ public abstract class BaseDebugLoggingFilter {
protected static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
protected static final String FILTER_REQUEST_START_TIME = "FILTER_REQUEST_START_TIME";
protected static final String FILTER_REQUEST_ENTITY = "FILTER_REQUEST_ENTITY";
private static final String DATE_FORMAT =
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; // ISO 8610, includes time zone
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

protected static String formatTime(long time) {
// construct a new SimpleDataFormat each time since it is not thread safe:
return new SimpleDateFormat(DATE_FORMAT).format(new Date(time));
protected static String formatTime(TemporalAccessor time) {
return DATE_FORMAT.format(time);
}

protected String formatEntity(MediaType type, String entityAsString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.time.OffsetDateTime;
import javax.annotation.Priority;

import jakarta.ws.rs.container.ContainerRequestContext;
Expand Down Expand Up @@ -38,7 +39,7 @@ public void filter(ContainerRequestContext req) {
try {
// cache the start time and request body so that the response filter
// can log them too
long start = System.currentTimeMillis();
OffsetDateTime start = OffsetDateTime.now();
req.setProperty(FILTER_REQUEST_START_TIME, start);
Object reqEntity = getRequestEntity(req);
if (reqEntity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package oracle.kubernetes.operator.rest;

import java.io.InputStream;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import javax.annotation.Priority;

import jakarta.ws.rs.container.ContainerRequestContext;
Expand Down Expand Up @@ -38,9 +40,9 @@ public void filter(ContainerRequestContext req, ContainerResponseContext res) {
LOGGER.fine("method=" + req.getMethod());
Object prop = req.getProperty(FILTER_REQUEST_START_TIME);
if (prop != null) {
long start = (Long) prop;
long end = System.currentTimeMillis();
long duration = end - start;
OffsetDateTime start = (OffsetDateTime) prop;
OffsetDateTime end = OffsetDateTime.now();
long duration = start.until(end, ChronoUnit.MILLIS);
LOGGER.fine("start=" + formatTime(start));
LOGGER.fine("duration=" + duration + " ms");
}
Expand Down
8 changes: 4 additions & 4 deletions operator/src/main/resources/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# 'ERROR' is converted to 'SEVERE'
# Unknown logLevels are converted to 'FINE'.
#
# This matches format of bash utils.sh trace, and rougly matches the operator's log format.
# This matches format of bash utils.sh trace and the operator's log format.
#
# Sample output: @[2018-09-28T17:23:55.335 UTC][introspectDomain.py:614][FINE] Domain introspection complete.
# Sample output: @[2018-09-28T17:23:55.335000Z][introspectDomain.py:614][FINE] Domain introspection complete.
#
# Importing this file when it's not in sys.path of the calling script:
#
Expand Down Expand Up @@ -52,8 +52,8 @@ def traceInner(logLevel,object):
}
# use FINE as logLevel if logLevel is not a known type
logLevel=switcher.get(logLevel.upper(),'FINE')
print("@[%d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d UTC][%s:%s][%s] %s"
% (dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond/1000,
print("@[%d-%.2d-%.2dT%.2d:%.2d:%.2d.%.6dZ][%s:%s][%s] %s"
% (dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond,
filename,lineno,logLevel,object))

def trace(arg1,arg2='SENTINEL'):
Expand Down
26 changes: 10 additions & 16 deletions operator/src/main/resources/scripts/utils_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
#
# examples:
# trace "Situation normal."
# @[2018-09-28T18:10:52.417 UTC][myscript.sh:91][FINE] Situation normal.
# @[2018-09-28T18:10:52.417000Z][myscript.sh:91][FINE] Situation normal.
#
# trace INFO "Situation normal."
# @[2018-09-28T18:10:52.417 UTC][myscript.sh:91][INFO] Situation normal.
# @[2018-09-28T18:10:52.417000Z][myscript.sh:91][INFO] Situation normal.
#
# trace "Info: Situation normal."
# @[2018-09-28T18:10:52.417 UTC][myscript.sh:91][INFO] Info: Situation normal.
# @[2018-09-28T18:10:52.417000Z][myscript.sh:91][INFO] Info: Situation normal.
#
# ls 2>&1 | tracePipe FINE "ls output: "
# @[2018-09-28T18:10:52.417 UTC][myscript.sh:91][FINE] ls output: file1
# @[2018-09-28T18:10:52.417 UTC][myscript.sh:91][FINE] ls output: file2
# @[2018-09-28T18:10:52.417000Z][myscript.sh:91][FINE] ls output: file1
# @[2018-09-28T18:10:52.417000Z][myscript.sh:91][FINE] ls output: file2
#
# Set TRACE_INCLUDE_FILE env var to false to suppress file name and line number.
#
Expand Down Expand Up @@ -167,21 +167,15 @@ function traceDirs() {
}

# timestamp
# purpose: echo timestamp in the form yyyymmddThh:mm:ss.mmm ZZZ
# example: 20181001T14:00:00.001 UTC
# purpose: echo timestamp in the form yyyy-mm-ddThh:mm:ss.nnnnnnZ
# example: 2018-10-01T14:00:00.001Z
function timestamp() {
local timestamp="`date --utc '+%Y-%m-%dT%H:%M:%S %N %s %Z' 2>&1`"
local timestamp="`date --utc '+%Y-%m-%dT%H:%M:%S.%NZ' 2>&1`"
if [ ! "${timestamp/illegal/xyz}" = "${timestamp}" ]; then
# old shell versions don't support %N or --utc
timestamp="`date -u '+%Y-%m-%dT%H:%M:%S 000000 %s %Z' 2>&1`"
timestamp="`date -u '+%Y-%m-%dT%H:%M:%S.000000Z' 2>&1`"
fi
local ymdhms="`echo $timestamp | awk '{ print $1 }'`"
# convert nano to milli
local milli="`echo $timestamp | awk '{ print $2 }' | sed 's/\(^...\).*/\1/'`"
local secs_since_epoch="`echo $timestamp | awk '{ print $3 }'`"
local millis_since_opoch="${secs_since_epoch}${milli}"
local timezone="`echo $timestamp | awk '{ print $4 }'`"
echo "${ymdhms}.${milli} ${timezone}"
echo "${timestamp}"
}

#
Expand Down