Skip to content

Commit

Permalink
Implement tinylog custom writers
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Dec 17, 2021
1 parent cc924e0 commit 3c4764a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@
requires com.fasterxml.jackson.datatype.jsr310;
requires net.harawata.appdirs;
requires applicationinsights.logging.log4j2;
requires org.tinylog.impl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import org.jabref.logic.util.OS;

import com.google.common.base.Throwables;
import org.slf4j.event.LoggingEvent;
import org.tinylog.core.LogEntry;

public class LogEventViewModel {

private final LoggingEvent logEvent;
private final LogEntry logEvent;

public LogEventViewModel(LoggingEvent logEvent) {
public LogEventViewModel(LogEntry logEvent) {
this.logEvent = Objects.requireNonNull(logEvent);
}

Expand Down Expand Up @@ -47,7 +47,7 @@ public JabRefIcon getIcon() {
}

public Optional<String> getStackTrace() {
return Optional.ofNullable(logEvent.getThrowable()).map(Throwables::getStackTraceAsString);
return Optional.ofNullable(logEvent.getException()).map(Throwables::getStackTraceAsString);
}

public String getDetailedText() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package org.jabref.gui.logging;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;

import org.jabref.gui.Globals;
import org.jabref.logic.logging.LogMessages;

import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry;
import com.microsoft.applicationinsights.telemetry.Telemetry;
import com.microsoft.applicationinsights.telemetry.TraceTelemetry;
import org.slf4j.event.LoggingEvent;
import org.tinylog.core.LogEntry;
import org.tinylog.core.LogEntryValue;
import org.tinylog.writers.AbstractFormatPatternWriter;

public class ApplicationInsightsAppender extends AbstractFormatPatternWriter {

public class ApplicationInsightsAppender {
public ApplicationInsightsAppender(final Map<String, String> properties) {
super(properties);
}

@Override
public Collection<LogEntryValue> getRequiredLogEntryValues() {
// TODO Auto-generated method stub
return EnumSet.allOf(LogEntryValue.class);
}

/**
* The log event will be forwarded to the {@link LogMessages} archive.
*/
public void append(LoggingEvent rawEvent) {
ApplicationInsightsLogEvent event = new ApplicationInsightsLogEvent(rawEvent);
@Override
public void write(LogEntry logEntry) throws Exception {
ApplicationInsightsLogEvent event = new ApplicationInsightsLogEvent(logEntry);

Telemetry telemetry;
if (event.isException()) {
ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException());
exceptionTelemetry.getProperties().put("Message", rawEvent.getMessage());
exceptionTelemetry.getProperties().put("Message", logEntry.getMessage());
exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel());
telemetry = exceptionTelemetry;
} else {
Expand All @@ -31,4 +44,13 @@ public void append(LoggingEvent rawEvent) {

Globals.getTelemetryClient().ifPresent(client -> client.track(telemetry));
}

@Override
public void flush() throws Exception {
}

@Override
public void close() throws Exception {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,35 @@
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.telemetry.SeverityLevel;
import org.slf4j.MDC;
import org.slf4j.event.Level;
import org.slf4j.event.LoggingEvent;
import org.tinylog.core.LogEntry;

// TODO: Remove this copy as soon as the one included in AI is compatible with log4j 3
public final class ApplicationInsightsLogEvent extends ApplicationInsightsEvent {

private final LoggingEvent logEvent;
private final LogEntry logEvent;

public ApplicationInsightsLogEvent(LoggingEvent logEvent) {
public ApplicationInsightsLogEvent(LogEntry logEvent) {
this.logEvent = logEvent;
}

@Override
public String getMessage() {
String message = this.logEvent.getMessage() != null ? this.logEvent.getMessage() : "Log4j Trace";
String message = this.logEvent.getMessage() != null ? this.logEvent.getMessage() : "Tinylog Trace";

return message;
}

@Override
public boolean isException() {
return this.logEvent.getThrowable() != null;
return this.logEvent.getException() != null;
}

@Override
public Exception getException() {
Exception exception = null;

if (isException()) {
Throwable throwable = this.logEvent.getThrowable();
Throwable throwable = this.logEvent.getException();
exception = throwable instanceof Exception ? (Exception) throwable : new Exception(throwable);
}

Expand All @@ -72,16 +71,16 @@ public Map<String, String> getCustomParameters() {

metaData.put("SourceType", "slf4j");

addLogEventProperty("LoggerName", logEvent.getLoggerName(), metaData);
// addLogEventProperty("LoggerName", logEvent.get metaData);
addLogEventProperty("LoggingLevel", logEvent.getLevel() != null ? logEvent.getLevel().name() : null, metaData);
addLogEventProperty("ThreadName", logEvent.getThreadName(), metaData);
addLogEventProperty("TimeStamp", getFormattedDate(logEvent.getTimeStamp()), metaData);
addLogEventProperty("ThreadName", logEvent.getThread().getName(), metaData);
addLogEventProperty("TimeStamp", getFormattedDate(logEvent.getTimestamp().toInstant().toEpochMilli()), metaData);

if (isException()) {
addLogEventProperty("Logger Message", getMessage(), metaData);
}

for (StackTraceElement stackTraceElement : logEvent.getThrowable().getStackTrace()) {
for (StackTraceElement stackTraceElement : logEvent.getException().getStackTrace()) {

addLogEventProperty("ClassName", stackTraceElement.getClassName(), metaData);
addLogEventProperty("FileName", stackTraceElement.getFileName(), metaData);
Expand All @@ -101,23 +100,19 @@ public Map<String, String> getCustomParameters() {

@Override
public SeverityLevel getNormalizedSeverityLevel() {
Level logEventLevel = logEvent.getLevel();
org.tinylog.Level logEventLevel = logEvent.getLevel();

switch (logEventLevel) {

case ERROR:
return SeverityLevel.Error;

case WARN:
return SeverityLevel.Warning;

case INFO:
return SeverityLevel.Information;

case TRACE:
case DEBUG:
return SeverityLevel.Verbose;

default:
InternalLogger.INSTANCE.error("Unknown slf4joption, %d, using TRACE level as default", logEventLevel);
return SeverityLevel.Verbose;
Expand Down
69 changes: 17 additions & 52 deletions src/main/java/org/jabref/gui/logging/GuiAppender.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,38 @@
package org.jabref.gui.logging;

import java.util.Locale;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;

import org.tinylog.Level;
import org.tinylog.format.AdvancedMessageFormatter;
import org.tinylog.format.MessageFormatter;
import org.tinylog.provider.ContextProvider;
import org.tinylog.provider.LoggingProvider;
import org.tinylog.provider.NopContextProvider;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.logging.LogMessages;

public class GuiAppender implements LoggingProvider {
import org.tinylog.core.LogEntry;
import org.tinylog.core.LogEntryValue;
import org.tinylog.writers.AbstractFormatPatternWriter;

@Override
public ContextProvider getContextProvider() {
return new NopContextProvider();
}

@Override
public Level getMinimumLevel() {
return Level.INFO;
}
public class GuiAppender extends AbstractFormatPatternWriter {

@Override
public Level getMinimumLevel(String tag) {
return Level.INFO;
public GuiAppender(final Map<String, String> properties) {
super(properties);
}

@Override
public boolean isEnabled(int depth, String tag, Level level) {
return level.ordinal() >= Level.INFO.ordinal();
public Collection<LogEntryValue> getRequiredLogEntryValues() {
return EnumSet.allOf(LogEntryValue.class);
}

@Override
public void shutdown() {
// Nothing to do
}

private void log(Throwable exception, String message, Object[] arguments) {
StringBuilder builder = new StringBuilder();
if (message != null) {
builder.append(new AdvancedMessageFormatter(Locale.ENGLISH, true).format(message, arguments));
}
if (exception != null) {
if (builder.length() > 0) {
builder.append(": ");
}
builder.append(exception);
}
System.out.println(builder);
public void write(LogEntry logEntry) throws Exception {
DefaultTaskExecutor.runInJavaFXThread(() -> LogMessages.getInstance().add(logEntry));
}

@Override
public void log(int depth, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) {
// TODO Auto-generated method stub

public void flush() throws Exception {
}

@Override
public void log(String loggerClassName, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) {
// TODO Auto-generated method stub

public void close() throws Exception {
}

/*
The log event will be forwarded to the {@link LogMessages} archive.
public void append(LoggingEvent event) {
// We need to make a copy as instances of LogEvent are reused by log4j
DefaultTaskExecutor.runInJavaFXThread(() -> LogMessages.getInstance().add(copy));
}
*/
}
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/logic/logging/LogMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.slf4j.event.LoggingEvent;
import org.tinylog.core.LogEntry;

/**
* This class is used for storing and archiving all message output of JabRef as log events.
Expand All @@ -13,7 +13,7 @@ public class LogMessages {

private static LogMessages instance = new LogMessages();

private final ObservableList<LoggingEvent> messages = FXCollections.observableArrayList();
private final ObservableList<LogEntry> messages = FXCollections.observableArrayList();

private LogMessages() {
}
Expand All @@ -22,11 +22,11 @@ public static LogMessages getInstance() {
return instance;
}

public ObservableList<LoggingEvent> getMessages() {
public ObservableList<LogEntry> getMessages() {
return FXCollections.unmodifiableObservableList(messages);
}

public void add(LoggingEvent event) {
public void add(LogEntry event) {
messages.add(event);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.jabreg.gui.logging.ApplicationInsightsAppender
org.jabref.gui.logging.GuiAppender
23 changes: 0 additions & 23 deletions src/main/resources/log4j2.xml

This file was deleted.

0 comments on commit 3c4764a

Please sign in to comment.