Skip to content

Commit

Permalink
Added logger version to name
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-martin committed Oct 31, 2014
1 parent b86eec3 commit fb16a8a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
21 changes: 8 additions & 13 deletions src/main/java/com/stackify/api/common/ApiClients.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package com.stackify.api.common;

import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
Expand All @@ -40,19 +38,16 @@ public class ApiClients {
* @param defaultClientName Default client name
* @return The client name
*/
public static String getApiClient(final String fileName, final String defaultClientName) {
public static String getApiClient(final Class<?> apiClass, final String fileName, final String defaultClientName) {

FileReader fileReader = null;
InputStream propertiesStream = null;

try {
URL fileUrl = ApiConfigurations.class.getResource(fileName);
File file = new File(fileUrl.toURI());

if (file.exists()) {
fileReader = new FileReader(file);
propertiesStream = apiClass.getResourceAsStream(fileName);

if (propertiesStream != null) {
Properties props = new Properties();
props.load(fileReader);
props.load(propertiesStream);

String name = (String) props.get("api-client.name");
String version = (String) props.get("api-client.version");
Expand All @@ -62,9 +57,9 @@ public static String getApiClient(final String fileName, final String defaultCli
} catch (Throwable t) {
LOGGER.error("Exception reading {} configuration file", fileName, t);
} finally {
if (fileReader != null) {
if (propertiesStream != null) {
try {
fileReader.close();
propertiesStream.close();
} catch (Throwable t) {
LOGGER.info("Exception closing {} configuration file", fileName, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static synchronized void startup() {
try {
CONFIG = ApiConfigurations.fromProperties();

String clientName = ApiClients.getApiClient("/stackify-api-common.properties", "stackify-api-common");
String clientName = ApiClients.getApiClient(LogManager.class, "/stackify-api-common.properties", "stackify-api-common");

LOG_APPENDER = new LogAppender<LogEvent>(clientName, new LogEventAdapter(CONFIG.getEnvDetail()));
LOG_APPENDER.activate(CONFIG);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/stackify/api/common/ApiClientsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ApiClientsTest {
*/
@Test
public void testGetApiClient() {
String apiClient = ApiClients.getApiClient("/stackify-api-common.properties", "stackify-api-common");
String apiClient = ApiClients.getApiClient(ApiClients.class, "/stackify-api-common.properties", "stackify-api-common");
Assert.assertEquals("name-version", apiClient);
}

Expand All @@ -49,7 +49,7 @@ public void testGetApiClient() {
*/
@Test
public void testGetApiClientWithoutProperties() {
String apiClient = ApiClients.getApiClient("/does-not-exist.properties", "stackify-api-common");
String apiClient = ApiClients.getApiClient(ApiClients.class, "/does-not-exist.properties", "stackify-api-common");
Assert.assertEquals("stackify-api-common", apiClient);
}

Expand All @@ -63,7 +63,7 @@ public void testGetApiClientWithException() throws Exception {
PowerMockito.doThrow(new IOException()).when(properties).load(Mockito.any(FileReader.class));
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(properties);

String apiClient = ApiClients.getApiClient("/does-not-exist.properties", "stackify-api-common");
String apiClient = ApiClients.getApiClient(ApiClients.class, "/does-not-exist.properties", "stackify-api-common");
Assert.assertEquals("stackify-api-common", apiClient);
}
}

0 comments on commit fb16a8a

Please sign in to comment.