diff --git a/README.md b/README.md index 224f848..4cfb90a 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/formatters.go b/formatters.go index d712f79..c8ec73e 100644 --- a/formatters.go +++ b/formatters.go @@ -7,6 +7,9 @@ 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 @@ -14,12 +17,18 @@ type IFormatter interface { // 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 @@ -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(), ) diff --git a/formatters_test.go b/formatters_test.go index 9dfb133..af3cc82 100644 --- a/formatters_test.go +++ b/formatters_test.go @@ -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") }