Skip to content

Commit

Permalink
Update logging to use java.time,
Browse files Browse the repository at this point in the history
deprecate usages of Date/DateFormat for removal,
remove most of the dependencies on internal packages

Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Aug 21, 2023
1 parent 352a1e6 commit d1df119
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2647,10 +2647,11 @@ public Writer getLog() {
* <p>
* This should be the implementation of toString(), and also the
* value should be calculated in the constructor for it is used all the
* time. However everything is lazily initialized now and the value is
* time. However, everything is lazily initialized now and the value is
* transient for the system hashcode could vary?
*/
public String getLogSessionString() {
@Override
public String getSessionId() {
if (logSessionString == null) {
StringWriter writer = new StringWriter();
writer.write(getSessionTypeString());
Expand All @@ -2662,6 +2663,21 @@ public String getLogSessionString() {
return logSessionString;
}

/**
* INTERNAL:
* Return the name of the session: class name + system hashcode.
* <p>
* This should be the implementation of toString(), and also the
* value should be calculated in the constructor for it is used all the
* time. However, everything is lazily initialized now and the value is
* transient for the system hashcode could vary?
* @deprecated Use {@link #getSessionId()}.
*/
@Deprecated(forRemoval = true)
public String getLogSessionString() {
return getSessionId();
}

/**
* INTERNAL:
* Returns the type of session, its class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
// - 526957 : Split the logging and trace messages
package org.eclipse.persistence.logging;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.localization.LoggingLocalization;
import org.eclipse.persistence.internal.localization.TraceLocalization;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.sessions.Session;

import java.io.IOException;
Expand All @@ -32,15 +29,19 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
* Represents the abstract log that implements all the generic logging functions.
* It contains a singleton SessionLog that logs messages from outside any EclipseLink session.
* The singleton SessionLog can also be passed to an EclipseLink session when messages
* are logged through that session. When JDK1.4 is used, a singleton JavaLog is created.
* Otherwise a singleton DefaultSessionLog is created.
* are logged through that session. By default, a singleton {@linkplain DefaultSessionLog} is created.
*
* @see SessionLog
* @see SessionLogEntry
Expand Down Expand Up @@ -119,12 +120,24 @@ public abstract class AbstractSessionLog implements SessionLog, java.lang.Clonea
*/
protected Writer writer;

/**
* The pattern for formatting date-time in the log entries.
* By default, set to {@code "yyyy.MM.dd HH:mm:ss.SSS"}.
*/
protected static String DATE_FORMAT_STR = "yyyy.MM.dd HH:mm:ss.SSS";

/**
* Format use to print the current date/time.
* @deprecated Use {@link #timeStampFormatter}.
*/
@Deprecated(forRemoval = true)
protected DateFormat dateFormat;

/**
* Formatter used to print the current date/time.
*/
protected DateTimeFormatter timeStampFormatter;

/**
* Allows the printing of the stack to be explicitly disabled/enabled.
* CR #3870467.
Expand Down Expand Up @@ -168,7 +181,11 @@ public abstract class AbstractSessionLog implements SessionLog, java.lang.Clonea
* @return The system default log level property value or {@code null} if no such property is set.
*/
private static String getDefaultLoggingLevelProperty() {
return PrivilegedAccessHelper.getSystemProperty(PersistenceUnitProperties.LOGGING_LEVEL);
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("eclipselink.logging.level", null));
} else {
return System.getProperty("eclipselink.logging.level", null);
}
}

/**
Expand Down Expand Up @@ -579,16 +596,6 @@ public void log(int level, String category, String message, Object[] params, boo
log(new SessionLogEntry(level, category, null, message, params, null, shouldTranslate));
}

/**
* PUBLIC:
* <p>
* Log a SessionLogEntry
*
* @param sessionLogEntry SessionLogEntry that holds all the information for an EclipseLink logging event
*/
@Override
public abstract void log(SessionLogEntry sessionLogEntry);

/**
* By default the session (and its connection is available) are printed,
* this can be turned off.
Expand Down Expand Up @@ -752,28 +759,45 @@ public void setWriter(OutputStream outputstream) {
* PUBLIC:
* Return the date format to be used when printing a log entry date.
* @return the date format
* @deprecated Use {@link #getTimeStampFormatter()}.
*/
@Deprecated(forRemoval = true)
public DateFormat getDateFormat() {
return dateFormat;
}

/**
* Return the specified date and/or time information in string.
* The format will be determined by the date format settings.
* @deprecated Use {@link #getTimeStampString(TemporalAccessor)}.
*/
@Deprecated(forRemoval = true)
protected String getDateString(Date date) {
if (getDateFormat() != null) {
return getDateFormat().format(date);
if (date == null) {
return null;
}
return getTimeStampString(date.toInstant());
}

/**
* Return the specified date and/or time information in string.
* The format will be determined by the {@linkplain DateTimeFormatter} settings.
* By default, the value of the {@linkplain #DATE_FORMAT_STR} pattern is used.
*/
public DateTimeFormatter getTimeStampFormatter() {
if (timeStampFormatter == null) {
timeStampFormatter = DateTimeFormatter
.ofPattern(DATE_FORMAT_STR)
.withZone(ZoneId.systemDefault());
}
return timeStampFormatter;
}

if (date == null) {
protected String getTimeStampString(TemporalAccessor temporalAccessor) {
if (temporalAccessor == null) {
return null;
}

// Since we currently do not have a thread-safe way to format dates,
// we will use ConversionManager to build the string.
return ConversionManager.getDefaultManager().convertObject(date, String.class).toString();
return getTimeStampFormatter().format(temporalAccessor);
}

/**
Expand All @@ -784,7 +808,7 @@ protected String getSupplementDetailString(SessionLogEntry entry) {
StringWriter writer = new StringWriter();

if (shouldPrintDate()) {
writer.write(getDateString(entry.getDate()));
writer.write(getTimeStampString(entry.getTimeStamp()));
writer.write("--");
}
if (shouldPrintSession() && (entry.getSession() != null)) {
Expand Down Expand Up @@ -818,7 +842,7 @@ protected String getSessionString(Session session) {
// event, not the static one in the SessionLog, for there are many
// sessions but only one SessionLog.
if (session != null) {
return ((org.eclipse.persistence.internal.sessions.AbstractSession)session).getLogSessionString();
return session.getSessionId();
} else {
return "";
}
Expand Down Expand Up @@ -913,15 +937,26 @@ protected void printPrefixString(int level, String category) {
* PUBLIC:
* Set the date format to be used when printing a log entry date.
* <p>Note: the JDK's <code>java.text.SimpleDateFormat</code> is <b>NOT</b> thread-safe.<br>
* The user is <b>strongly</b> advised to consider using Apache Commons<br>
* <code>org.apache.commons.lang.time.FastDateFormat</code> instead.</p>
* The user is <b>strongly</b> advised to use {@linkplain #setTimeStampFormatter(DateTimeFormatter)} instead.</p>
*
* @param dateFormat java.text.DateFormat
* @deprecated Use {@link #setTimeStampFormatter(DateTimeFormatter)}.
*/
@Deprecated(forRemoval = true)
public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

/**
* PUBLIC:
* Set the date-time format to be used when printing a log entry date.
*
* @param timeStampFormatter Formatter for printing time stamp in the log entry.
*/
public void setTimeStampFormatter(DateTimeFormatter timeStampFormatter) {
this.timeStampFormatter = timeStampFormatter;
}

/**
* Return the formatted message based on the information from the given SessionLogEntry.
* The message will either be translated and formatted or formatted only depending
Expand All @@ -938,7 +973,7 @@ protected String formatMessage(SessionLogEntry entry) {
} else {
//Bug5976657, if there are entry parameters and the string "{0" contained in the message
//body, we assume it needs to be formatted.
if (entry.getParameters()!=null && entry.getParameters().length>0 && message.indexOf("{0") >= 0) {
if (entry.getParameters() != null && entry.getParameters().length > 0 && message.contains("{0")) {
message = java.text.MessageFormat.format(message, entry.getParameters());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ public void setLevel(final int level, String category) {
if (logger == null) {
return;
}

AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
logger.setLevel(getJavaLevel(level));
return null; // nothing to return
}
});
});
} else {
logger.setLevel(getJavaLevel(level));
}
}

/**
Expand All @@ -173,7 +173,7 @@ public void setWriter(OutputStream fileOutputStream){
protected String getNameSpaceString(String category) {
if (session == null) {
return DEFAULT_TOPLINK_NAMESPACE;
} else if ((category == null) || (category.length() == 0)) {
} else if ((category == null) || (category.isEmpty())) {
return sessionNameSpace;
} else {
return nameSpaceMap.get(category);
Expand All @@ -187,7 +187,7 @@ protected String getNameSpaceString(String category) {
protected Logger getLogger(String category) {
if (session == null) {
return categoryloggers.get(DEFAULT_TOPLINK_NAMESPACE);
} else if ((category == null) || (category.length() == 0) || !this.categoryloggers.containsKey(category)) {
} else if ((category == null) || (category.isEmpty()) || !this.categoryloggers.containsKey(category)) {
return categoryloggers.get(sessionNameSpace);
} else {
Logger logger = categoryloggers.get(category);
Expand All @@ -210,7 +210,7 @@ public void setSession(Session session) {
super.setSession(session);
if (session != null) {
String sessionName = session.getName();
if ((sessionName != null) && (sessionName.length() != 0)) {
if ((sessionName != null) && (!sessionName.isEmpty())) {
sessionNameSpace = SESSION_TOPLINK_NAMESPACE + "." + sessionName;
} else {
sessionNameSpace = DEFAULT_TOPLINK_NAMESPACE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// Tomas Kraus - Initial implementation
package org.eclipse.persistence.logging;

import org.eclipse.persistence.config.PersistenceUnitProperties;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -91,14 +89,14 @@ public enum LogCategory {
private static final String[] levelNameSpaces = new String[length];

static {
// Initialize String to LogCategory case insensitive lookup Map.
// Initialize String to LogCategory case-insensitive lookup Map.
for (LogCategory category : LogCategory.values()) {
stringValuesMap.put(category.name.toLowerCase(), category);
}
// Initialize logger name spaces lookup table.
for (LogCategory category : LogCategory.values()) {
nameSpaces[category.id] = (NAMESPACE_PREFIX + category.name).intern();
levelNameSpaces[category.id] = (PersistenceUnitProperties.CATEGORY_LOGGING_LEVEL_ + category.name).intern();
levelNameSpaces[category.id] = ("eclipselink.logging.level." + category.name).intern();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public interface SessionLog extends Cloneable {
* All the pertinent information will be contained in
* the specified entry.
*
* @param entry org.eclipse.persistence.sessions.LogEntry
* @param entry SessionLogEntry that holds all the information for an EclipseLink logging event
*/
void log(SessionLogEntry entry);

Expand Down

0 comments on commit d1df119

Please sign in to comment.