Skip to content

Commit 912b312

Browse files
authored
Merge pull request #113 from ebean-orm/feature/system-logger
Replace slf4j-api with java platform logger (System.Logger)
2 parents 63a5e6f + 9c5db50 commit 912b312

File tree

10 files changed

+76
-84
lines changed

10 files changed

+76
-84
lines changed

ebean-migration/pom.xml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
</properties>
2323

2424
<dependencies>
25-
2625
<dependency>
27-
<groupId>org.slf4j</groupId>
28-
<artifactId>slf4j-api</artifactId>
29-
<version>1.7.36</version>
30-
<scope>provided</scope>
26+
<groupId>io.avaje</groupId>
27+
<artifactId>avaje-applog</artifactId>
28+
<version>1.0</version>
3129
</dependency>
3230

3331
<dependency>
@@ -58,6 +56,13 @@
5856

5957
<!-- test dependencies -->
6058

59+
<dependency>
60+
<groupId>io.avaje</groupId>
61+
<artifactId>avaje-slf4j-jpl</artifactId>
62+
<version>1.1</version>
63+
<scope>test</scope>
64+
</dependency>
65+
6166
<dependency>
6267
<groupId>com.h2database</groupId>
6368
<artifactId>h2</artifactId>
@@ -82,7 +87,7 @@
8287
<dependency>
8388
<groupId>org.postgresql</groupId>
8489
<artifactId>postgresql</artifactId>
85-
<version>42.3.3</version>
90+
<version>42.4.2</version>
8691
<scope>test</scope>
8792
</dependency>
8893

@@ -103,7 +108,7 @@
103108
<dependency>
104109
<groupId>org.mariadb.jdbc</groupId>
105110
<artifactId>mariadb-java-client</artifactId>
106-
<version>2.6.0</version>
111+
<version>3.0.6</version>
107112
<scope>test</scope>
108113
</dependency>
109114

ebean-migration/src/main/java/io/ebean/migration/DbNameUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010

11+
import static java.lang.System.Logger.Level.WARNING;
12+
1113
/**
1214
* Derive well known database platform names from JDBC MetaData.
1315
*/
@@ -64,7 +66,7 @@ private static String readPostgres(Connection connection) {
6466
}
6567
}
6668
} catch (SQLException e) {
67-
MigrationRunner.log.warn("Error running detection query on Postgres", e);
69+
MigrationRunner.log.log(WARNING, "Error running detection query on Postgres", e);
6870
}
6971
return POSTGRES;
7072
}

ebean-migration/src/main/java/io/ebean/migration/MigrationRunner.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
package io.ebean.migration;
22

3-
import io.ebean.migration.runner.LocalMigrationResource;
4-
import io.ebean.migration.runner.LocalMigrationResources;
5-
import io.ebean.migration.runner.MigrationPlatform;
6-
import io.ebean.migration.runner.MigrationSchema;
7-
import io.ebean.migration.runner.MigrationTable;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
3+
import io.avaje.applog.AppLog;
4+
import io.ebean.migration.runner.*;
105

116
import javax.annotation.Nonnull;
127
import javax.sql.DataSource;
138
import java.sql.Connection;
149
import java.sql.SQLException;
1510
import java.util.List;
1611

