Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ewohltman
Copy link
Contributor

Added support for displaying timestamps according to a timezone locale. This change should be fully backwards compatible with existing implementations of this library.

The Opts struct has a new field, TimestampLocale:

type Opts struct {
	// Original fields remain unaltered
	// TimestampLocale specifies a custom locale for the timestamp
	TimestampLocale *time.Location
}

In addition, the parseToJson method was updated to support the Opts struct change:

func (hook *Hook) parseToJson(entry *logrus.Entry) (*[]byte, error) {
	// Beginning unchanged

	// Add footer to embed
	if !hook.Opts.DisableTimestamp {
		if hook.Opts.TimestampLocale != nil {
			entry.Time = entry.Time.In(hook.Opts.TimestampLocale)
		}
	// Remainder unchanged
	}

	// Remainder unchanged
}

@ewohltman
Copy link
Contributor Author

@kz Accepting pull requests for your library? 😄

@kz
Copy link
Owner

kz commented Dec 23, 2017

Hi @ewohltman - many thanks for your PR, and for the very detailed explanation. I definitely am accepting PRs. :) I'll take a look tomorrow and get back to you then!

@ewohltman
Copy link
Contributor Author

Real world usage of your library (well, the fork for this pull request) in the wild: https://github.com/ewohltman/ephemeral-roles/blob/master/pkg/logging/logging.go

@kz
Copy link
Owner

kz commented Dec 24, 2017

Glad to see that the library is being used in the wild! I'll leave a thorough review shortly.

Copy link
Owner

@kz kz left a comment

Choose a reason for hiding this comment

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

@ewohltman - I've reviewed the code. discordrus.go looks good, however a few changes need to be made in README.md and discordrus_test.go. Thanks for your help so far!

Also, once you've implemented all changes, please can you squash your commits with the message Add support for timestamp timezone locale (#2)?

README.md Outdated
# discordrus | a [Discord](https://discordapp.com/) hook for [Logrus](https://github.com/Sirupsen/logrus) <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Travis CI](https://api.travis-ci.org/kz/discordrus.svg?branch=master)](https://travis-ci.org/kz/discordrus) [![GoDoc](https://godoc.org/github.com/puddingfactory/logentrus?status.svg)](https://godoc.org/github.com/kz/discordrus)

**Current version:** v1.0.1
**Current version:** v1.0.2
Copy link
Owner

Choose a reason for hiding this comment

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

Please can you change this to v1.1.0? Your commit has added new functionality, so semver dictates a minor version increase!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

README.md Outdated
logrus.SetOutput(os.Stderr)
logrus.SetLevel(logrus.DebugLevel)

// Log timestamps in UTC
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think we'll need 37-55 as there should be a simpler way of approaching this (detailed below). Additionally, the example usage should be as straightforward as possible so most people can get an understanding of the API, so if there is additional usage to be documented, a ### H3 header could be used beneath the default usage.

If you want TimestampFormat to include the time zone at the end, could you test out your code using "Jan 2 15:04:05.00000 MST"? According to https://golang.org/pkg/time/#Time.String, MST would be replaced with the time zone during formatting, which would remove the need for L37-55 entirely.

Also, in general, if you do want to retrieve the time zone token, I believe there is a much simpler way of doing this using the Zone function. You can find a demonstration here: https://play.golang.org/p/OHKTfqqnwrR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great tip! Maybe there is something special about "MST"? I too saw that in other documentation, however at the time I was testing using "UTC" so maybe that wasn't being parsed how I expected? Explains why I would have to explicitly get the time zone string myself and append it to the pattern since it wasn't being replaced.

Copy link
Owner

Choose a reason for hiding this comment

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

I had the same confusion around the same time I was writing discordrus. Basically, every token in the format (Jan, 2, 15, 04, 05, MST) actually have meanings behind them (month, date of month without trailing zero, hour, minute, seconds, time zone respectively). You'd think you could replace Jan with Feb, but this wouldn't work since the point of hardcoding the meanings to values is to prevent ambiguity (it wouldn't be very straightforward getting Go to parse 15:15:15000, for example).

The significance of the values chosen are explained well here:

README.md Outdated
DisableTimestamp: false, // Setting this to true will disable timestamps from appearing in the footer
TimestampFormat: "Jan 2 15:04:05.00000", // The timestamp takes this format; if it is unset, it will take logrus' default format
EnableCustomColors: true, // If set to true, the below CustomLevelColors will apply
Username: "",
Copy link
Owner

Choose a reason for hiding this comment

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

Please be sure to keep the values from the old Opts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

README.md Outdated
Fatal: 13631488,
},
DisableInlineFields: false, // If set to true, fields will not appear in columns ("inline")
DisableTimestamp: false, // Setting this to true will disable timestamps from appearing in the footer
Copy link
Owner

Choose a reason for hiding this comment

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

Can we keep the old ordering (although TimestampLocale should still be directly after TimestampFormat)? It's more likely for time-related details to be changed than level colours, so time-related fields should take precedent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

README.md Outdated
DisableInlineFields: false, // If set to true, fields will not appear in columns ("inline")
DisableTimestamp: false, // Setting this to true will disable timestamps from appearing in the footer
TimestampFormat: timeStampFormat, // The timestamp takes this format; if it is unset, it will take logrus' default format
TimestampLocale: nil, // nil == time.Local, time.UTC, time.LoadLocation("America/New_York"), etc
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer the comment // The timestamp uses this locale; if it is unset, it will use time.Local. By convention all usage has been left until later in the README.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

README.md Outdated
Username | Replaces the default username of the webhook bot for the sent message only | Username unchanged | Any non-empty string (2-32 chars. inclusive)
Author | Adds an author field to the header if set | Author not set | Any non-empty string (1-256 chars inclusive)
DisableInlineFields | Inline means whether Discord will display the field in a column (with maximum three columns to a row). Setting this to `true` will cause Discord to display the field in its own row. | false | bool
DisableInlineFields | Inline means whether Discord will display the field in a column (with maximum three columns to a row). Setting this to `true` will cause Discord to display the field in its own row. | false | bool
Copy link
Owner

Choose a reason for hiding this comment

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

As specified above, the previous ordering should be kept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

README.md Outdated
"os"
"github.com/Sirupsen/logrus"
"os"
"strings"
Copy link
Owner

Choose a reason for hiding this comment

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

Having implemented the changes below, do make sure to remove unused imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Author: "", // Setting this to a non-empty string adds the author text to the message header
DisableTimestamp: false, // Setting this to true will disable timestamps from appearing in the footer
TimestampFormat: "Jan 2 15:04:05.00000", // The timestamp takes this format; if it is unset, it will take logrus' default format
TimestampLocale: time.UTC, // The timestamp takes it's timezone from the provided locale
Copy link
Owner

Choose a reason for hiding this comment

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

Previously, TimestampLocale was not specified so the entry's time would default to time.Local, so we should use time.Local instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ewohltman
Copy link
Contributor Author

ewohltman commented Dec 24, 2017

@kz I believe all suggestions have been addressed, squashed, and pushed - thanks for the great feedback!

@ewohltman
Copy link
Contributor Author

FYI: go test ./... did return successfully. I also updated my other project to depend on this updated fork and it's working as well!

@kz kz added the enhancement label Dec 24, 2017
@kz
Copy link
Owner

kz commented Dec 24, 2017

Everything looks good to me! I'll merge now.

@kz kz merged commit 233680a into kz:master Dec 24, 2017
@kz
Copy link
Owner

kz commented Dec 24, 2017

All tests passing! 🚀

Thanks very much for your PR. It's quite a useful feature to have!

Screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants