fluent / fluent/fluent-logger-java

version 0.3.2 cause null sender from FluentLoggerFactory.getLogger

Aperta
#64 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
210
Fork
86
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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
}
}
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.