Skip to content

Commit a852fc6

Browse files
authored
Updated simplelogging (#311)
* Updated simplelogging
1 parent c80bde1 commit a852fc6

File tree

9 files changed

+35
-34
lines changed

9 files changed

+35
-34
lines changed

pom.xml

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
<easymock-version>3.4</easymock-version>
5454
<h2-version>2.2.224</h2-version>
5555
<junit-version>5.10.3</junit-version>
56-
<commons-lang-version>2.1</commons-lang-version>
57-
<commons-logging-version>1.1.1</commons-logging-version>
58-
<logback.version>1.4.12</logback.version>
59-
<slf4j-version>1.7.32</slf4j-version>
56+
<!-- logging test packages -->
57+
<commons-logging-version>1.2</commons-logging-version>
58+
<logback.version>1.3.13</logback.version>
59+
<slf4j-version>2.0.12</slf4j-version>
6060
<log4j2-version>2.21.1</log4j2-version>
6161
</properties>
6262
<profiles>
@@ -340,6 +340,7 @@
340340
<groupId>org.slf4j</groupId>
341341
<artifactId>slf4j-simple</artifactId>
342342
<version>${slf4j-version}</version>
343+
<optional>true</optional>
343344
</dependency>
344345
<dependency>
345346
<groupId>javax.persistence</groupId>

src/main/java/com/j256/ormlite/logger/BaseLogger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public BaseLogger(LogBackend backend) {
3131
}
3232

3333
/**
34-
* Get the global log level. For testing purposes.
34+
* Get the global log level. For testing purposes.
3535
*/
3636
public static Level getGlobalLevel() {
3737
return globalLevel;

src/main/java/com/j256/ormlite/logger/LoggerFactory.java

-13
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ public static void setLogBackendType(LogBackendType type) {
9696
}
9797
}
9898

99-
/**
100-
* Return the single class name from a class-name string.
101-
*/
102-
public static String getSimpleClassName(String className) {
103-
// get the last part of the class name
104-
int index = className.lastIndexOf('.');
105-
if (index < 0 || index == className.length() - 1) {
106-
return className;
107-
} else {
108-
return className.substring(index + 1);
109-
}
110-
}
111-
11299
/**
113100
* Maybe assign the global log level based on the system property. Exposed for testing purposes.
114101
*/

src/main/java/com/j256/ormlite/logger/PropertyUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static List<String[]> readPropertiesFile(LogBackendFactory defaultBackendFactory
175175
// now try to load in properties
176176
InputStream stream = propertiesInputStream;
177177
if (stream == null) {
178-
stream = LoggerFactory.class.getResourceAsStream(LoggerConstants.PROPERTIES_CONFIG_FILE);
178+
stream = PropertyUtils.class.getResourceAsStream(LoggerConstants.PROPERTIES_CONFIG_FILE);
179179
if (stream == null) {
180180
// file not found
181181
return Collections.emptyList();
@@ -220,7 +220,7 @@ private static String trimString(String str) {
220220
}
221221

222222
private static void logWarning(LogBackendFactory defaultBackendFactory, String msg, Throwable th) {
223-
LogBackend backend = defaultBackendFactory.createLogBackend(LoggerFactory.class.getName());
223+
LogBackend backend = defaultBackendFactory.createLogBackend(PropertyUtils.class.getName());
224224
if (th == null) {
225225
backend.log(Level.WARNING, msg);
226226
} else {

src/main/java/com/j256/ormlite/logger/backend/LocalLogBackend.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.j256.ormlite.logger.LogBackendFactory;
1515
import com.j256.ormlite.logger.LogBackendType;
1616
import com.j256.ormlite.logger.LoggerConstants;
17-
import com.j256.ormlite.logger.LoggerFactory;
1817
import com.j256.ormlite.logger.PropertyUtils;
1918
import com.j256.ormlite.logger.PropertyUtils.PatternLevel;
2019

@@ -70,7 +69,12 @@ public class LocalLogBackend implements LogBackend {
7069

7170
public LocalLogBackend(String className) {
7271
// get the last part of the class name
73-
this.className = LoggerFactory.getSimpleClassName(className);
72+
int index = className.lastIndexOf('.');
73+
if (index < 0 || index == className.length() - 1) {
74+
this.className = className;
75+
} else {
76+
this.className = className.substring(index + 1);
77+
}
7478

7579
Level level = null;
7680
if (classLevels != null) {
@@ -156,7 +160,7 @@ private void printMessage(Level level, String message, Throwable throwable) {
156160

157161
/**
158162
* Internal factory for LocalLogBackend instances. This can be used with the
159-
* {@link LoggerFactory#setLogBackendFactory(LogBackendFactory)} method to send all log messages to a file.
163+
* LoggerFactory.setLogBackendFactory(LogBackendFactory) method to send all log messages to a file.
160164
*/
161165
public static class LocalLogBackendFactory implements LogBackendFactory {
162166

src/main/java/com/j256/ormlite/logger/backend/LogbackLogBackend.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
public class LogbackLogBackend implements LogBackend {
1616

17-
private final org.slf4j.Logger logger;
17+
private final ch.qos.logback.classic.Logger logger;
1818

19-
public LogbackLogBackend(org.slf4j.Logger logger) {
19+
public LogbackLogBackend(ch.qos.logback.classic.Logger logger) {
2020
this.logger = logger;
2121
}
2222

@@ -98,10 +98,10 @@ public void log(Level level, String msg, Throwable t) {
9898
*/
9999
public static class LogbackLogBackendFactory implements LogBackendFactory {
100100

101-
private final org.slf4j.ILoggerFactory factory;
101+
private final ch.qos.logback.classic.LoggerContext loggerContext;
102102

103103
public LogbackLogBackendFactory() {
104-
this.factory = org.slf4j.impl.StaticLoggerBinder.getSingleton().getLoggerFactory();
104+
this.loggerContext = new ch.qos.logback.classic.LoggerContext();
105105
}
106106

107107
@Override
@@ -112,7 +112,7 @@ public boolean isAvailable() {
112112

113113
@Override
114114
public LogBackend createLogBackend(String classLabel) {
115-
return new LogbackLogBackend(factory.getLogger(classLabel));
115+
return new LogbackLogBackend(loggerContext.getLogger(classLabel));
116116
}
117117
}
118118
}

src/main/java/com/j256/ormlite/logger/backend/NullLogBackend.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.j256.ormlite.logger.Level;
44
import com.j256.ormlite.logger.LogBackend;
55
import com.j256.ormlite.logger.LogBackendFactory;
6-
import com.j256.ormlite.logger.LoggerFactory;
76

87
/**
98
* Log backend that ignores all log requests.
@@ -36,7 +35,7 @@ public void log(Level level, String msg, Throwable throwable) {
3635

3736
/**
3837
* Factory for generating NullLogBackend instances. This can be used with the
39-
* {@link LoggerFactory#setLogBackendFactory(LogBackendFactory)} method to completely disable all logging.
38+
* LoggerFactory.setLogBackendFactory(LogBackendFactory) method to completely disable all logging.
4039
*/
4140
public static class NullLogBackendFactory implements LogBackendFactory {
4241

src/test/java/com/j256/ormlite/logger/FluentLoggerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public void testAppendMsgDocs() {
381381
}
382382

383383
@Test
384-
@Disabled
384+
@Disabled("Only to be run once and a while")
385385
public void testPerformance() {
386386
/*
387387
* Not really a fair test because hotswap might take out some of the method calls but I thought it would be

src/test/java/com/j256/ormlite/logger/LoggerFactoryTest.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public void testLogTypeKnownLog() {
8989
@Test
9090
public void testGetSimpleClassName() {
9191
String first = "foo";
92-
assertEquals(first, LoggerFactory.getSimpleClassName(first));
92+
assertEquals(first, extractSimpleClassName(first));
9393
String second = "bar";
9494
String className = first + "." + second;
95-
assertEquals(second, LoggerFactory.getSimpleClassName(className));
95+
assertEquals(second, extractSimpleClassName(className));
9696
className = first + ".";
97-
assertEquals(className, LoggerFactory.getSimpleClassName(className));
97+
assertEquals(className, extractSimpleClassName(className));
9898
}
9999

100100
@Test
@@ -263,6 +263,16 @@ public void testGlobalProperty() {
263263
}
264264
}
265265

266+
private String extractSimpleClassName(String className) {
267+
// get the last part of the class name
268+
int index = className.lastIndexOf('.');
269+
if (index < 0 || index == className.length() - 1) {
270+
return className;
271+
} else {
272+
return className.substring(index + 1);
273+
}
274+
}
275+
266276
public static class OurLogFactory implements LogBackendFactory {
267277

268278
LogBackend log;

0 commit comments

Comments
 (0)