Skip to content
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 syslog writer #507

Merged
merged 4 commits into from
Aug 18, 2023
Merged

Add syslog writer #507

merged 4 commits into from
Aug 18, 2023

Conversation

pkarlowicz
Copy link
Contributor

@pkarlowicz pkarlowicz commented Jul 29, 2023

Description

Linked issue:

#156

Linked website pull request, if the documention of https://tinylog.org/v2/ has to be updated:

tinylog-org/website#000

Definition of Done

  • I read contributing.md
  • There are no TODOs left in the code
  • Code style follows the tinylog standard
  • All classes and methods have Javadoc
  • Changes are covered by JUnit tests including edge cases, errors, and exception handling
  • Maven build works including compiling, tests, and checks (mvn verify)
  • Changes are committed by a verified email address that is assigned to the GitHub account (https://github.com/settings/emails)
  • Documentation on the tinylog website is up-to-date or updated by a pull request to the repository tinylog-org/website

Agreements

  • I agree that my changes will be published under the terms of the Apache License 2.0 (mandatory)
  • I agree that my GitHub user name will be published in the release notes (optional)

@codecov
Copy link

codecov bot commented Jul 29, 2023

Codecov Report

Merging #507 (109a52f) into v2.7 (512e053) will decrease coverage by 0.14%.
Report is 2 commits behind head on v2.7.
The diff coverage is 88.33%.

@@             Coverage Diff              @@
##               v2.7     #507      +/-   ##
============================================
- Coverage     94.46%   94.33%   -0.14%     
- Complexity     2799     2830      +31     
============================================
  Files           143      149       +6     
  Lines          5457     5577     +120     
  Branches        714      722       +8     
============================================
+ Hits           5155     5261     +106     
- Misses          164      176      +12     
- Partials        138      140       +2     
Files Changed Coverage Δ
.../java/org/tinylog/writers/raw/TcpSocketWriter.java 60.00% <60.00%> (ø)
.../java/org/tinylog/writers/raw/UdpSocketWriter.java 70.00% <70.00%> (ø)
...rc/main/java/org/tinylog/writers/SyslogWriter.java 71.42% <71.42%> (ø)
...n/java/org/tinylog/writers/raw/SyslogSeverity.java 90.47% <90.47%> (ø)
.../org/tinylog/writers/raw/AbstractSocketWriter.java 97.22% <97.22%> (ø)
...n/java/org/tinylog/writers/raw/SyslogFacility.java 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@pmwmedia
Copy link
Member

Thank you for your pull request! I will review it soon. However, your changes looks very good at first sight. :)

@@ -65,7 +65,7 @@ protected String getFileName() {
*
* @return Configured charset
*/
protected Charset getCharset() {
protected final Charset getCharset() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to make this method final. This could break other writers that are not part of tinylog itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


String protocol = getStringValue("protocol");
if (protocol == null) {
throw new IllegalArgumentException("Missing protocol");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (but not a must): What do you think about using the UdpSocketWriter or TcpSocketWriter by default if no protocol is configured instead of throwing an exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, used udp protocol as a default.

* @return The formated message.
*/
public byte[] formatMessage(final LogEntry logEntry) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@Override
public void write(final LogEntry logEntry) throws IOException {
byte[] b = formatMessage(logEntry);
socket.getOutputStream().write(b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a full name for the variable b like data for example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


@Override
public void write(final LogEntry logEntry) throws IOException {
byte[] b = formatMessage(logEntry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use here a full name for the variable b, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

import org.tinylog.Level;
import org.tinylog.provider.InternalLogger;

public class TcpSyslogServer extends Thread {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please start and end the body of the class with an empty line.

import org.tinylog.Level;
import org.tinylog.provider.InternalLogger;

public class UdpSyslogServer extends Thread {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please start and end the body of the class with an empty line, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

private String getExpectedSyslogMessage(final String message,
final SyslogFacility facility,
final SyslogSeverity severity,
final String identification) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the indentation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.WARNING, ""));
server.shutdown();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an empty line at the end of the class body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

/**
* Tests for {@link SyslogWriter}.
*/
public final class SyslogWriterTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two general wishes:

1st: If you split lines, please move the dot from the end of the line to the start of the next line.

Example:

assertThatThrownBy(() -> new SyslogWriter(doubletonMap("protocol", "udp", "facility", "invalid"))
		.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create())).hasMessageMatching("(?i).*SyslogFacility.*");

2nd: Please add some blank lines in longer test methods to group the logic. This makes it easier to read and unterstand the logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@pmwmedia
Copy link
Member

pmwmedia commented Aug 4, 2023

I'm done with the review. I could find only a few minors. You have written very good and maintainable code :)

@pmwmedia
Copy link
Member

pmwmedia commented Aug 4, 2023

By the way: Would you be able to add some documentation for the tinylog website?

Link:
https://github.com/tinylog-org/website/edit/v2/content/documentation/configuration.md

Copy link
Member

@pmwmedia pmwmedia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your changes. I could find only one minor. Can you add the missing empty line? Afterwards, I will merge your pull request.

builder.append(": ");
builder.append(render(logEntry));
return builder.toString().getBytes(charset);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an empty line between line 127 and 128.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@pmwmedia
Copy link
Member

Thank you very much for your great pull request! It's really awesome to have a syslog writer in tinylog.

@pmwmedia pmwmedia merged commit 1566c7f into tinylog-org:v2.7 Aug 18, 2023
2 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 26, 2023
@pmwmedia pmwmedia added this to the 2.7 milestone Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants