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

Readme and time layout customization #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

[![Build Status](https://travis-ci.org/go-st/epilog.svg?branch=master)](https://travis-ci.org/go-st/epilog)

Logger inspired by Monolog.
Epilog is a simple, fast and reliable logger for GO.

Look for usage examples in *example_test.go*
Epilog natively supports base `ILogger` interface from package `github.com/go-st/logger`.
So, if you use `ILogger` interface in your application -
migration to Epilog will be very easy.

Inspired by Monolog.

## Install
Using go get:
`go get github.com/go-st/epilog`

Using [glide](https://github.com/masterminds/glide):
`glide get github.com/go-st/epilog`

Checkout latest version on [release page](https://github.com/go-st/epilog/releases).

## Usage

```
logger := New("MyLogger", DefaultHandler)
logger.Debug("hello debug")
logger.Info("hello info")

// Output:
// [2017-01-18T16:00:00.000000+00:00] (DEBUG) hello debug
// [2017-01-18T16:00:00.000000+00:00] (INFO) hello info
```

Extended usage examples see in [example_test.go](https://github.com/go-st/epilog/blob/master/example_test.go)
15 changes: 12 additions & 3 deletions formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import (
"strings"
)

// timeLayout is a layout for time formatting being used by default
const defaultTimeLayout = "2006-01-02T15:04:05.000000-07:00"

// IFormatter formatters convert entries to []byte and used by handlers
type IFormatter interface {
Format(entry *Entry) []byte
}

// TextFormatter simple formatter
type TextFormatter struct {
format string
format string
timeLayout string
}

// NewTextFormatter creates new TextFormatter
func NewTextFormatter(format string) *TextFormatter {
return &TextFormatter{format: format}
return &TextFormatter{format: format, timeLayout: defaultTimeLayout}
}

// NewTextFormatterWithTimeLayout creates new TextFormatter with custom time layout
func NewTextFormatterWithTimeLayout(format string, timeLayout string) *TextFormatter {
return &TextFormatter{format: format, timeLayout: timeLayout}
}

// Format formats Entry
Expand All @@ -36,7 +45,7 @@ func (f *TextFormatter) Format(entry *Entry) []byte {
replaces = append(
replaces,
":level:", entry.Level.String(),
":time:", entry.Time.UTC().Format("2006-01-02T15:04:05.000000-07:00"),
":time:", entry.Time.UTC().Format(f.timeLayout),
":message:", entry.Message,
":additional:", additionalBuf.String(),
)
Expand Down
11 changes: 11 additions & 0 deletions formatters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,16 @@ func (s *FormatterTestSuite) TestFormat(c *C) {
result := formatter.Format(entry)

c.Assert(string(result), Equals, "[2015-09-17T16:00:00.000000+00:00] (bar) hello const\n")
}

func (s *FormatterTestSuite) TestFormatWithTimeLayout(c *C) {
formatter := NewTextFormatterWithTimeLayout("[:time:] :message:", time.RFC3339)

entryTime, _ := time.Parse("2006-01-02T15:04:05", "2015-09-17T16:00:00")

entry := NewEntry(logger.LevelDebug, entryTime, "hello world")
entry.Fields["foo"] = "bar"
result := formatter.Format(entry)

c.Assert(string(result), Equals, "[2015-09-17T16:00:00Z] hello world\n")
}