github.com/gogf/gf@v1.16.9/os/glog/glog_logger_handler.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package glog
     8  
     9  import (
    10  	"bytes"
    11  	"context"
    12  	"time"
    13  )
    14  
    15  // Handler is function handler for custom logging content outputs.
    16  type Handler func(ctx context.Context, in *HandlerInput)
    17  
    18  // HandlerInput is the input parameter struct for logging Handler.
    19  type HandlerInput struct {
    20  	Logger       *Logger         // Logger.
    21  	Ctx          context.Context // Context.
    22  	Buffer       *bytes.Buffer   // Buffer for logging content outputs.
    23  	Time         time.Time       // Logging time, which is the time that logging triggers.
    24  	TimeFormat   string          // Formatted time string, like "2016-01-09 12:00:00".
    25  	Color        int             // Using color, like COLOR_RED, COLOR_BLUE, etc.
    26  	Level        int             // Using level, like LEVEL_INFO, LEVEL_ERRO, etc.
    27  	LevelFormat  string          // Formatted level string, like "DEBU", "ERRO", etc.
    28  	CallerFunc   string          // The source function name that calls logging.
    29  	CallerPath   string          // The source file path and its line number that calls logging.
    30  	CtxStr       string          // The retrieved context value string from context.
    31  	Prefix       string          // Custom prefix string for logging content.
    32  	Content      string          // Content is the main logging content that passed by you.
    33  	IsAsync      bool            // IsAsync marks it is in asynchronous logging.
    34  	handlerIndex int             // Middleware handling index for internal usage.
    35  }
    36  
    37  // Next calls the next logging handler in middleware way.
    38  func (i *HandlerInput) Next() {
    39  	if len(i.Logger.config.Handlers)-1 > i.handlerIndex {
    40  		i.handlerIndex++
    41  		i.Logger.config.Handlers[i.handlerIndex](i.Ctx, i)
    42  	} else {
    43  		defaultHandler(i.Ctx, i)
    44  	}
    45  }
    46  
    47  // String returns the logging content formatted by default logging handler.
    48  func (i *HandlerInput) String(withColor ...bool) string {
    49  	formatWithColor := false
    50  	if len(withColor) > 0 {
    51  		formatWithColor = withColor[0]
    52  	}
    53  	return i.getDefaultBuffer(formatWithColor).String()
    54  }
    55  
    56  func (i *HandlerInput) getDefaultBuffer(withColor bool) *bytes.Buffer {
    57  	buffer := bytes.NewBuffer(nil)
    58  	if i.TimeFormat != "" {
    59  		buffer.WriteString(i.TimeFormat)
    60  	}
    61  	if i.LevelFormat != "" {
    62  		if withColor {
    63  			i.addStringToBuffer(buffer, i.Logger.getColoredStr(
    64  				i.Logger.getColorByLevel(i.Level), i.LevelFormat,
    65  			))
    66  		} else {
    67  			i.addStringToBuffer(buffer, i.LevelFormat)
    68  		}
    69  	}
    70  	if i.Prefix != "" {
    71  		i.addStringToBuffer(buffer, i.Prefix)
    72  	}
    73  	if i.CtxStr != "" {
    74  		i.addStringToBuffer(buffer, i.CtxStr)
    75  	}
    76  	if i.CallerFunc != "" {
    77  		i.addStringToBuffer(buffer, i.CallerFunc)
    78  	}
    79  	if i.CallerPath != "" {
    80  		i.addStringToBuffer(buffer, i.CallerPath)
    81  	}
    82  	if i.Content != "" {
    83  		i.addStringToBuffer(buffer, i.Content)
    84  	}
    85  	i.addStringToBuffer(buffer, "\n")
    86  	return buffer
    87  }
    88  
    89  func (i *HandlerInput) getRealBuffer(withColor bool) *bytes.Buffer {
    90  	if i.Buffer.Len() > 0 {
    91  		return i.Buffer
    92  	}
    93  	return i.getDefaultBuffer(withColor)
    94  }
    95  
    96  // defaultHandler is the default handler for logger.
    97  func defaultHandler(ctx context.Context, in *HandlerInput) {
    98  	buffer := in.Logger.doDefaultPrint(ctx, in)
    99  	if in.Buffer.Len() == 0 {
   100  		in.Buffer = buffer
   101  	}
   102  }
   103  
   104  func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, strings ...string) {
   105  	for _, s := range strings {
   106  		if buffer.Len() > 0 {
   107  			buffer.WriteByte(' ')
   108  		}
   109  		buffer.WriteString(s)
   110  	}
   111  }