-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add support for Socket Logging Handler #23128
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea82928
logging socket
9e4910a
logging to socket
8370758
package corrected
dcd3805
logging into socket https://github.com/quarkusio/quarkus/issues/23127
2d9bb4d
Merge remote-tracking branch 'origin/main' into main
c5dd234
json support for all handlers not just for console
e2c68dd
Merge branch 'main' into main
JozefDropco 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 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 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 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
95 changes: 95 additions & 0 deletions
95
core/runtime/src/main/java/io/quarkus/runtime/logging/SocketConfig.java
This file contains 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,95 @@ | ||
package io.quarkus.runtime.logging; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
|
||
import org.jboss.logmanager.handlers.SocketHandler.Protocol; | ||
|
||
import io.quarkus.runtime.annotations.ConfigDocMapKey; | ||
import io.quarkus.runtime.annotations.ConfigDocSection; | ||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class SocketConfig { | ||
|
||
/** | ||
* If socket logging should be enabled | ||
*/ | ||
@ConfigItem | ||
boolean enable; | ||
|
||
/** | ||
* | ||
* The IP address and port of the server receiving the logs | ||
*/ | ||
@ConfigItem(defaultValue = "localhost:4560") | ||
InetSocketAddress endpoint; | ||
|
||
/** | ||
* Sets the protocol used to connect to the server | ||
*/ | ||
@ConfigItem(defaultValue = "tcp") | ||
Protocol protocol; | ||
|
||
/** | ||
* Enables or disables blocking when attempting to reconnect a | ||
* {@link Protocol#TCP | ||
* TCP} or {@link Protocol#SSL_TCP SSL TCP} protocol | ||
*/ | ||
@ConfigItem | ||
boolean blockOnReconnect; | ||
|
||
/** | ||
* The log message formatter. json or pattern is supported | ||
*/ | ||
@ConfigItem(defaultValue = "pattern") | ||
String formatter; | ||
|
||
/** | ||
* The log message format | ||
*/ | ||
@ConfigItem(defaultValue = "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n") | ||
String format; | ||
/** | ||
* The log level specifying, which message levels will be logged by socket logger | ||
*/ | ||
@ConfigItem(defaultValue = "ALL") | ||
Level level; | ||
/** | ||
* comma-seperated key=value pairs | ||
* possible keys are listed {@link org.jboss.logmanager.formatters.StructuredFormatter.Key} | ||
* e.g. HOST_NAME=host,LEVEL=severity | ||
*/ | ||
@ConfigItem(defaultValue = "<NA>") | ||
public String keyoverrides; | ||
/** | ||
* Post additional fields only for JSON formatted messages | ||
* You can add static fields to each log event in the following form: | ||
* | ||
* <pre> | ||
* quarkus.log.handler.socket.additional-field.field1.value=value1 | ||
* quarkus.log.handler.socket.additional-field.field1.type=String | ||
* </pre> | ||
*/ | ||
@ConfigItem | ||
@ConfigDocMapKey("field-name") | ||
@ConfigDocSection | ||
public Map<String, AdditionalFieldConfig> additionalField; | ||
|
||
/** | ||
* Syslog async logging config | ||
*/ | ||
AsyncConfig async; | ||
|
||
@ConfigGroup | ||
public class AdditionalFieldConfig { | ||
/** | ||
* Additional field value. | ||
*/ | ||
@ConfigItem | ||
public String value; | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
core/test-extension/deployment/src/main/resources/application-async-socket-output.properties
This file contains 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,11 @@ | ||
quarkus.log.level=INFO | ||
quarkus.log.socket.enable=true | ||
quarkus.log.socket.endpoint=localhost:5140 | ||
quarkus.log.socket.protocol=TCP | ||
quarkus.log.socket.formatter=json | ||
quarkus.log.socket.level=WARNING | ||
quarkus.log.socket.async=true | ||
quarkus.log.socket.async.queue-length=256 | ||
quarkus.log.socket.async.overflow=discard | ||
# Resource path to DSAPublicKey base64 encoded bytes | ||
quarkus.root.dsa-key-location=/DSAPublicKey.encoded |
8 changes: 8 additions & 0 deletions
8
core/test-extension/deployment/src/main/resources/application-socket-output.properties
This file contains 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,8 @@ | ||
quarkus.log.level=INFO | ||
quarkus.log.socket.enable=true | ||
quarkus.log.socket.endpoint=localhost:5140 | ||
quarkus.log.socket.protocol=TCP | ||
quarkus.log.socket.format=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n | ||
quarkus.log.socket.level=WARNING | ||
# Resource path to DSAPublicKey base64 encoded bytes | ||
quarkus.root.dsa-key-location=/DSAPublicKey.encoded |
51 changes: 51 additions & 0 deletions
51
core/test-extension/deployment/src/test/java/io/quarkus/logging/AsyncSocketHandlerTest.java
This file contains 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,51 @@ | ||
package io.quarkus.logging; | ||
|
||
import static io.quarkus.logging.LoggingTestsHelper.getHandler; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.logging.Handler; | ||
import java.util.logging.Level; | ||
|
||
import org.jboss.logmanager.formatters.JsonFormatter; | ||
import org.jboss.logmanager.formatters.PatternFormatter; | ||
import org.jboss.logmanager.handlers.AsyncHandler; | ||
import org.jboss.logmanager.handlers.SocketHandler; | ||
import org.jboss.logmanager.handlers.SyslogHandler; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AsyncSocketHandlerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("application-async-socket-output.properties") | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(LoggingTestsHelper.class) | ||
.addAsManifestResource("application.properties", "microprofile-config.properties")) | ||
.setLogFileName("AsyncSyslogHandlerTest.log"); | ||
|
||
@Test | ||
public void asyncSyslogHandlerConfigurationTest() throws NullPointerException { | ||
Handler handler = getHandler(AsyncHandler.class); | ||
assertThat(handler.getLevel()).isEqualTo(Level.WARNING); | ||
|
||
AsyncHandler asyncHandler = (AsyncHandler) handler; | ||
assertThat(asyncHandler.getHandlers()).isNotEmpty(); | ||
assertThat(asyncHandler.getQueueLength()).isEqualTo(256); | ||
assertThat(asyncHandler.getOverflowAction()).isEqualTo(AsyncHandler.OverflowAction.DISCARD); | ||
|
||
Handler nestedSyslogHandler = Arrays.stream(asyncHandler.getHandlers()) | ||
.filter(h -> (h instanceof SocketHandler)) | ||
.findFirst().get(); | ||
|
||
SocketHandler socketHandler = (SocketHandler) nestedSyslogHandler; | ||
assertThat(socketHandler.getPort()).isEqualTo(5140); | ||
assertThat(socketHandler.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); | ||
assertThat(socketHandler.getProtocol()).isEqualTo(SocketHandler.Protocol.TCP); | ||
assertThat(socketHandler.isBlockOnReconnect()).isEqualTo(false); | ||
assertThat(socketHandler.getFormatter()).isInstanceOf(JsonFormatter.class); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this added vs what SysLogConfig?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is added as the output where you are sending messages can require formatted output differently than the Pattern. In our case we have logstash which has input tcp and json_lines. We need custom attributes/fields to add to the json formatted log meesage. If you have some other suggestion how to achieve it - I am happy to modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gsmet WDYT about this? Does this use case warrant doing something different than what our SysLog handling does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I thought I will use logging-json extension however this extension is limited to console logging. Maybe it would be more beneficial to extend that support instead and keep socket logging clean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's a better approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HI @geoand, @gsmet ,
I have reworked the code completely. Now socket handler in core has just pattern and logging-json was extended to support all handlers not just console as it was before. Now file, console, syslog, socket and also named handlers can log as json. Previous implementation of consoleFormatter was opiniated and if JSON was added as extension basic console but also all named handlers were forced to log as JSON. I kept the original behavior but personally I am not fan of it. With "json=false" you can set console to use PatternFormatter and some named console handlers to use JSON. But you cannot use console as JSON and some named console handler to use Pattern. Its just because implementation should be backward compatible.
If users add logging-json extension
I changed integration test of logging-json as previous one was not doing anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @JozefDropco.
I'll have a look soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I somehow missed this PR but I wonder if the work I did in #25505 isn't somehow related.
I probably need to have another look at it.