12+
import static java.lang.System.Logger.Level.*;
13+
1714
/**
1815
* Runs the DB migration typically on application start.
1916
*/
2017
public class MigrationRunner {
2118

22-
static final Logger log = LoggerFactory.getLogger("io.ebean.migration");
19+
static final System.Logger log = AppLog.getLogger("io.ebean.migration");
2320

2421
protected final MigrationConfig migrationConfig;
2522

@@ -81,7 +78,7 @@ private Connection getConnection(DataSource dataSource) {
8178
if (username == null) {
8279
return dataSource.getConnection();
8380
}
84-
log.debug("using db user [{}] to run migrations ...", username);
81+
log.log(DEBUG, "using db user [{0}] to run migrations ...", username);
8582
return dataSource.getConnection(username, migrationConfig.getDbPassword());
8683
} catch (SQLException e) {
8784
String msgSuffix = (username == null) ? "" : " using user [" + username + "]";
@@ -96,7 +93,7 @@ protected void run(Connection connection, boolean checkStateMode) {
9693
try {
9794
LocalMigrationResources resources = new LocalMigrationResources(migrationConfig);
9895
if (!resources.readResources() && !resources.readInitResources()) {
99-
log.debug("no migrations to check");
96+
log.log(DEBUG, "no migrations to check");
10097
return;
10198
}
10299

@@ -137,12 +134,12 @@ private void runMigrations(LocalMigrationResources resources, MigrationTable tab
137134
LocalMigrationResource initVersion = getInitVersion();
138135
if (initVersion != null) {
139136
// run using a dbinit script
140-
log.info("dbinit migration version:{} local migrations:{} checkState:{}", initVersion, localVersions.size(), checkStateMode);
137+
log.log(INFO, "dbinit migration version:{0} local migrations:{1} checkState:{2}", initVersion, localVersions.size(), checkStateMode);
141138
checkMigrations = table.runInit(initVersion, localVersions);
142139
return;
143140
}
144141
}
145-
log.info("Local migrations:{} existing migrations:{} checkState:{}", localVersions.size(), table.size(), checkStateMode);
142+
log.log(INFO, "Local migrations:{0} existing migrations:{1} checkState:{2}", localVersions.size(), table.size(), checkStateMode);
146143
checkMigrations = table.runAll(localVersions);
147144
}
148145

@@ -183,7 +180,7 @@ private void close(Connection connection) {
183180
connection.close();
184181
}
185182
} catch (SQLException e) {
186-
log.warn("Error closing connection", e);
183+
log.log(WARNING, "Error closing connection", e);
187184
}
188185
}
189186

@@ -196,7 +193,7 @@ private void rollback(Connection connection) {
196193
connection.rollback();
197194
}
198195
} catch (SQLException e) {
199-
log.warn("Error on connection rollback", e);
196+
log.log(WARNING, "Error on connection rollback", e);
200197
}
201198
}
202199
}

ebean-migration/src/main/java/io/ebean/migration/MigrationVersion.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Arrays;
44

5+
import static java.lang.System.Logger.Level.*;
6+
57
/**
68
* The version of a migration used so that migrations are processed in order.
79
*/
@@ -216,8 +218,8 @@ public static MigrationVersion parse(String raw) {
216218
delimiterPos++;
217219
} catch (NumberFormatException e) {
218220
// stop parsing
219-
MigrationRunner.log.warn("The migrationscript '{}' contains non numeric version part. "
220-
+ "This may lead to misordered version scripts. NumberFormatException {}", raw, e.getMessage());
221+
MigrationRunner.log.log(WARNING, "The migrationscript '{0}' contains non numeric version part. "
222+
+ "This may lead to misordered version scripts. NumberFormatException {1}", raw, e.getMessage());
221223
break;
222224
}
223225
}

ebean-migration/src/main/java/io/ebean/migration/runner/IOUtils.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.OutputStream;
7-
import java.io.UnsupportedEncodingException;
87
import java.net.URL;
98
import java.net.URLConnection;
9+
import java.nio.charset.StandardCharsets;
1010

