-
Notifications
You must be signed in to change notification settings - Fork 1
Capture application logs #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
itssimon
merged 6 commits into
main
from
simon/dev-9-add-application-log-capture-to-java-sdk
Jan 22, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package io.apitally.common; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import ch.qos.logback.classic.Logger; | ||
| import ch.qos.logback.classic.LoggerContext; | ||
| import ch.qos.logback.classic.spi.ILoggingEvent; | ||
| import ch.qos.logback.core.AppenderBase; | ||
|
|
||
| import io.apitally.common.dto.LogRecord; | ||
|
|
||
| public class ApitallyAppender extends AppenderBase<ILoggingEvent> { | ||
| private static final String NAME = "ApitallyAppender"; | ||
| private static final int MAX_BUFFER_SIZE = 1000; | ||
| private static final int MAX_MESSAGE_LENGTH = 2048; | ||
|
|
||
| private static final ThreadLocal<List<LogRecord>> logBuffer = new ThreadLocal<>(); | ||
|
|
||
| public static synchronized void register() { | ||
| if (!(LoggerFactory.getILoggerFactory() instanceof LoggerContext loggerContext)) { | ||
| return; | ||
| } | ||
| Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); | ||
|
|
||
| if (rootLogger.getAppender(NAME) != null) { | ||
| return; | ||
| } | ||
|
|
||
| ApitallyAppender appender = new ApitallyAppender(); | ||
| appender.setContext(loggerContext); | ||
| appender.setName(NAME); | ||
| appender.start(); | ||
| rootLogger.addAppender(appender); | ||
| } | ||
|
|
||
| public static void startCapture() { | ||
| logBuffer.set(new ArrayList<>()); | ||
| } | ||
|
|
||
| public static List<LogRecord> endCapture() { | ||
| List<LogRecord> logs = logBuffer.get(); | ||
| logBuffer.remove(); | ||
| return logs; | ||
| } | ||
|
|
||
| @Override | ||
| protected void append(ILoggingEvent event) { | ||
| List<LogRecord> buffer = logBuffer.get(); | ||
| if (buffer == null || buffer.size() >= MAX_BUFFER_SIZE) { | ||
| return; | ||
| } | ||
|
|
||
| double timestamp = event.getTimeStamp() / 1000.0; | ||
| String loggerName = event.getLoggerName(); | ||
| String level = event.getLevel().toString(); | ||
| String message = truncateMessage(event.getFormattedMessage()); | ||
|
|
||
| buffer.add(new LogRecord(timestamp, loggerName, level, message)); | ||
| } | ||
|
|
||
| private static String truncateMessage(String message) { | ||
| if (message == null) { | ||
| return null; | ||
| } | ||
| if (message.length() <= MAX_MESSAGE_LENGTH) { | ||
| return message; | ||
| } | ||
| String suffix = "... (truncated)"; | ||
| return message.substring(0, MAX_MESSAGE_LENGTH - suffix.length()) + suffix; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package io.apitally.common.dto; | ||
|
|
||
| public class LogRecord { | ||
| private final double timestamp; | ||
| private final String logger; | ||
| private final String level; | ||
| private final String message; | ||
|
|
||
| public LogRecord(double timestamp, String logger, String level, String message) { | ||
| this.timestamp = timestamp; | ||
| this.logger = logger; | ||
| this.level = level; | ||
| this.message = message; | ||
| } | ||
|
|
||
| public double getTimestamp() { | ||
| return timestamp; | ||
| } | ||
|
|
||
| public String getLogger() { | ||
| return logger; | ||
| } | ||
|
|
||
| public String getLevel() { | ||
| return level; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.