diff --git a/entry.go b/entry.go index 63e25583c..ab0800a64 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) @@ -264,6 +278,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...)) } @@ -310,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...)) } @@ -345,6 +368,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 +383,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...)) } 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) +}