|
| 1 | +/* |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign.slf4j; |
| 17 | + |
| 18 | +import feign.Feign; |
| 19 | +import feign.Logger; |
| 20 | +import feign.Request; |
| 21 | +import feign.RequestTemplate; |
| 22 | +import feign.Response; |
| 23 | +import feign.Util; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | +import org.testng.annotations.AfterMethod; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.io.FileReader; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.Collections; |
| 32 | + |
| 33 | +import static org.testng.Assert.assertEquals; |
| 34 | + |
| 35 | +public class Slf4jLoggerTest { |
| 36 | + private static final String CONFIG_KEY = "someMethod()"; |
| 37 | + private static final Request REQUEST = |
| 38 | + new RequestTemplate().method("GET").append("http://api.example.com").request(); |
| 39 | + private static final Response RESPONSE = |
| 40 | + Response.create(200, "OK", Collections.<String, Collection<String>>emptyMap(), new byte[0]); |
| 41 | + |
| 42 | + private File logFile; |
| 43 | + private Slf4jLogger logger; |
| 44 | + |
| 45 | + @AfterMethod |
| 46 | + void tearDown() throws Exception { |
| 47 | + SimpleLoggerUtil.resetToDefaults(); |
| 48 | + logFile.delete(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test public void useFeignLoggerByDefault() throws Exception { |
| 52 | + initializeSimpleLogger("debug"); |
| 53 | + logger = new Slf4jLogger(); |
| 54 | + logger.log(CONFIG_KEY, "This is my message"); |
| 55 | + assertLoggedMessages("DEBUG feign.Logger - [someMethod] This is my message\n"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test public void useLoggerByNameIfRequested() throws Exception { |
| 59 | + initializeSimpleLogger("debug"); |
| 60 | + logger = new Slf4jLogger("named.logger"); |
| 61 | + logger.log(CONFIG_KEY, "This is my message"); |
| 62 | + assertLoggedMessages("DEBUG named.logger - [someMethod] This is my message\n"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test public void useLoggerByClassIfRequested() throws Exception { |
| 66 | + initializeSimpleLogger("debug"); |
| 67 | + logger = new Slf4jLogger(Feign.class); |
| 68 | + logger.log(CONFIG_KEY, "This is my message"); |
| 69 | + assertLoggedMessages("DEBUG feign.Feign - [someMethod] This is my message\n"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test public void useSpecifiedLoggerIfRequested() throws Exception { |
| 73 | + initializeSimpleLogger("debug"); |
| 74 | + logger = new Slf4jLogger(LoggerFactory.getLogger("specified.logger")); |
| 75 | + logger.log(CONFIG_KEY, "This is my message"); |
| 76 | + assertLoggedMessages("DEBUG specified.logger - [someMethod] This is my message\n"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test public void logOnlyIfDebugEnabled() throws Exception { |
| 80 | + initializeSimpleLogger("info"); |
| 81 | + logger = new Slf4jLogger(); |
| 82 | + logger.log(CONFIG_KEY, "A message with %d formatting %s.", 2, "tokens"); |
| 83 | + logger.logRequest(CONFIG_KEY, Logger.Level.BASIC, REQUEST); |
| 84 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.BASIC, RESPONSE, 273); |
| 85 | + assertLoggedMessages(""); |
| 86 | + } |
| 87 | + |
| 88 | + @Test public void logRequestsAndResponses() throws Exception { |
| 89 | + initializeSimpleLogger("debug"); |
| 90 | + logger = new Slf4jLogger(); |
| 91 | + logger.log(CONFIG_KEY, "A message with %d formatting %s.", 2, "tokens"); |
| 92 | + logger.logRequest(CONFIG_KEY, Logger.Level.BASIC, REQUEST); |
| 93 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.BASIC, RESPONSE, 273); |
| 94 | + assertLoggedMessages( |
| 95 | + "DEBUG feign.Logger - [someMethod] A message with 2 formatting tokens.\n" + |
| 96 | + "DEBUG feign.Logger - [someMethod] ---> GET http://api.example.com HTTP/1.1\n" + |
| 97 | + "DEBUG feign.Logger - [someMethod] <--- HTTP/1.1 200 OK (273ms)\n" |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + private void initializeSimpleLogger(String logLevel) throws Exception { |
| 102 | + logFile = File.createTempFile(getClass().getName(), ".log"); |
| 103 | + SimpleLoggerUtil.initialize(logFile, logLevel); |
| 104 | + } |
| 105 | + |
| 106 | + private void assertLoggedMessages(String expectedMessages) throws Exception { |
| 107 | + assertEquals(Util.toString(new FileReader(logFile)), expectedMessages); |
| 108 | + } |
| 109 | +} |
0 commit comments