Skip to content

Commit

Permalink
use logbacks own hostname resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Oct 8, 2023
1 parent c6aa530 commit 23c220d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 89 deletions.
10 changes: 3 additions & 7 deletions src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
package de.siegmar.logbackgelf;

import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.util.LevelToSyslogSeverity;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.encoder.EncoderBase;
import de.siegmar.logbackgelf.mappers.CallerDataFieldMapper;
import de.siegmar.logbackgelf.mappers.KeyValueFieldMapper;
Expand Down Expand Up @@ -324,12 +325,7 @@ private Object convertToNumberIfNeeded(final Object value) {
@Override
public void start() {
if (originHost == null || originHost.isBlank()) {
try {
originHost = InetUtil.getLocalHostName();
} catch (final UnknownHostException e) {
addWarn("Could not determine local hostname", e);
originHost = "unknown";
}
originHost = Optional.ofNullable(context.getProperty(CoreConstants.HOSTNAME_KEY)).orElse("unknown");
}
if (shortPatternLayout == null) {
shortPatternLayout = buildPattern(DEFAULT_SHORT_PATTERN);
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/de/siegmar/logbackgelf/InetUtil.java

This file was deleted.

9 changes: 5 additions & 4 deletions src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static java.time.Duration.ofMillis;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -500,13 +501,13 @@ void rewriteStaticField() {

@ParameterizedTest
@NullAndEmptySource
void originHostDefaultToLocalHostNameIfEmpty(final String configuredHostname) throws IOException {
void originHostDefaultToLocalHostNameIfEmpty(final String configuredHostname) {
encoder.setOriginHost(configuredHostname);
encoder.start();

final String localhost = InetUtil.getLocalHostName();

assertEquals(localhost, encoder.getOriginHost());
assertThat(encoder.getOriginHost())
.isNotBlank()
.isNotEqualTo("unknown");
}

@Test
Expand Down
35 changes: 0 additions & 35 deletions src/test/java/de/siegmar/logbackgelf/InetUtilTest.java

This file was deleted.

0 comments on commit 23c220d

Please sign in to comment.