Add listener functionality to LogWriter

This commit is contained in:
Mehdikhody 2024-02-10 05:21:16 +03:30
parent def2463988
commit 774a046673

View file

@ -6,11 +6,14 @@ import (
) )
func NewLogWriter() *LogWriter { func NewLogWriter() *LogWriter {
return &LogWriter{} return &LogWriter{
listeners: &[]func(line string){},
}
} }
type LogWriter struct { type LogWriter struct {
lastLine string lastLine string
listeners *[]func(line string)
} }
func (lw *LogWriter) Write(m []byte) (n int, err error) { func (lw *LogWriter) Write(m []byte) (n int, err error) {
@ -33,7 +36,8 @@ func (lw *LogWriter) Write(m []byte) (n int, err error) {
endIndex := strings.Index(messageBody, "]") endIndex := strings.Index(messageBody, "]")
if startIndex != -1 && endIndex != -1 && startIndex < endIndex { if startIndex != -1 && endIndex != -1 && startIndex < endIndex {
level := strings.TrimSpace(messageBody[startIndex+1 : endIndex]) level := strings.TrimSpace(messageBody[startIndex+1 : endIndex])
msgBody := "XRAY: " + strings.TrimSpace(messageBody[endIndex+1:]) rawMsg := strings.TrimSpace(messageBody[endIndex+1:])
msgBody := "XRAY: " + rawMsg
// Map the level to the appropriate logger function // Map the level to the appropriate logger function
switch level { switch level {
@ -48,6 +52,11 @@ func (lw *LogWriter) Write(m []byte) (n int, err error) {
default: default:
logger.Debug("XRAY: " + msg) logger.Debug("XRAY: " + msg)
} }
// Notify listeners of the message
for _, listener := range *lw.listeners {
listener(messageBody)
}
} else if msg != "" { } else if msg != "" {
logger.Debug("XRAY: " + msg) logger.Debug("XRAY: " + msg)
return len(m), nil return len(m), nil
@ -56,3 +65,11 @@ func (lw *LogWriter) Write(m []byte) (n int, err error) {
return len(m), nil return len(m), nil
} }
// SetListener adds a listener to the log writer
// The listener will be called with each line of the log
// that is written to the log writer
// We use this method to prevent reading the log file for better performance
func (lw *LogWriter) SetListener(listener func(line string)) {
*lw.listeners = append(*lw.listeners, listener)
}