-
Notifications
You must be signed in to change notification settings - Fork 73
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
Fix #37 - Fix RFC 3164 date padding: use
for padding single digit day of month
#38
Open
cyrille-leclerc
wants to merge
1
commit into
jenkinsci:master
Choose a base branch
from
cyrille-leclerc:fix-rfc-3164-date-padding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
101 changes: 101 additions & 0 deletions
101
src/main/java/com/cloudbees/syslog/util/ConcurrentRfc3164DateFormat.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,101 @@ | ||
package com.cloudbees.syslog.util; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
/** | ||
* https://tools.ietf.org/html/rfc3164#section-4.1.2 | ||
* <pre> | ||
* The TIMESTAMP field is the local time and is in the format of "Mmm dd | ||
* hh:mm:ss" (without the quote marks) where: | ||
* | ||
* Mmm is the English language abbreviation for the month of the | ||
* year with the first character in uppercase and the other two | ||
* characters in lowercase. The following are the only acceptable | ||
* values: | ||
* | ||
* Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec | ||
* | ||
* dd is the day of the month. If the day of the month is less | ||
* than 10, then it MUST be represented as a space and then the | ||
* number. For example, the 7th day of August would be | ||
* represented as "Aug 7", with two spaces between the "g" and | ||
* the "7". | ||
* | ||
* hh:mm:ss is the local time. The hour (hh) is represented in a | ||
* 24-hour format. Valid entries are between 00 and 23, | ||
* inclusive. The minute (mm) and second (ss) entries are between | ||
* 00 and 59 inclusive. | ||
* </pre> | ||
*/ | ||
public class ConcurrentRfc3164DateFormat { | ||
private final BlockingQueue<SimpleDateFormat> monthDateFormats; | ||
private final BlockingQueue<SimpleDateFormat> timeDateFormats; | ||
private final Locale locale; | ||
private final TimeZone timeZone; | ||
|
||
/** | ||
* @param locale the locale whose date pattern symbols should be used | ||
* @param timeZone the timezone used by the underlying calendar | ||
* @param maxCacheSize | ||
* @throws NullPointerException if the given pattern or locale is null | ||
* @throws IllegalArgumentException if the given pattern is invalid | ||
*/ | ||
public ConcurrentRfc3164DateFormat(Locale locale, TimeZone timeZone, int maxCacheSize) { | ||
this.monthDateFormats = new LinkedBlockingDeque<>(maxCacheSize); | ||
this.timeDateFormats = new LinkedBlockingDeque<>(maxCacheSize); | ||
this.locale = locale; | ||
this.timeZone = timeZone; | ||
} | ||
|
||
/** | ||
* Formats a Date into a date/time string. | ||
* | ||
* @param date the time value to be formatted into a time string. | ||
* @return the formatted time string. | ||
*/ | ||
@Nonnull | ||
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") | ||
public String format(@Nonnull Date date) { | ||
|
||
|
||
// MONTH | ||
|
||
String month; | ||
|
||
|
||
// DAY OF MONTH | ||
GregorianCalendar calendar = new GregorianCalendar(); | ||
calendar.setTime(date); | ||
|
||
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); | ||
String dayofMonthPadding = dayOfMonth < 10 ? " " : ""; | ||
|
||
// TIME | ||
SimpleDateFormat timeDateFormat = timeDateFormats.poll(); | ||
if (timeDateFormat == null) { | ||
timeDateFormat = new SimpleDateFormat("HH:mm:ss", locale); | ||
timeDateFormat.setTimeZone(timeZone); | ||
} | ||
String time; | ||
try { | ||
time = timeDateFormat.format(date); | ||
} finally { | ||
timeDateFormats.offer(timeDateFormat); | ||
} | ||
|
||
String result = month + ' ' + dayofMonthPadding + dayOfMonth + ' ' + time; | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ConcurrentRfc3164DateFormat"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,18 +33,23 @@ public class TcpSyslogMessageSenderTest { | |
@Test | ||
public void send() throws Exception { | ||
TcpSyslogMessageSender messageSender = new TcpSyslogMessageSender(); | ||
messageSender.setDefaultMessageHostname("mysecretkey"); | ||
// messageSender.setDefaultMessageHostname("mysecretkey"); | ||
messageSender.setDefaultAppName("myapp"); | ||
messageSender.setDefaultFacility(Facility.USER); | ||
messageSender.setDefaultSeverity(Severity.INFORMATIONAL); | ||
messageSender.setSyslogServerHostname("logs2.papertrailapp.com"); | ||
messageSender.setSyslogServerPort(46022); | ||
|
||
// messageSender.setSyslogServerHostname("logs2.papertrailapp.com"); | ||
// messageSender.setSyslogServerPort(46022); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment |
||
// messageSender.setSsl(true); | ||
|
||
messageSender.setSyslogServerHostname("localhost"); | ||
messageSender.setSyslogServerPort(9000); | ||
|
||
messageSender.setMessageFormat(MessageFormat.RFC_3164); | ||
messageSender.setSsl(true); | ||
messageSender.sendMessage("unit test message over tcp éèà " + getClass() + " - " + new Timestamp(System.currentTimeMillis())); | ||
} | ||
|
||
@Ignore | ||
// @Ignore | ||
@Test | ||
public void send2() throws Exception { | ||
|
||
|
@@ -57,10 +62,15 @@ public void send2() throws Exception { | |
.withTimestamp(System.currentTimeMillis()); | ||
|
||
TcpSyslogMessageSender messageSender = new TcpSyslogMessageSender(); | ||
messageSender.setSyslogServerHostname("logs2.papertrailapp.com"); | ||
messageSender.setSyslogServerPort(46022); | ||
|
||
// messageSender.setSyslogServerHostname("logs2.papertrailapp.com"); | ||
// messageSender.setSyslogServerPort(46022); | ||
// messageSender.setSsl(true); | ||
|
||
messageSender.setSyslogServerHostname("localhost"); | ||
messageSender.setSyslogServerPort(9000); | ||
|
||
messageSender.setMessageFormat(MessageFormat.RFC_3164); | ||
messageSender.setSsl(true); | ||
|
||
System.out.println(msg.toSyslogMessage(messageSender.getMessageFormat())); | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
src/test/java/com/cloudbees/syslog/util/ConcurrentRfc3164DateFormatTest.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,47 @@ | ||
package com.cloudbees.syslog.util; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
|
||
public class ConcurrentRfc3164DateFormatTest { | ||
|
||
@Test | ||
public void test_format_date_with_single_digit_day_of_month(){ | ||
TimeZone timeZone = TimeZone.getTimeZone("UTC"); | ||
Calendar cal = Calendar.getInstance(); | ||
cal.setTimeZone(timeZone); | ||
cal.set(2013, Calendar.AUGUST, 7, 10, 30, 5); | ||
cal.set(Calendar.MILLISECOND, 0); | ||
|
||
Date singleDigitDayOfMonthDate = cal.getTime(); | ||
|
||
ConcurrentRfc3164DateFormat rfc3164DateFormat = new ConcurrentRfc3164DateFormat(Locale.US, timeZone, 50); | ||
|
||
String actual = rfc3164DateFormat.format(singleDigitDayOfMonthDate); | ||
|
||
Assert.assertThat(actual, Matchers.is("Aug 7 10:30:05")); | ||
} | ||
|
||
@Test | ||
public void test_format_date_with_double_digit_day_of_month(){ | ||
TimeZone timeZone = TimeZone.getTimeZone("UTC"); | ||
Calendar cal = Calendar.getInstance(); | ||
cal.setTimeZone(timeZone); | ||
cal.set(2013, Calendar.AUGUST, 17, 10, 30, 5); | ||
cal.set(Calendar.MILLISECOND, 0); | ||
|
||
Date singleDigitDayOfMonthDate = cal.getTime(); | ||
|
||
ConcurrentRfc3164DateFormat rfc3164DateFormat = new ConcurrentRfc3164DateFormat(Locale.US, timeZone, 50); | ||
|
||
String actual = rfc3164DateFormat.format(singleDigitDayOfMonthDate); | ||
|
||
Assert.assertThat(actual, Matchers.is("Aug 17 10:30:05")); | ||
} | ||
} |
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.
remove comment