Skip to content

Commit

Permalink
GH-985: Correct unexpected behavior when using JavaLogger (#1047)
Browse files Browse the repository at this point in the history
Fixes #985 

* JavaLogger(String name) added. Workaround for JavaLogger() provided
* JavaLogger() marked as deprecated. Workaround for JavaLogger() removed
* Little fix for note in README
* One more little fix for note in README
* JavaLogger(Class<?>) constructor added
  • Loading branch information
finnetrolle authored and kdavisk6 committed Aug 30, 2019
1 parent 964ae79 commit 976e2c1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,16 @@ public class Example {
public static void main(String[] args) {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.logger(new Logger.JavaLogger().appendToFile("logs/http.log"))
.logger(new Logger.JavaLogger("GitHub.Logger").appendToFile("logs/http.log"))
.logLevel(Logger.Level.FULL)
.target(GitHub.class, "https://api.github.com");
}
}
```

> **A Note on JavaLogger**:
> Avoid using of default ```JavaLogger()``` constructor - it was marked as deprecated and will be removed soon.
The SLF4JLogger (see above) may also be of interest.


Expand Down
41 changes: 39 additions & 2 deletions core/src/main/java/feign/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.FileHandler;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
Expand Down Expand Up @@ -164,8 +165,44 @@ protected void log(String configKey, String format, Object... args) {
*/
public static class JavaLogger extends Logger {

final java.util.logging.Logger logger =
java.util.logging.Logger.getLogger(Logger.class.getName());
final java.util.logging.Logger logger;

/**
* @deprecated Use {@link #JavaLogger(String)} or {@link #JavaLogger(Class)} instead.
*
* This constructor can be used to create just one logger. Example =
* {@code Logger.JavaLogger().appendToFile("logs/first.log")}
*
* If you create multiple loggers for multiple clients and provide different files
* to write log - you'll have unexpected behavior - all clients will write same log
* to each file.
*
* That's why this constructor will be removed in future.
*/
@Deprecated
public JavaLogger() {
logger = java.util.logging.Logger.getLogger(Logger.class.getName());
}

/**
* Constructor for JavaLogger class
*
* @param loggerName a name for the logger. This should be a dot-separated name and should
* normally be based on the package name or class name of the subsystem, such as java.net
* or javax.swing
*/
public JavaLogger(String loggerName) {
logger = java.util.logging.Logger.getLogger(loggerName);
}

/**
* Constructor for JavaLogger class
*
* @param clazz the returned logger will be named after clazz
*/
public JavaLogger(Class<?> clazz) {
logger = java.util.logging.Logger.getLogger(clazz.getName());
}

@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
Expand Down
55 changes: 55 additions & 0 deletions core/src/test/java/feign/MultipleLoggerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2012-2019 The Feign Authors
*
* Licensed 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.
*/
package feign;

import org.junit.Test;
import java.lang.reflect.Field;

public class MultipleLoggerTest {

private static java.util.logging.Logger getInnerLogger(Logger.JavaLogger logger)
throws Exception {
Field inner = logger.getClass().getDeclaredField("logger");
inner.setAccessible(true);
return (java.util.logging.Logger) inner.get(logger);
}

@Test
public void testAppendSeveralFilesToOneJavaLogger() throws Exception {
Logger.JavaLogger logger = new Logger.JavaLogger().appendToFile("1.log").appendToFile("2.log");
java.util.logging.Logger inner = getInnerLogger(logger);
assert (inner.getHandlers().length == 2);
}

@Test
public void testJavaLoggerInstantationWithLoggerName() throws Exception {
Logger.JavaLogger l1 = new Logger.JavaLogger("First client").appendToFile("1.log");
Logger.JavaLogger l2 = new Logger.JavaLogger("Second client").appendToFile("2.log");
java.util.logging.Logger logger1 = getInnerLogger(l1);
assert (logger1.getHandlers().length == 1);
java.util.logging.Logger logger2 = getInnerLogger(l2);
assert (logger2.getHandlers().length == 1);
}

@Test
public void testJavaLoggerInstantationWithClazz() throws Exception {
Logger.JavaLogger l1 = new Logger.JavaLogger(String.class).appendToFile("1.log");
Logger.JavaLogger l2 = new Logger.JavaLogger(Integer.class).appendToFile("2.log");
java.util.logging.Logger logger1 = getInnerLogger(l1);
assert (logger1.getHandlers().length == 1);
java.util.logging.Logger logger2 = getInnerLogger(l2);
assert (logger2.getHandlers().length == 1);
}

}

0 comments on commit 976e2c1

Please sign in to comment.