1111
/**
1212
* Utilities for IO.
1313
*/
14-
class IOUtils {
14+
final class IOUtils {
1515

1616
/**
1717
* Reads the entire contents of the specified URL and return them as UTF-8 string.
1818
*/
1919
static String readUtf8(URL url) throws IOException {
2020
URLConnection urlConnection = url.openConnection();
2121
urlConnection.setUseCaches(false);
22-
return readUtf8(urlConnection.getInputStream());
22+
try (InputStream is = urlConnection.getInputStream()) {
23+
return readUtf8(is);
24+
}
2325
}
2426

2527
/**
@@ -42,11 +44,7 @@ private static byte[] read(InputStream in) throws IOException {
4244
* Returns the UTF-8 string corresponding to the specified bytes.
4345
*/
4446
private static String bytesToUtf8(byte[] data) {
45-
try {
46-
return new String(data, "UTF-8");
47-
} catch (UnsupportedEncodingException e) {
48-
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
49-
}
47+
return new String(data, StandardCharsets.UTF_8);
5048
}
5149

5250
/**
@@ -57,8 +55,8 @@ private static String bytesToUtf8(byte[] data) {
5755
private static void pump(InputStream in, OutputStream out) throws IOException {
5856
if (in == null) throw new IOException("Input stream is null");
5957
if (out == null) throw new IOException("Output stream is null");
60-
try {
61-
try {
58+
try (out) {
59+
try (in) {
6260
byte[] buffer = new byte[4096];
6361
for (; ; ) {
6462
int bytes = in.read(buffer);
@@ -67,11 +65,7 @@ private static void pump(InputStream in, OutputStream out) throws IOException {
6765
}
6866
out.write(buffer, 0, bytes);
6967
}
70-
} finally {
71-
in.close();
7268
}
73-
} finally {
74-
out.close();
7569
}
7670
}
7771
}

ebean-migration/src/main/java/io/ebean/migration/runner/LocalMigrationResources.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
import io.ebean.migration.JdbcMigration;
66
import io.ebean.migration.MigrationConfig;
77
import io.ebean.migration.MigrationVersion;
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
108

119
import javax.annotation.Nonnull;
1210
import java.util.ArrayList;
1311
import java.util.Collections;
1412
import java.util.List;
1513
import java.util.function.Predicate;
1614

15+
import static java.lang.System.Logger.Level.DEBUG;
16+
1717
/**
1818
* Loads the DB migration resources and sorts them into execution order.
1919
*/
2020
public class LocalMigrationResources {
2121

22-
private static final Logger log = LoggerFactory.getLogger("io.ebean.migration");
22+
private static final System.Logger log = MigrationSchema.log;
2323

2424
private final List<LocalMigrationResource> versions = new ArrayList<>();
2525
private final MigrationConfig migrationConfig;
@@ -73,7 +73,7 @@ private boolean loadedFrom(String path, String platform) {
7373
if (versions.isEmpty()) {
7474
return false;
7575
}
76-
log.debug("platform migrations for {}", platform);
76+
log.log(DEBUG, "platform migrations for {0}", platform);
7777
if (searchForJdbcMigrations) {
7878
addResources(scanForJdbcOnly(path));
7979
}
@@ -97,7 +97,7 @@ private List<Resource> scanForBoth(String path) {
9797

9898
private void addResources(List<Resource> resourceList) {
9999
if (!resourceList.isEmpty()) {
100-
log.debug("resources: {}", resourceList);
100+
log.log(DEBUG, "resources: {0}", resourceList);
101101
}
102102
for (Resource resource : resourceList) {
103103
String filename = resource.name();

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationPlatform.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.ebean.migration.runner;
22

3-
43
import io.ebean.ddlrunner.DdlDetect;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
74

85
import javax.annotation.Nonnull;
96
import java.sql.Connection;
@@ -13,12 +10,14 @@
1310
import java.util.ArrayList;
1411
import java.util.List;
1512

13+
import static java.lang.System.Logger.Level.*;
14+
1615
/**
1716
* Handle database platform specific locking on db migration table.
1817
*/
1918
public class MigrationPlatform {
2019

21-
private static final Logger log = LoggerFactory.getLogger("io.ebean.DDL");
20+
private static final System.Logger log = MigrationTable.log;
2221

2322
private static final String BASE_SELECT_ID = "select id from ";
2423
private static final String BASE_SELECT_ALL = "select id, mtype, mstatus, mversion, mcomment, mchecksum, run_on, run_by, run_time from ";
@@ -56,9 +55,9 @@ void lockMigrationTable(String sqlTable, Connection connection) throws SQLExcept
5655
private static void backoff(int attempt) {
5756
try {
5857
if (attempt % 100 == 0) {
59-
log.warn("In backoff loop attempting to obtain lock on DBMigration table ...");
58+
log.log(WARNING, "In backoff loop attempting to obtain lock on DBMigration table ...");
6059
} else {
61-
log.trace("in backoff loop obtaining lock...");
60+
log.log(TRACE, "in backoff loop obtaining lock...");
6261
}
6362
Thread.sleep(100);
6463
} catch (InterruptedException e) {
@@ -122,7 +121,7 @@ void lockMigrationTable(String sqlTable, Connection connection) throws SQLExcept
122121
while (!obtainLogicalLock(sqlTable, connection)) {
123122
backoff(++attempts);
124123
}
125-
log.trace("obtained logical lock");
124+
log.log(TRACE, "obtained logical lock");
126125
}
127126

128127
@Override
@@ -150,9 +149,9 @@ private void releaseLogicalLock(String sqlTable, Connection connection) throws S
150149
String sql = "update " + sqlTable + " set mcomment='<init>' where id=0";
151150
try (PreparedStatement query = connection.prepareStatement(sql)) {
152151
if (query.executeUpdate() != 1) {
153-
log.error("Failed to release logical lock. Please review why [" + sql + "] didn't update the row?");
152+
log.log(ERROR, "Failed to release logical lock. Please review why [" + sql + "] didn't update the row?");
154153
} else {
155-
log.trace("released logical lock");
154+
log.log(TRACE, "released logical lock");
156155
}
157156
}
158157
}

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package io.ebean.migration.runner;
22

3+
import io.avaje.applog.AppLog;
34
import io.ebean.migration.MigrationConfig;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
65

76
import java.sql.Connection;
87
import java.sql.ResultSet;
98
import java.sql.SQLException;
109
import java.sql.Statement;
1110

11+
import static java.lang.System.Logger.Level.INFO;
12+
1213
/**
1314
* Create Schema if needed and set current Schema in Migration
1415
*/
15-
public class MigrationSchema {
16+
public final class MigrationSchema {
1617

17-
private static final Logger log = LoggerFactory.getLogger("io.ebean.migration");
18+
static final System.Logger log = AppLog.getLogger("io.ebean.migration");
1819

1920
private final Connection connection;
2021
private final String dbSchema;
@@ -40,7 +41,7 @@ private String trim(String dbSchema) {
4041
*/
4142
public void createAndSetIfNeeded() throws SQLException {
4243
if (dbSchema != null) {
43-
log.info("Migration Schema: {}", dbSchema);
44+
log.log(INFO, "Migration Schema: {0}", dbSchema);
4445
if (createSchemaIfNotExists) {
4546
createSchemaIfNeeded();
4647
}
@@ -52,7 +53,7 @@ public void createAndSetIfNeeded() throws SQLException {
5253

5354
private void createSchemaIfNeeded() throws SQLException {
5455
if (!schemaExists()) {
55-
log.info("Creating Schema: {}", dbSchema);
56+
log.log(INFO, "Creating Schema: {0}", dbSchema);
5657
try (Statement query = connection.createStatement()) {
5758
query.executeUpdate("CREATE SCHEMA " + dbSchema);
5859
}
@@ -72,7 +73,7 @@ private boolean schemaExists() throws SQLException {
7273
}
7374

7475
private void setSchema() throws SQLException {
75-
log.info("Setting Schema: {}", dbSchema);
76+
log.log(INFO, "Setting Schema: {0}", dbSchema);
7677
connection.setSchema(dbSchema);
7778
}
7879

0 commit comments

Comments
 (0)