Update logrus vendor
closes #2125 Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
parent
76f514b618
commit
1d6c1a6468
25 changed files with 681 additions and 197 deletions
21
vendor/github.com/bshuster-repo/logrus-logstash-hook/LICENSE
generated
vendored
Normal file
21
vendor/github.com/bshuster-repo/logrus-logstash-hook/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Boaz Shuster
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
129
vendor/github.com/bshuster-repo/logrus-logstash-hook/logstash.go
generated
vendored
Normal file
129
vendor/github.com/bshuster-repo/logrus-logstash-hook/logstash.go
generated
vendored
Normal file
|
@ -0,0 +1,129 @@
|
|||
package logrus_logstash
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Hook represents a connection to a Logstash instance
|
||||
type Hook struct {
|
||||
conn net.Conn
|
||||
appName string
|
||||
alwaysSentFields logrus.Fields
|
||||
hookOnlyPrefix string
|
||||
}
|
||||
|
||||
// NewHook creates a new hook to a Logstash instance, which listens on
|
||||
// `protocol`://`address`.
|
||||
func NewHook(protocol, address, appName string) (*Hook, error) {
|
||||
return NewHookWithFields(protocol, address, appName, make(logrus.Fields))
|
||||
}
|
||||
|
||||
// NewHookWithConn creates a new hook to a Logstash instance, using the supplied connection
|
||||
func NewHookWithConn(conn net.Conn, appName string) (*Hook, error) {
|
||||
return NewHookWithFieldsAndConn(conn, appName, make(logrus.Fields))
|
||||
}
|
||||
|
||||
// NewHookWithFields creates a new hook to a Logstash instance, which listens on
|
||||
// `protocol`://`address`. alwaysSentFields will be sent with every log entry.
|
||||
func NewHookWithFields(protocol, address, appName string, alwaysSentFields logrus.Fields) (*Hook, error) {
|
||||
return NewHookWithFieldsAndPrefix(protocol, address, appName, alwaysSentFields, "")
|
||||
}
|
||||
|
||||
// NewHookWithFieldsAndPrefix creates a new hook to a Logstash instance, which listens on
|
||||
// `protocol`://`address`. alwaysSentFields will be sent with every log entry. prefix is used to select fields to filter
|
||||
func NewHookWithFieldsAndPrefix(protocol, address, appName string, alwaysSentFields logrus.Fields, prefix string) (*Hook, error) {
|
||||
conn, err := net.Dial(protocol, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewHookWithFieldsAndConnAndPrefix(conn, appName, alwaysSentFields, prefix)
|
||||
}
|
||||
|
||||
// NewHookWithFieldsAndConn creates a new hook to a Logstash instance using the supplied connection
|
||||
func NewHookWithFieldsAndConn(conn net.Conn, appName string, alwaysSentFields logrus.Fields) (*Hook, error) {
|
||||
return NewHookWithFieldsAndConnAndPrefix(conn, appName, alwaysSentFields, "")
|
||||
}
|
||||
|
||||
//NewHookWithFieldsAndConnAndPrefix creates a new hook to a Logstash instance using the suppolied connection and prefix
|
||||
func NewHookWithFieldsAndConnAndPrefix(conn net.Conn, appName string, alwaysSentFields logrus.Fields, prefix string) (*Hook, error) {
|
||||
return &Hook{conn: conn, appName: appName, alwaysSentFields: alwaysSentFields, hookOnlyPrefix: prefix}, nil
|
||||
}
|
||||
|
||||
//NewFilterHook makes a new hook which does not forward to logstash, but simply enforces the prefix rules
|
||||
func NewFilterHook() *Hook {
|
||||
return NewFilterHookWithPrefix("")
|
||||
}
|
||||
|
||||
//NewFilterHookWithPrefix make a new hook which does not forward to logstash, but simply enforces the specified prefix
|
||||
func NewFilterHookWithPrefix(prefix string) *Hook {
|
||||
return &Hook{conn: nil, appName: "", alwaysSentFields: make(logrus.Fields), hookOnlyPrefix: prefix}
|
||||
}
|
||||
|
||||
func (h *Hook) filterHookOnly(entry *logrus.Entry) {
|
||||
if h.hookOnlyPrefix != "" {
|
||||
for key := range entry.Data {
|
||||
if strings.HasPrefix(key, h.hookOnlyPrefix) {
|
||||
delete(entry.Data, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//WithPrefix sets a prefix filter to use in all subsequent logging
|
||||
func (h *Hook) WithPrefix(prefix string) {
|
||||
h.hookOnlyPrefix = prefix
|
||||
}
|
||||
|
||||
func (h *Hook) WithField(key string, value interface{}) {
|
||||
h.alwaysSentFields[key] = value
|
||||
}
|
||||
|
||||
func (h *Hook) WithFields(fields logrus.Fields) {
|
||||
//Add all the new fields to the 'alwaysSentFields', possibly overwriting exising fields
|
||||
for key, value := range fields {
|
||||
h.alwaysSentFields[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hook) Fire(entry *logrus.Entry) error {
|
||||
//make sure we always clear the hookonly fields from the entry
|
||||
defer h.filterHookOnly(entry)
|
||||
|
||||
// Add in the alwaysSentFields. We don't override fields that are already set.
|
||||
for k, v := range h.alwaysSentFields {
|
||||
if _, inMap := entry.Data[k]; !inMap {
|
||||
entry.Data[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
//For a filteringHook, stop here
|
||||
if h.conn == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
formatter := LogstashFormatter{Type: h.appName}
|
||||
|
||||
dataBytes, err := formatter.FormatWithPrefix(entry, h.hookOnlyPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = h.conn.Write(dataBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Hook) Levels() []logrus.Level {
|
||||
return []logrus.Level{
|
||||
logrus.PanicLevel,
|
||||
logrus.FatalLevel,
|
||||
logrus.ErrorLevel,
|
||||
logrus.WarnLevel,
|
||||
logrus.InfoLevel,
|
||||
logrus.DebugLevel,
|
||||
}
|
||||
}
|
81
vendor/github.com/bshuster-repo/logrus-logstash-hook/logstash_formatter.go
generated
vendored
Normal file
81
vendor/github.com/bshuster-repo/logrus-logstash-hook/logstash_formatter.go
generated
vendored
Normal file
|
@ -0,0 +1,81 @@
|
|||
package logrus_logstash
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Formatter generates json in logstash format.
|
||||
// Logstash site: http://logstash.net/
|
||||
type LogstashFormatter struct {
|
||||
Type string // if not empty use for logstash type field.
|
||||
|
||||
// TimestampFormat sets the format used for timestamps.
|
||||
TimestampFormat string
|
||||
}
|
||||
|
||||
func (f *LogstashFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
return f.FormatWithPrefix(entry, "")
|
||||
}
|
||||
|
||||
func (f *LogstashFormatter) FormatWithPrefix(entry *logrus.Entry, prefix string) ([]byte, error) {
|
||||
fields := make(logrus.Fields)
|
||||
for k, v := range entry.Data {
|
||||
//remvove the prefix when sending the fields to logstash
|
||||
if prefix != "" && strings.HasPrefix(k, prefix) {
|
||||
k = strings.TrimPrefix(k, prefix)
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
case error:
|
||||
// Otherwise errors are ignored by `encoding/json`
|
||||
// https://github.com/Sirupsen/logrus/issues/377
|
||||
fields[k] = v.Error()
|
||||
default:
|
||||
fields[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
fields["@version"] = "1"
|
||||
|
||||
timeStampFormat := f.TimestampFormat
|
||||
|
||||
if timeStampFormat == "" {
|
||||
//timeStampFormat = logrus.DefaultTimestampFormat
|
||||
timeStampFormat = "2006-01-02 15:04:05.000"
|
||||
}
|
||||
|
||||
fields["@timestamp"] = entry.Time.Format(timeStampFormat)
|
||||
|
||||
// set message field
|
||||
v, ok := entry.Data["message"]
|
||||
if ok {
|
||||
fields["fields.message"] = v
|
||||
}
|
||||
fields["message"] = entry.Message
|
||||
|
||||
// set level field
|
||||
v, ok = entry.Data["level"]
|
||||
if ok {
|
||||
fields["fields.level"] = v
|
||||
}
|
||||
fields["level"] = entry.Level.String()
|
||||
|
||||
// set type field
|
||||
if f.Type != "" {
|
||||
v, ok = entry.Data["type"]
|
||||
if ok {
|
||||
fields["fields.type"] = v
|
||||
}
|
||||
fields["type"] = f.Type
|
||||
}
|
||||
|
||||
serialized, err := json.Marshal(fields)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
||||
}
|
||||
return append(serialized, '\n'), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue