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

Use AppLog and System.Logger for logging rather than slf4j-api #2797

Merged
merged 2 commits into from
Aug 23, 2022
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
28 changes: 16 additions & 12 deletions ebean-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,23 @@

<dependencies>

<!--
Projects are expected to explicit depend on version
of slf4j that they want to use
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<scope>provided</scope>
<groupId>io.avaje</groupId>
<artifactId>avaje-applog</artifactId>
<version>1.0</version>
</dependency>

<!-- exclude avaje-applog-slf4j to direct logging to something else -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-config</artifactId>
<version>2.2</version>
<artifactId>avaje-applog-slf4j</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-applog-slf4j</artifactId>
<version>1.0</version>
<artifactId>avaje-config</artifactId>
<version>2.2</version>
</dependency>

<!--
Expand Down Expand Up @@ -70,6 +66,14 @@
<version>${ebean-datasource.version}</version>
</dependency>

<!-- Support MdcBackgroundExecutorWrapper -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<optional>true</optional>
</dependency>

<!-- Jackson core used internally by Ebean -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
9 changes: 5 additions & 4 deletions ebean-api/src/main/java/io/ebean/DbContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import io.ebean.config.BeanNotEnhancedException;
import io.ebean.datasource.DataSourceConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.PersistenceException;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.System.Logger.Level.ERROR;

/**
* Holds Database instances.
*/
final class DbContext {

private static final Logger log = LoggerFactory.getLogger("io.ebean");
private static final System.Logger log = EbeanVersion.log;

static {
EbeanVersion.getVersion();
}
Expand Down Expand Up @@ -52,7 +53,7 @@ private DbContext() {
throw new DataSourceConfigurationException(msg, e);

} catch (Throwable e) {
log.error("Error trying to create the default Database", e);
log.log(ERROR, "Error trying to create the default Database", e);
throw new RuntimeException(e);
}
}
Expand Down
21 changes: 13 additions & 8 deletions ebean-api/src/main/java/io/ebean/EbeanVersion.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package io.ebean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.avaje.applog.AppLog;

import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Properties;

import static java.lang.System.Logger.Level.*;

/**
* Class to determine the ebean version.
*
* @author Roland Praml, FOCONIS AG
*/
public final class EbeanVersion {

private static final Logger log = LoggerFactory.getLogger("io.ebean");
public static final System.Logger log = AppLog.getLogger("io.ebean");

/**
* Maintain the minimum ebean-agent version manually based on required ebean-agent bug fixes.
*/
private static final int MIN_AGENT_MAJOR_VERSION = 12;
private static final int MIN_AGENT_MINOR_VERSION = 12;
private static String version = "unknown";

static {
readVersion();
checkAgentVersion();
Expand All @@ -32,12 +37,12 @@ private static void readVersion() {
if (in != null) {
try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(in))) {
version = reader.readLine();
log.info("ebean version: {}", version);
log.log(INFO, "ebean version: {0}", version);
}
}
}
} catch (IOException e) {
log.warn("Could not determine ebean version: {}", e.getMessage());
log.log(WARNING, "Could not determine ebean version: {0}", e.getMessage());
}
}

Expand All @@ -49,13 +54,13 @@ private static void checkAgentVersion() {
String agentVersion = readVersion(in);
if (agentVersion != null) {
if (checkMinAgentVersion(agentVersion)) {
log.error("Expected minimum ebean-agent version {}.{}.0 but we have {}, please update the ebean-agent", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, agentVersion);
log.log(ERROR, "Expected minimum ebean-agent version {0}.{1}.0 but we have {2}, please update the ebean-agent", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, agentVersion);
}
}
}
}
} catch (IOException e) {
log.warn("Could not check minimum ebean-agent version {}.{}.0 required due to - {}", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, e.getMessage());
log.log(WARNING, "Could not check minimum ebean-agent version {0}.{1}.0 required due to - {2}", MIN_AGENT_MAJOR_VERSION, MIN_AGENT_MINOR_VERSION, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package io.ebean.config.dbplatform;

import io.ebean.BackgroundExecutor;
import io.ebean.EbeanVersion;
import io.ebean.Query;
import io.ebean.annotation.PartitionMode;
import io.ebean.annotation.PersistBatch;
import io.ebean.annotation.Platform;
import io.ebean.config.CustomDbTypeMapping;
import io.ebean.config.PlatformConfig;
import io.ebean.util.JdbcClose;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.PersistenceException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.sql.*;

import static java.lang.System.Logger.Level.*;

/**
* Database platform specific settings.
*/
public class DatabasePlatform {

private static final Logger log = LoggerFactory.getLogger("io.ebean");
private static final System.Logger log = EbeanVersion.log;

/**
* Behavior used when ending a query only transaction (at read committed isolation level).
Expand Down Expand Up @@ -60,7 +54,7 @@ public enum OnQueryOnly {
* Can we use native java time API objects in
* {@link ResultSet#getObject(int, Class)} and
* {@link PreparedStatement#setObject(int, Object)}.
*
* <p>
* Not all drivers (DB2 e.g.) will support this.
*/
protected boolean supportsNativeJavaTime = true;
Expand Down Expand Up @@ -165,7 +159,7 @@ public enum OnQueryOnly {
* want to use quoted identifiers for. The backticks get converted to the
* appropriate characters in convertQuotedIdentifiers
*/
private static final char[] QUOTED_IDENTIFIERS = new char[] { '"', '\'', '[', ']', '`' };
private static final char[] QUOTED_IDENTIFIERS = new char[]{'"', '\'', '[', ']', '`'};

/**
* The non-escaped like clause (to stop slash being escaped on some platforms).
Expand Down Expand Up @@ -666,7 +660,7 @@ public String convertQuotedIdentifiers(String dbName) {
if (isQuote(dbName.charAt(dbName.length() - 1))) {
return openQuote + dbName.substring(1, dbName.length() - 1) + closeQuote;
} else {
log.error("Missing backquote on [" + dbName + "]");
log.log(ERROR, "Missing backquote on [" + dbName + "]");
}
} else if (allQuotedIdentifiers) {
return openQuote + dbName + closeQuote;
Expand Down Expand Up @@ -729,7 +723,7 @@ public String fromForUpdate(Query.LockWait lockWait) {

protected String withForUpdate(String sql, Query.LockWait lockWait, Query.LockType lockType) {
// silently assume the database does not support the "for update" clause.
log.info("it seems your database does not support the 'for update' clause");
log.log(INFO, "it seems your database does not support the 'for update' clause");
return sql;
}

Expand Down Expand Up @@ -763,7 +757,7 @@ public void createSchemaIfNotExists(String dbSchema, Connection connection) thro
if (!schemaExists(dbSchema, connection)) {
Statement query = connection.createStatement();
try {
log.debug("create schema:{}", dbSchema);
log.log(DEBUG, "create schema:{0}", dbSchema);
query.executeUpdate("create schema " + dbSchema);
} finally {
JdbcClose.close(query);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.ebean.config.dbplatform;

import io.avaje.applog.AppLog;
import io.ebean.BackgroundExecutor;
import io.ebean.Transaction;
import io.ebean.util.JdbcClose;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.PersistenceException;
import javax.sql.DataSource;
Expand All @@ -19,12 +18,15 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;

/**
* Database sequence based IdGenerator.
*/
public abstract class SequenceIdGenerator implements PlatformIdGenerator {

protected static final Logger log = LoggerFactory.getLogger("io.ebean.SEQ");
protected static final System.Logger log = AppLog.getLogger("io.ebean.SEQ");

private final ReentrantLock lock = new ReentrantLock();
protected final String seqName;
Expand Down Expand Up @@ -120,7 +122,7 @@ private void loadMore(int requestSize) {
protected void loadInBackground(final int requestSize) {
if (currentlyBackgroundLoading.get()) {
// skip as already background loading
log.debug("... skip background sequence load (another load in progress)");
log.log(DEBUG, "... skip background sequence load (another load in progress)");
return;
}
currentlyBackgroundLoading.set(true);
Expand Down Expand Up @@ -152,9 +154,6 @@ protected List<Long> getMoreIds(int requestSize) {
resultSet = statement.executeQuery();

List<Long> newIds = readIds(resultSet, requestSize);
if (log.isTraceEnabled()) {
log.trace("seq:{} loaded:{} sql:{}", seqName, newIds.size(), sql);
}
if (newIds.isEmpty()) {
throw new PersistenceException("Always expecting more than 1 row from " + sql);
}
Expand All @@ -164,7 +163,7 @@ protected List<Long> getMoreIds(int requestSize) {
} catch (SQLException e) {
if (e.getMessage().contains("Database is already closed")) {
String msg = "Error getting SEQ when DB shutting down " + e.getMessage();
log.error(msg);
log.log(ERROR, msg);
System.out.println(msg);
return Collections.emptyList();
} else {
Expand Down
21 changes: 12 additions & 9 deletions ebean-api/src/main/java/io/ebean/event/ShutdownManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.ebean.event;

import io.ebean.Database;
import io.ebean.EbeanVersion;
import io.ebean.service.SpiContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Driver;
import java.sql.DriverManager;
Expand All @@ -13,20 +12,23 @@
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.System.Logger.Level.*;

/**
* Manages the shutdown of Ebean.
* <p>
* Makes sure all the resources are shutdown properly and in order.
*/
public final class ShutdownManager {

private static final Logger log = LoggerFactory.getLogger("io.ebean");
private static final System.Logger log = EbeanVersion.log;
private static final ReentrantLock lock = new ReentrantLock();
private static final List<Database> databases = new ArrayList<>();
private static final ShutdownHook shutdownHook = new ShutdownHook();

private static boolean stopping;
private static SpiContainer container;

static {
// Register the Shutdown hook
registerShutdownHook();
Expand Down Expand Up @@ -120,8 +122,8 @@ public static void shutdown() {
// Already run shutdown...
return;
}
if (log.isDebugEnabled()) {
log.debug("Ebean shutting down");
if (log.isLoggable(DEBUG)) {
log.log(DEBUG, "Ebean shutting down");
}
stopping = true;
deregisterShutdownHook();
Expand All @@ -133,7 +135,7 @@ public static void shutdown() {
Runnable r = (Runnable) ClassUtil.newInstance(shutdownRunner);
r.run();
} catch (Exception e) {
log.error("Error running custom shutdown runnable", e);
log.log(ERROR, "Error running custom shutdown runnable", e);
}
}

Expand All @@ -147,7 +149,7 @@ public static void shutdown() {
try {
server.shutdown();
} catch (Exception ex) {
log.error("Error executing shutdown runnable", ex);
log.log(ERROR, "Error executing shutdown runnable", ex);
ex.printStackTrace();
}
}
Expand All @@ -165,10 +167,10 @@ private static void deregisterAllJdbcDrivers() {
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
log.info("De-registering jdbc driver: " + driver);
log.log(INFO, "De-registering jdbc driver: " + driver);
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
log.error("Error de-registering driver " + driver, e);
log.log(ERROR, "Error de-registering driver " + driver, e);
}
}
}
Expand Down Expand Up @@ -204,6 +206,7 @@ private static class ShutdownHook extends Thread {
private ShutdownHook() {
super("EbeanHook");
}

@Override
public void run() {
ShutdownManager.shutdown();
Expand Down
Loading