Skip to content

Commit

Permalink
pass the name of the logfile up to the frontend so it can distinguish…
Browse files Browse the repository at this point in the history
… the log lines into log files
  • Loading branch information
jdrews committed Aug 7, 2022
1 parent 14b185c commit 51a638d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ var (
)

type LogMessage struct {
Text string `json:"text"`
Color string `json:"color"`
Text string `json:"text"`
Color string `json:"color"`
LogFile string `json:"logfile"`
}

type CompiledRegexColors struct {
Expand Down Expand Up @@ -185,12 +186,12 @@ func wshandler(c echo.Context, pubSub *pubsub.PubSub) error {
return nil
}

func follow(path string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors) {
func follow(logFilePath string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors) {
logger := logrus.New()
//logger.Level = logrus.DebugLevel
logger.SetOutput(os.Stdout)

parsedGlob, err := glob.Parse(path)
parsedGlob, err := glob.Parse(logFilePath)
if err != nil {
panic(fmt.Sprintf("%q: failed to parse glob: %q", parsedGlob, err))

Expand All @@ -201,7 +202,7 @@ func follow(path string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors)
select {
case line := <-tailer.Lines():
logger.Debug(line.Line)
logMessage := colorize(line.Line, patterns)
logMessage := colorize(line.Line, logFilePath, patterns)
pubSub.Pub(logMessage, "lines")
default:
continue
Expand All @@ -211,13 +212,13 @@ func follow(path string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors)

// Run each line through the regex patterns to determine if the line should be colored.
// Outputs a LogMessage with line color information
func colorize(line string, patterns []CompiledRegexColors) LogMessage {
func colorize(line string, logFile string, patterns []CompiledRegexColors) LogMessage {
var lineColor = ""
for _, element := range patterns {
if element.regex.MatchString(line) {
lineColor = element.color
break
}
}
return LogMessage{line, lineColor}
return LogMessage{line, lineColor, logFile}
}

0 comments on commit 51a638d

Please sign in to comment.