Commit 04b9db6c authored by Dmitry Smirnov's avatar Dmitry Smirnov

Merge tag 'upstream/0.4.1'

Upstream version 0.4.1
parents 989dec21 88586216
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
.idea
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
*.iml
# AUTHORS
- Boaz Shuster [ripcurld00d](https://github.com/ripcurld00d) @[ripcurld0](https://twitter.com/ripcurld0)
# Changelog
## 0.4
* Update the name of the package from `logrus_logstash` to `logrustash`
* Add TimeFormat to Hook
* Replace the old logrus package path: `github.com/Sirupsen/logrus` with `github.com/sirupsen/logrus`
## 0.3
* Fix the Logstash format to set `@version` to `"1"`
* Add unit-tests to logstash.go
* Remove the assert package
* Add prefix filtering
## Before that (major changes)
* Update LICENSE to MIT from GPL
......@@ -7,13 +7,13 @@ Use this hook to send the logs to [Logstash](https://www.elastic.co/products/log
package main
import (
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/bshuster-repo/logrus-logstash-hook"
)
func main() {
log := logrus.New()
hook, err := logrus_logstash.NewHook("tcp", "172.17.0.2:9999", "myappName")
hook, err := logrustash.NewHook("tcp", "172.17.0.2:9999", "myappName")
if err != nil {
log.Fatal(err)
......@@ -47,7 +47,7 @@ This can be done when creating the hook:
```go
hook, err := logrus_logstash.NewHookWithFields("tcp", "172.17.0.2:9999", "myappName", logrus.Fields{
hook, err := logrustash.NewHookWithFields("tcp", "172.17.0.2:9999", "myappName", logrus.Fields{
"hostname": os.Hostname(),
"serviceName": "myServiceName",
})
......@@ -83,7 +83,7 @@ For example if you don't want to see the hostname and serviceName on each log li
```go
hook, err := logrus_logstash.NewHookWithFields("tcp", "172.17.0.2:9999", "myappName", logrus.Fields{
hook, err := logrustash.NewHookWithFields("tcp", "172.17.0.2:9999", "myappName", logrus.Fields{
"_hostname": os.Hostname(),
"_serviceName": "myServiceName",
})
......@@ -93,3 +93,14 @@ hook.WithPrefix("_")
There are also constructors available which allow you to specify the prefix from the start.
The std-out will not have the '\_hostname' and '\_servicename' fields, and the logstash output will, but the prefix will be dropped from the name.
# Authors
Name | Github | Twitter |
------------ | --------- | ---------- |
Boaz Shuster | ripcurld0 | @ripcurld0 |
# License
MIT.
package logrus_logstash
package logrustash
import (
"net"
"strings"
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)
// Hook represents a connection to a Logstash instance
......@@ -13,6 +13,7 @@ type Hook struct {
appName string
alwaysSentFields logrus.Fields
hookOnlyPrefix string
TimeFormat string
}
// NewHook creates a new hook to a Logstash instance, which listens on
......@@ -106,6 +107,9 @@ func (h *Hook) Fire(entry *logrus.Entry) error {
}
formatter := LogstashFormatter{Type: h.appName}
if h.TimeFormat != "" {
formatter.TimestampFormat = h.TimeFormat
}
dataBytes, err := formatter.FormatWithPrefix(entry, h.hookOnlyPrefix)
if err != nil {
......
package logrus_logstash
package logrustash
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)
// Formatter generates json in logstash format.
......@@ -44,8 +45,7 @@ func (f *LogstashFormatter) FormatWithPrefix(entry *logrus.Entry, prefix string)
timeStampFormat := f.TimestampFormat
if timeStampFormat == "" {
//timeStampFormat = logrus.DefaultTimestampFormat
timeStampFormat = "2006-01-02 15:04:05.000"
timeStampFormat = time.RFC3339
}
fields["@timestamp"] = entry.Time.Format(timeStampFormat)
......
package logrus_logstash
package logrustash
import (
"bytes"
......@@ -7,7 +7,7 @@ import (
"net/url"
"testing"
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)
func TestLogstashFormatter(t *testing.T) {
......
package logrus_logstash
package logrustash
import (
"bytes"
......@@ -9,7 +9,7 @@ import (
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)
func TestLegostashHook(t *testing.T) {
......@@ -242,7 +242,7 @@ func TestFire(t *testing.T) {
t.Error(err)
}
expected := map[string]string{
"@timestamp": "0001-01-01 00:00:00.000",
"@timestamp": "0001-01-01T00:00:00Z",
"@version": "1",
"ignore": "haaa",
"level": "debug",
......@@ -297,3 +297,24 @@ func TestLevels(t *testing.T) {
}
}
func TestLogstashTimeStampFormat(t *testing.T) {
conn := ConnMock{buff: bytes.NewBufferString("")}
hook := &Hook{
conn: conn,
TimeFormat: time.Kitchen,
}
fTime := time.Date(2009, time.November, 10, 3, 4, 0, 0, time.UTC)
if err := hook.Fire(&logrus.Entry{Time: fTime}); err != nil {
t.Errorf("expected fire to not return error: %s", err)
}
var res map[string]string
if err := json.NewDecoder(conn.buff).Decode(&res); err != nil {
t.Error(err)
}
if value, ok := res["@timestamp"]; !ok {
t.Error("expected result to have '@timestamp'")
} else if value != "3:04AM" {
t.Errorf("expected time to be '%s' but got '%s'", "3:04AM", value)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment