From 183a5ec12e2eeb0efa72ad4ab17be9844666f9bb Mon Sep 17 00:00:00 2001 From: Shubham Sinha Date: Wed, 2 Oct 2019 14:07:12 +0530 Subject: [PATCH 1/4] added checks for logger not being attached to the entry type --- entry.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/entry.go b/entry.go index 63e25583c..bc29b6e19 100644 --- a/entry.go +++ b/entry.go @@ -3,6 +3,7 @@ package logrus import ( "bytes" "context" + "errors" "fmt" "os" "reflect" @@ -30,6 +31,10 @@ const ( knownLogrusFrames int = 4 ) +var ( + loggerNotAttached = errors.New("entry not attached to any logger") +) + func init() { bufferPool = &sync.Pool{ New: func() interface{} { @@ -88,6 +93,10 @@ func NewEntry(logger *Logger) *Entry { // Returns the string representation from the reader and ultimately the // formatter. func (entry *Entry) String() (string, error) { + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + return "", loggerNotAttached + } serialized, err := entry.Logger.Formatter.Format(entry) if err != nil { return "", err @@ -241,6 +250,11 @@ func (entry Entry) log(level Level, msg string) { } func (entry *Entry) fireHooks() { + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } entry.Logger.mu.Lock() defer entry.Logger.mu.Unlock() err := entry.Logger.Hooks.Fire(entry.Level, entry) @@ -250,6 +264,11 @@ func (entry *Entry) fireHooks() { } func (entry *Entry) write() { + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } entry.Logger.mu.Lock() defer entry.Logger.mu.Unlock() serialized, err := entry.Logger.Formatter.Format(entry) @@ -345,6 +364,11 @@ func (entry *Entry) Errorf(format string, args ...interface{}) { func (entry *Entry) Fatalf(format string, args ...interface{}) { entry.Logf(FatalLevel, format, args...) + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } entry.Logger.Exit(1) } @@ -355,6 +379,11 @@ func (entry *Entry) Panicf(format string, args ...interface{}) { // Entry Println family functions func (entry *Entry) Logln(level Level, args ...interface{}) { + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } if entry.Logger.IsLevelEnabled(level) { entry.Log(level, entry.sprintlnn(args...)) } From 2433a5a461c4e94b44338a0212f7e548950a3f4d Mon Sep 17 00:00:00 2001 From: Shubham Sinha Date: Wed, 2 Oct 2019 15:20:52 +0530 Subject: [PATCH 2/4] added checks for logger not being attached to the entry type --- entry.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/entry.go b/entry.go index bc29b6e19..4200031fb 100644 --- a/entry.go +++ b/entry.go @@ -283,6 +283,11 @@ func (entry *Entry) write() { } func (entry *Entry) Log(level Level, args ...interface{}) { + // add a check for logger being nil because entry is exposed + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } if entry.Logger.IsLevelEnabled(level) { entry.log(level, fmt.Sprint(args...)) } From 50f9d42676deace5be55a8fe026f3fca6f1a3a62 Mon Sep 17 00:00:00 2001 From: Shubham Sinha Date: Wed, 9 Oct 2019 22:35:33 +0530 Subject: [PATCH 3/4] added test cases for entry logger not attached --- entry.go | 5 ----- entry_test.go | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/entry.go b/entry.go index 4200031fb..6de30e475 100644 --- a/entry.go +++ b/entry.go @@ -264,11 +264,6 @@ func (entry *Entry) fireHooks() { } func (entry *Entry) write() { - // add a check for logger being nil because entry is exposed - if entry.Logger == nil { - fmt.Fprintf(os.Stderr, "Logger not attached\n") - return - } entry.Logger.mu.Lock() defer entry.Logger.mu.Unlock() serialized, err := entry.Logger.Formatter.Format(entry) diff --git a/entry_test.go b/entry_test.go index f764085ef..b538ecca2 100644 --- a/entry_test.go +++ b/entry_test.go @@ -166,4 +166,20 @@ func TestEntryLogfLevel(t *testing.T) { entry.Logf(WarnLevel, "%s", "warn") assert.Contains(t, buffer.String(), "warn", ) -} \ No newline at end of file +} + +func TestEntryLoggerNotAttached(t *testing.T) { + assert := assert.New(t) + + e := Entry{} + + _, err := e.String() + assert.NotNil(err) + + e.fireHooks() + + e.Log(InfoLevel) + e.Logf(InfoLevel, "") + e.Fatalf("") + e.Logln(InfoLevel) +} From abc679f8456b1f33baaa89a9a3cf13ea44080798 Mon Sep 17 00:00:00 2001 From: Shubham Sinha Date: Wed, 9 Oct 2019 22:40:27 +0530 Subject: [PATCH 4/4] added checks for logger not being attached to the entry type --- entry.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/entry.go b/entry.go index 6de30e475..ab0800a64 100644 --- a/entry.go +++ b/entry.go @@ -329,6 +329,10 @@ func (entry *Entry) Panic(args ...interface{}) { // Entry Printf family functions func (entry *Entry) Logf(level Level, format string, args ...interface{}) { + if entry.Logger == nil { + fmt.Fprintf(os.Stderr, "Logger not attached\n") + return + } if entry.Logger.IsLevelEnabled(level) { entry.Log(level, fmt.Sprintf(format, args...)) }