fluent / fluent/fluent-logger-java

version 0.3.2 cause null sender from FluentLoggerFactory.getLogger

Abierto
#64 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Java
Estrellas
210
Forks
86
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I have read some issues before about the reason to use a **WeakHashMap** in **FluentLoggerFactory**. And the WeakHashMap was changed from **WeakHashMap** (version 0.3.1) to **WeakHashMap**(current version 0.3.2).

But I got a FluentLogger with null sender in version 0.3.2 when using **logback** in **Spring Boot** application.

The sender could not be connecting to fluent server for long time. So the sender should be assigned to null after calling FluentLogger.close().

But in my situation, how can I get a work-well logger after Spring Boot application has been luanched.

```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class FluentLoggerFactory {

private final Map loggers;

public FluentLoggerFactory() {
loggers = new WeakHashMap();
}

public synchronized FluentLogger getLogger(String tagPrefix, String host, int port, int timeout, int bufferCapacity,
Reconnector reconnector) {
String key = String.format("%s_%s_%d_%d_%d", new Object[] { tagPrefix, host, port, timeout, bufferCapacity });

for (Map.Entry entry : loggers.entrySet()) {
if (entry.getValue().equals(key)) {
FluentLogger found = entry.getKey();
if(found != null) {
return found; // the found contains a null valued sender
}
break;
}
}

Sender sender = null;
Properties props = System.getProperties();
if (!props.containsKey(Config.FLUENT_SENDER_CLASS)) {
// create default sender object
sender = new RawSocketSender(host, port, timeout, bufferCapacity, reconnector);
} else {
String senderClassName = props.getProperty(Config.FLUENT_SENDER_CLASS);
try {
sender = createSenderInstance(senderClassName, new Object[] { host, port, timeout, bufferCapacity });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
FluentLogger logger = new FluentLogger(tagPrefix, sender);
loggers.put(logger, key);
return logger;
}
...
...
}
```

```java
@SpringBootApplication
public class Application {

private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
LOGGER.info("hello world);
}

}
```

```xml





DEBUG


${CONSOLE_LOG_PATTERN}
utf8


debug
logback
localhost
24224


999




```

```java
public class FluentLogger {

public boolean log(String tag, Map data, long timestamp) {
String concatTag = null;
if (tagPrefix == null || tagPrefix.length() == 0) {
concatTag = tag;
}
else {
concatTag = tagPrefix + "." + tag;
}

if (timestamp != 0) {
return sender.emit(concatTag, timestamp, data);
} else {
return sender.emit(concatTag, data);
}
}

public void flush() {
sender.flush();
}

public void close() {
if (sender != null) {
sender.flush();
sender.close();
sender = null; // set sender to null after closing FluentLogger
}
}
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.