mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve handler feature for package glog
This commit is contained in:
31
.example/os/glog/handler/glog_handler_greylog.go
Normal file
31
.example/os/glog/handler/glog_handler_greylog.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
//import (
|
||||
// "context"
|
||||
// "github.com/gogf/gf/frame/g"
|
||||
// "github.com/gogf/gf/os/glog"
|
||||
// "github.com/robertkowalski/graylog-golang"
|
||||
//)
|
||||
//
|
||||
//var greyLogClient = gelf.New(gelf.Config{
|
||||
// GraylogPort: 80,
|
||||
// GraylogHostname: "graylog-host.com",
|
||||
// Connection: "wan",
|
||||
// MaxChunkSizeWan: 42,
|
||||
// MaxChunkSizeLan: 1337,
|
||||
//})
|
||||
//
|
||||
//// LoggingGreyLogHandler is an example handler for logging content to remote GreyLog service.
|
||||
//var LoggingGreyLogHandler glog.Handler = func(ctx context.Context, in *glog.HandlerInput) {
|
||||
// in.Next()
|
||||
// greyLogClient.Log(in.Buffer.String())
|
||||
//}
|
||||
//
|
||||
//func main() {
|
||||
// g.Log().SetHandlers(LoggingGreyLogHandler)
|
||||
//
|
||||
// g.Log().Debug("Debugging...")
|
||||
// g.Log().Warning("It is warning info")
|
||||
// g.Log().Error("Error occurs, please have a check")
|
||||
// glog.Println("test log")
|
||||
//}
|
||||
@ -16,7 +16,7 @@ type JsonOutputsForLogger struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// LoggingJsonHandler is a example handler for logging JSON format content.
|
||||
// LoggingJsonHandler is an example handler for logging JSON format content.
|
||||
var LoggingJsonHandler glog.Handler = func(ctx context.Context, in *glog.HandlerInput) {
|
||||
jsonForLogger := JsonOutputsForLogger{
|
||||
Time: in.TimeFormat,
|
||||
|
||||
@ -227,24 +227,33 @@ func (l *Logger) print(ctx context.Context, level int, values ...interface{}) {
|
||||
}
|
||||
|
||||
// doDefaultPrint outputs the logging content according configuration.
|
||||
func (l *Logger) doDefaultPrint(ctx context.Context, input *HandlerInput) {
|
||||
func (l *Logger) doDefaultPrint(ctx context.Context, input *HandlerInput) *bytes.Buffer {
|
||||
var buffer *bytes.Buffer
|
||||
if l.config.Writer == nil {
|
||||
// Output content to disk file.
|
||||
if l.config.Path != "" {
|
||||
l.printToFile(ctx, input.Time, input)
|
||||
}
|
||||
// Allow output to stdout?
|
||||
if l.config.StdoutPrint {
|
||||
l.printToStdout(ctx, input)
|
||||
if buf := l.printToStdout(ctx, input); buf != nil {
|
||||
buffer = buf
|
||||
}
|
||||
}
|
||||
|
||||
// Output content to disk file.
|
||||
if l.config.Path != "" {
|
||||
if buf := l.printToFile(ctx, input.Time, input); buf != nil {
|
||||
buffer = buf
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Output to custom writer.
|
||||
l.printToWriter(ctx, input)
|
||||
if buf := l.printToWriter(ctx, input); buf != nil {
|
||||
buffer = buf
|
||||
}
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
||||
// printToWriter writes buffer to writer.
|
||||
func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) {
|
||||
func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) *bytes.Buffer {
|
||||
if l.config.Writer != nil {
|
||||
var (
|
||||
buffer = input.getRealBuffer(l.config.WriterColorEnable)
|
||||
@ -252,23 +261,30 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) {
|
||||
if _, err := l.config.Writer.Write(buffer.Bytes()); err != nil {
|
||||
intlog.Error(ctx, err)
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// printToStdout outputs logging content to stdout.
|
||||
func (l *Logger) printToStdout(ctx context.Context, input *HandlerInput) {
|
||||
func (l *Logger) printToStdout(ctx context.Context, input *HandlerInput) *bytes.Buffer {
|
||||
if l.config.StdoutPrint {
|
||||
var (
|
||||
buffer = input.getRealBuffer(true)
|
||||
)
|
||||
// This will lose color in Windows os system.
|
||||
// if _, err := os.Stdout.Write(input.getRealBuffer(true).Bytes()); err != nil {
|
||||
// This will print color in Windows os system.
|
||||
if _, err := fmt.Fprintf(color.Output, input.getRealBuffer(true).String()); err != nil {
|
||||
if _, err := fmt.Fprintf(color.Output, buffer.String()); err != nil {
|
||||
intlog.Error(ctx, err)
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// printToFile outputs logging content to disk file.
|
||||
func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput) {
|
||||
func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput) *bytes.Buffer {
|
||||
var (
|
||||
buffer = in.getRealBuffer(l.config.WriterColorEnable)
|
||||
logFilePath = l.getFilePath(t)
|
||||
@ -294,6 +310,7 @@ func (l *Logger) printToFile(ctx context.Context, t time.Time, in *HandlerInput)
|
||||
intlog.Error(ctx, err)
|
||||
}
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
||||
// getFilePointer retrieves and returns a file pointer from file pool.
|
||||
|
||||
@ -95,7 +95,10 @@ func (i *HandlerInput) getRealBuffer(withColor bool) *bytes.Buffer {
|
||||
|
||||
// defaultHandler is the default handler for logger.
|
||||
func defaultHandler(ctx context.Context, in *HandlerInput) {
|
||||
in.Logger.doDefaultPrint(ctx, in)
|
||||
buffer := in.Logger.doDefaultPrint(ctx, in)
|
||||
if in.Buffer.Len() == 0 {
|
||||
in.Buffer = buffer
|
||||
}
|
||||
}
|
||||
|
||||
func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, strings ...string) {
|
||||
|
||||
Reference in New Issue
Block a user