Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed logging of java.sql.Date #1378

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -26,6 +26,7 @@
public final class StringBuilders {

private static final Class<?> timeClass;
private static final Class<?> dateClass;

static {
Class<?> clazz;
Expand All @@ -35,6 +36,13 @@ public final class StringBuilders {
clazz = null;
}
timeClass = clazz;

try {
clazz = Class.forName("java.sql.Date");
} catch(ClassNotFoundException ex) {
clazz = null;
}
dateClass = clazz;
}

private StringBuilders() {
Expand Down Expand Up @@ -110,7 +118,7 @@ public static boolean appendSpecificTypes(final StringBuilder stringBuilder, fin
stringBuilder.append(((Float) obj).floatValue());
} else if (obj instanceof Byte) {
stringBuilder.append(((Byte) obj).byteValue());
} else if (isTime(obj) || obj instanceof java.time.temporal.Temporal) {
} else if (isTime(obj) || isDate(obj) || obj instanceof java.time.temporal.Temporal) {
stringBuilder.append(obj);
} else {
return false;
Expand All @@ -119,12 +127,18 @@ public static boolean appendSpecificTypes(final StringBuilder stringBuilder, fin
}

/*
Check to see if obj is an instance of java.sql.time without requiring the java.sql module.
Check to see if obj is an instance of java.sql.Time without requiring the java.sql module.
*/
private static boolean isTime(final Object obj) {
return timeClass != null && timeClass.isAssignableFrom(obj.getClass());
}

/*
Check to see if obj is an instance of java.sql.Date without requiring the java.sql module.
*/
private static boolean isDate(final Object obj) {
return dateClass != null && dateClass.isAssignableFrom(obj.getClass());
}

/**
* Returns true if the specified section of the left CharSequence equals the specified section of the right
Expand Down
28 changes: 28 additions & 0 deletions src/changelog/.2.x.x/1366_fix_java_sql_date.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
type="fixed">
<issue id="1366" link="https://github.com/apache/logging-log4j2/pull/1366"/>
<author id="Hikarikun92"/>
<author name="Lucas Souza"/>
<description format="asciidoc">
Fixed logging of java.sql.Date objects by appending it before Log4J tries to call java.util.Date.toInstant() on it.
</description>
</entry>