From 85ecae301482469a686b41a6a1a530526bd1dc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D0=B7=D0=B0=D1=80=D1=8F=D0=BD=20=D0=9D=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=D1=82=D0=B0=20=D0=93=D0=B0=D1=80=D0=B8=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Thu, 10 Jan 2019 14:29:01 +0300 Subject: [PATCH] Add filename to log --- go.mod | 12 +++++++----- go.sum | 26 ++++++++++++++++++++++++++ logger.go | 7 +++++-- 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index ce60324..3d8e2c3 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,10 @@ -module "github.com/lelvisl/logger" +module github.com/lelvisl/logger require ( - "github.com/bshuster-repo/logrus-logstash-hook" v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce - "github.com/sirupsen/logrus" v1.0.5 - "golang.org/x/crypto" v0.0.0-20180322175230-88942b9c40a4 - "golang.org/x/sys" v0.0.0-20180326154331-13d03a9a82fb + github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce + github.com/onrik/logrus v0.2.1 + github.com/sirupsen/logrus v1.3.0 + github.com/stretchr/testify v1.3.0 // indirect + golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc + golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb ) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b299fde --- /dev/null +++ b/go.sum @@ -0,0 +1,26 @@ +github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce h1:GH0okCzXIZ3kJQr+D7X1/c4jEK51m1eW7nc63cRE6Yo= +github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/onrik/logrus v0.2.1 h1:xEYR+opLvr+hNixPPAimuQppFYHaZ0XLO9hZ2G8WPLI= +github.com/onrik/logrus v0.2.1/go.mod h1:qfe9NeZVAJfIxviw3cYkZo3kvBtLoPRJriAO8zl7qTk= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.0.5 h1:8c8b5uO0zS4X6RPl/sd1ENwSkIc0/H2PaHxE3udaE8I= +github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= +github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= +github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20180322175230-88942b9c40a4 h1:AJCW0rhPjFKEAoValWpqnRKxX8YV0Xvqfw+dOexCTPc= +golang.org/x/crypto v0.0.0-20180322175230-88942b9c40a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M= +golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/sys v0.0.0-20180326154331-13d03a9a82fb h1:w1eDrzbtlRsu1SyjnZZFUBVuhRN2Rn6DlrHCx9s4ud0= +golang.org/x/sys v0.0.0-20180326154331-13d03a9a82fb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb h1:1w588/yEchbPNpa9sEvOcMZYbWHedwJjg4VOAdDHWHk= +golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/logger.go b/logger.go index 762d7be..11580f6 100644 --- a/logger.go +++ b/logger.go @@ -2,9 +2,9 @@ package logger import ( "context" - "os" - + "github.com/onrik/logrus/filename" log "github.com/sirupsen/logrus" + "os" ) //LogConfig is a configuration for logger @@ -63,6 +63,9 @@ func CreateLogger(config LogConfig) *log.Logger { func initLogger(config LogConfig) *log.Logger { logger := log.New() + filenameHook := filename.NewHook() + filenameHook.Field = "source" // Customize source field name + logger.AddHook(filenameHook) switch config.Type { case "syslog": logger = initSyslogger(config)