phuslu / log

Fastest structured logging
MIT License
672 stars 44 forks source link

Phuslu Wrapper for Fiber #15

Closed sujit-baniya closed 3 years ago

sujit-baniya commented 3 years ago

Hi, I've created phuslu log wrapper for Fiber. Please suggest if you have any opinions or suggestion to improve

https://github.com/sujit-baniya/sblogger

phuslu commented 3 years ago

I added a new option FileWriter.EnsureFolder, so seems that usage in README could simplify to

log.DefaultLogger = log.Logger{
    TimeField:  "timestamp",
    TimeFormat: "2006-01-02 15:04:05",
    Writer:     &log.MultiWriter{
        InfoWriter:    &log.FileWriter{Filename: "storage/logs/INFO.log", EnsureFolder: true},
        WarnWriter:    &log.FileWriter{Filename: "storage/logs/WARN.log", EnsureFolder: true},
        ErrorWriter:   &log.FileWriter{Filename: "storage/logs/ERROR.log", EnsureFolder: true},
        ConsoleWriter: &log.IOWriter{os.Stderr},
        ConsoleLevel:  log.InfoLevel,
    },
}
phuslu commented 3 years ago

Maybe replace the map(Fields) with Context is better for performance and ordering. https://github.com/sujit-baniya/sblogger/blob/master/sblogger.go#L34

An example, https://github.com/phuslu/log#contextual-fields

sujit-baniya commented 3 years ago

@phuslu I've changed the code as per you've suggested.

I want to use only Date in format INFO.2020-11-17.log instead of INFO.2020-11-17T11-31-32.log

How can I change the file name

phuslu commented 3 years ago

I added new option FileWriter.TimeFormat, it should be OK https://github.com/phuslu/log/commit/22893967911709736a99d52ee6469d17c780a130

(I will release a tag later)

sujit-baniya commented 3 years ago

Great! Let me check.

sujit-baniya commented 3 years ago

It's working as expected. I've one question though... What is the use of INFO.log when there's other files per timestamp For e.g. I see two files: INFO.2020-01-02.log and INFO.log

INFO.log contains all the log data. Is there any use of this file? I see it just consume space as the information are already in other files with timestamp

phuslu commented 3 years ago
  1. INFO.log is a symlink to INFO.2020-01-02.log, it does not consume space.
  2. INFO.2020-01-02.log contains all the log data is as intended, to following glog design.
sujit-baniya commented 3 years ago

Aha Great! Also can we include 2rd party Log Monitor tools like Grafana, Logstash, graylog,etc along with Multi FileWriter?

phuslu commented 3 years ago

I don't understand, could you please give a sample ?

sujit-baniya commented 3 years ago

typo: 3rd party log monitor

I mean it would be great, if I could use some other log collector services as Hook along with Files. For e.g. Graylog - I can send logs to Graylog https://github.com/Graylog2/collector-sidecar Grafana - https://github.com/grafana/loki Logstash - https://github.com/bshuster-repo/logrus-logstash-hook

sujit-baniya commented 3 years ago

For e.g.


type FileWriter struct {
    Hook io.Writer
    Filename string
    EnsureFolder bool
}

In Hook, I could define any other service and the logger would write to the Writer as well as to the file.Or only to Hook if Filename not available.

sujit-baniya commented 3 years ago
gelfWriter, err := gelf.NewWriter(graylogAddr)
        if err != nil {
            log.Fatalf("gelf.NewWriter: %s", err)
        }
        // log to both stderr and graylog2
        log.SetOutput(io.MultiWriter(os.Stderr, /*FileWriter*/, gelfWriter))
phuslu commented 3 years ago

Yes, Hooks is a must have feature for a modern logger.

  1. For log entry hook, we could use a middleware like writer as Hooks

    
    type MoniterWriter struct {
    RemoteAddr string
    Writer     log.Writer
    
    once sync.Once
    conn *net.Conn
    }

func (w MoniterWriter) WriteEntry(e Entry) (n int, err error) { w.once.Do(func() { w.conn, err = net.Dial("tcp", RemoteAddr) })

if err != nil {
    return
}

if e.Level > log.InfoLevel {
    // do something with w.conn
}

return w.Writer.WriteEntry(e)

}

func main() { logger := log.Logger{ Writer: &MoniterWriter{ RemoteAddr: "", Writer: &log.FileWriter{Filename: "main.log"}, } }

logger.Error().Msgf("hello %s", "world")

}


2. For log Field hooks, there's no solution currently
sujit-baniya commented 3 years ago

Yeah, I think you could add one more Writer MoniterWriter in MultiWriter for now.

phuslu commented 3 years ago

I'd like keep multi.go https://github.com/phuslu/log/blob/master/multi.go be simple. Users could copy and paste it and add the additional functions as their wanted.

  1. *Entry.Level expose the log entry level
  2. *Entry.Context() expose the underlaying buf