fluent / fluent/fluent-logger-java

version 0.3.2 cause null sender from FluentLoggerFactory.getLogger

Đang mở
#64 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Java
Star
210
Fork
86
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.