OpenDiablo2 / HellSpawner

The OpenDiablo2 toolset
GNU General Public License v3.0
153 stars 45 forks source link

refactor logging throughout the app #309

Open gravestench opened 3 years ago

gravestench commented 3 years ago

We need a better abstraction on top of our current logging implementation.

@essial suggests we try to integrate zerolog(https://pkg.go.dev/github.com/rs/zerolog)

gucio321 commented 3 years ago

@gravestench whats the problem with log? do we really need log levels? also, I suggest to rewrite Write method of console (or add a separated Write method for our pro logger :grinning:) to open dialog message for errors

// Write writes input on console, stdout and (if exists) to the log file
func (c *Console) Write(p []byte) (n int, err error) {
        msg := string(p) // convert message from byte slice into string

        c.outputText = msg + c.outputText // append message

        fmt.Print(msg) // print to terminal

        // log message looks like: file:line: message
        // as an alternative, use strings.Contains(strings.ToLower(msg), "error")
        if splited := strings.Split(msg, " "); strings.ToLower(splited[1]) == "error" {
              dialog.Message(msg).Error()
       }

        if c.logFile != nil {
                n, err = c.logFile.Write(p) // print to file
                if err != nil {
                        return n, fmt.Errorf("error writing to log file: %w", err)
                } else if n != len(p) {
                        return n, fmt.Errorf("invalid data written to log file")
                }
        }

        return len(p), nil
}