Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/releases.yml
#	pom.xml
  • Loading branch information
Scarsz committed Jan 5, 2024
2 parents 817b2de + 6aadecd commit 1b0454e
Show file tree
Hide file tree
Showing 24 changed files with 784 additions and 133 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/releases.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/snapshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
env:
NEXUS_USER: ${{ secrets.NEXUS_USER }}
NEXUS_PASS: ${{ secrets.NEXUS_PASS }}
run: mvn --batch-mode --update-snapshots --settings deploy.xml clean deploy
run: mvn --batch-mode --update-snapshots --settings deploy.xml clean source:jar deploy
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ ChannelLoggingHandler handler = new ChannelLoggingHandler(() -> jda.getTextChann
</repository>
```
```xml
<!-- JDA 5 -->
<dependency>
<groupId>me.scarsz</groupId>
<artifactId>jdaappender</artifactId>
<version>1.0.2-SNAPSHOT</version>
<groupId>me.scarsz.jdaappender</groupId>
<artifactId>jda5</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>

or

<!-- JDA 4 -->
<dependency>
<groupId>me.scarsz.jdaappender</groupId>
<artifactId>jda4</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
```

Expand Down
62 changes: 62 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.scarsz.jdaappender</groupId>
<artifactId>parent</artifactId>
<version>1.2.0-SNAPSHOT</version>
</parent>

<artifactId>common</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>annotations</artifactId>
<version>9.0.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>

<!-- logging frameworks -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.31</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
<scope>provided</scope>
</dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.7</version>
<scope>provided</scope>
</dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta9</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.scarsz.jdaappender;

import java.util.concurrent.ScheduledFuture;

public interface IChannelLoggingHandler {

void enqueue(LogItem logItem);

void flush();

String escapeMarkdown(String message);

ScheduledFuture<?> getScheduledFuture();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.entities.Message;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -18,20 +17,23 @@
/**
* Represents a loggable message from the application
*/
@Getter
public class LogItem {

public static final int CLIPPING_MAX_LENGTH = Message.MAX_CONTENT_LENGTH - 20;
public static final int CLIPPING_MAX_LENGTH = 2000 - 20;

@Getter private final String logger;
@Getter private final long timestamp;
@Getter private final LogLevel level;
@Getter @Setter(AccessLevel.PACKAGE) @Nullable private String message;
@Getter @Nullable private final Throwable throwable;
private final IChannelLoggingHandler handler;
private final String logger;
private final long timestamp;
private final LogLevel level;
@Setter(AccessLevel.PACKAGE) @Nullable private String message;
@Nullable private final Throwable throwable;

public LogItem(String logger, LogLevel level, String message) {
this(logger, System.currentTimeMillis(), level, message, null);
public LogItem(IChannelLoggingHandler handler, String logger, LogLevel level, String message) {
this(handler, logger, System.currentTimeMillis(), level, message, null);
}
public LogItem(String logger, long timestamp, LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
public LogItem(IChannelLoggingHandler handler, String logger, long timestamp, LogLevel level, @Nullable String message, @Nullable Throwable throwable) {
this.handler = handler;
this.logger = logger;
this.timestamp = timestamp;
this.level = level;
Expand All @@ -48,7 +50,7 @@ protected String format(@NotNull HandlerConfig config) {
StringBuilder builder = new StringBuilder();

if (config.getPrefixer() != null) builder.append(config.getPrefixer().apply(this));
builder.append(message);
if (message != null) builder.append(config.isUseCodeBlocks() ? message.replace("```", "`\u200B`\u200B`\u200B") : handler.escapeMarkdown(message));
if (config.getSuffixer() != null) builder.append(config.getSuffixer().apply(this));
if (throwable != null) {
try (StringWriter stringWriter = new StringWriter()) {
Expand All @@ -66,7 +68,7 @@ protected String format(@NotNull HandlerConfig config) {
}

/**
* Clip the log item's message content if it exceeds {@link Message#MAX_CONTENT_LENGTH}
* Clip the log item's message content if it exceeds {@link LogItem#CLIPPING_MAX_LENGTH}
* @param config the appender config
* @return a new {@link LogItem} containing excess characters from this LogItem,
* null if no clipping was performed
Expand All @@ -77,7 +79,7 @@ protected LogItem clip(@NotNull HandlerConfig config) {
}
/**
* Clip the log item's message content into a maximum of specified number of log items, if it exceeds
* {@link Message#MAX_CONTENT_LENGTH}
* {@link LogItem#CLIPPING_MAX_LENGTH}
* @param config the appender config
* @param max the maximum amount of {@link LogItem}s to clip from this message
* @return a set containing {@link LogItem}s formed from excess characters in this LogItem,
Expand All @@ -89,15 +91,15 @@ protected Set<LogItem> clip(@NotNull HandlerConfig config, int max) {
LogItem bottom = this;
int formattingLength = config.getFormattingLength(bottom);
int i = 0;
while (message != null && message.length() > 0 && i < max && message.length() + formattingLength >= CLIPPING_MAX_LENGTH) {
while (message != null && !message.isEmpty() && i < max && message.length() + formattingLength >= CLIPPING_MAX_LENGTH) {
formattingLength = config.getFormattingLength(bottom);
int cutoff = CLIPPING_MAX_LENGTH - formattingLength;
int pulledCharacterCount = Math.min(cutoff, bottom.message.length());

String remaining = substring(bottom.message, pulledCharacterCount);
bottom.message = substring(bottom.message, 0, pulledCharacterCount);

if (remaining == null || remaining.length() == 0) break;
if (remaining == null || remaining.isEmpty()) break;
if (++i == max) break;

bottom = clone(remaining);
Expand All @@ -109,9 +111,9 @@ protected Set<LogItem> clip(@NotNull HandlerConfig config, int max) {
}

/**
* regex-powered aggressive stripping pattern, see https://regex101.com/r/RDcGRE for explanation
* strip ANSI escape codes
*/
private static final Pattern colorPattern = Pattern.compile("\u001B(?:\\[0?m|\\[38;2(?:;\\d{1,3}){3}m|\\[([0-9]{1,2}[;m]?){3})");
private static final Pattern colorPattern = Pattern.compile("\u001B\\[[\\d;]*m");
public static String stripColors(@NotNull String str) {
return colorPattern.matcher(str).replaceAll("");
}
Expand All @@ -132,7 +134,7 @@ public int getFormattedLength(HandlerConfig config) {
}

public LogItem clone(String message) {
return new LogItem(logger, timestamp, level, message, throwable);
return new LogItem(handler, logger, timestamp, level, message, throwable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.scarsz.jdaappender.adapter;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import me.scarsz.jdaappender.IChannelLoggingHandler;
import me.scarsz.jdaappender.LogItem;
import me.scarsz.jdaappender.LogLevel;

Expand All @@ -14,9 +14,9 @@

public class JavaLoggingAdapter extends Handler {

private final ChannelLoggingHandler handler;
private final IChannelLoggingHandler handler;

public JavaLoggingAdapter(ChannelLoggingHandler handler) {
public JavaLoggingAdapter(IChannelLoggingHandler handler) {
this.handler = handler;
}

Expand All @@ -30,6 +30,7 @@ public void publish(LogRecord record) {

if (level != null) {
handler.enqueue(new LogItem(
handler,
record.getLoggerName(),
record.getMillis(),
level,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.scarsz.jdaappender.adapter;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import me.scarsz.jdaappender.IChannelLoggingHandler;
import me.scarsz.jdaappender.LogItem;
import me.scarsz.jdaappender.LogLevel;
import org.apache.logging.log4j.Level;
Expand Down Expand Up @@ -42,9 +42,9 @@ public class Log4JLoggingAdapter extends AbstractAppender {
}
}

private final ChannelLoggingHandler handler;
private final IChannelLoggingHandler handler;

public Log4JLoggingAdapter(ChannelLoggingHandler handler) {
public Log4JLoggingAdapter(IChannelLoggingHandler handler) {
super("JDAAppender", null, PATTERN_LAYOUT, false);
this.handler = handler;
}
Expand All @@ -59,6 +59,7 @@ public void append(LogEvent event) {

if (level != null) {
handler.enqueue(new LogItem(
handler,
event.getLoggerName(),
LOG_EVENT_HAS_MILLIS ? event.getMillis() : System.currentTimeMillis(),
level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.AppenderBase;
import me.scarsz.jdaappender.ChannelLoggingHandler;
import me.scarsz.jdaappender.IChannelLoggingHandler;
import me.scarsz.jdaappender.LogItem;
import me.scarsz.jdaappender.LogLevel;

public class LogbackLoggingAdapter extends AppenderBase<ILoggingEvent> {

private final ChannelLoggingHandler handler;
private final IChannelLoggingHandler handler;

public LogbackLoggingAdapter(ChannelLoggingHandler handler, LoggerContext context) {
public LogbackLoggingAdapter(IChannelLoggingHandler handler, LoggerContext context) {
this.handler = handler;
setContext(context);
this.start();
Expand All @@ -29,6 +29,7 @@ protected void append(ILoggingEvent event) {

if (level != null) {
handler.enqueue(new LogItem(
handler,
event.getLoggerName(),
event.getTimeStamp(),
level,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.scarsz.jdaappender.adapter;

import lombok.Getter;
import me.scarsz.jdaappender.ChannelLoggingHandler;
import me.scarsz.jdaappender.IChannelLoggingHandler;
import me.scarsz.jdaappender.LogItem;
import me.scarsz.jdaappender.LogLevel;
import org.jetbrains.annotations.NotNull;
Expand All @@ -13,7 +13,7 @@ public class SystemLoggingAdapter {
@Getter private final LogStream outStream;
@Getter private final LogStream errStream;

public SystemLoggingAdapter(ChannelLoggingHandler handler) {
public SystemLoggingAdapter(IChannelLoggingHandler handler) {
this.outStream = new LogStream(System.out, "SOUT", LogLevel.INFO, handler);
this.errStream = new LogStream(System.err, "SERR", LogLevel.ERROR, handler);
}
Expand All @@ -22,9 +22,9 @@ static class LogStream extends PrintStream {

private final String loggerName;
private final LogLevel level;
private final ChannelLoggingHandler handler;
private final IChannelLoggingHandler handler;

public LogStream(PrintStream standardStream, String loggerName, LogLevel level, ChannelLoggingHandler handler) {
public LogStream(PrintStream standardStream, String loggerName, LogLevel level, IChannelLoggingHandler handler) {
super(standardStream, true);
this.loggerName = loggerName;
this.level = level;
Expand All @@ -40,7 +40,7 @@ public void println(Object o) {
@Override
public void println(String str) {
super.println(str);
handler.enqueue(new LogItem(loggerName, System.currentTimeMillis(), level, str, null));
handler.enqueue(new LogItem(handler, loggerName, System.currentTimeMillis(), level, str, null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import me.scarsz.jdaappender.ExtensionBuilder;
import net.dv8tion.jda.api.JDA;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package me.your.app;

import me.scarsz.jdaappender.ChannelLoggingHandler;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down
Loading

0 comments on commit 1b0454e

Please sign in to comment.