Closed sujit-baniya closed 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,
},
}
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
@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
I added new option FileWriter.TimeFormat
, it should be OK
https://github.com/phuslu/log/commit/22893967911709736a99d52ee6469d17c780a130
(I will release a tag later)
Great! Let me check.
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
Aha Great! Also can we include 2rd party Log Monitor tools like Grafana, Logstash, graylog,etc along with Multi FileWriter?
I don't understand, could you please give a sample ?
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
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.
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))
Yes, Hooks
is a must have feature for a modern logger.
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: "
logger.Error().Msgf("hello %s", "world")
}
2. For log Field hooks, there's no solution currently
Yeah, I think you could add one more Writer MoniterWriter
in MultiWriter for now.
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.
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