Documentation ¶
Index ¶
- Constants
- Variables
- func CopyHeader(dst *LogBuffer, src *LogBuffer)
- func CxtRequestID(c *gin.Context) string
- func GetCtxLogger(c *gin.Context) *logrus.Entry
- func GetCxtRequestID(c *gin.Context) string
- func SetCtxLogger(c *gin.Context, logger *logrus.Entry) *logrus.Entry
- func SetCtxLoggerHeader(c *gin.Context, name string, data interface{})
- func WithTracing(logger loggerEntryWithFields, useBanner bool, timeFormat string, utc bool, ...) gin.HandlerFunc
- type LogBuffer
- func (b *LogBuffer) DeleteHeader(k string)
- func (b *LogBuffer) GetAllHeaders() (map[string]interface{}, error)
- func (b *LogBuffer) GetHeader(k string) (interface{}, bool)
- func (b *LogBuffer) Length() int
- func (b *LogBuffer) SetCustomBanner(banner string)
- func (b *LogBuffer) StoreHeader(k string, v interface{})
- func (b *LogBuffer) String() string
- func (b *LogBuffer) Write(data []byte) (n int, err error)
- type LogBufferOption
- type Option
- type ReducedLoggingFunc
Constants ¶
const DefaultBanner = "" /* 155-byte string literal not displayed */
const DefaultLogBufferMaxSize = 100000
DefaultLogBufferMaxSize - avg single spaced page contains 3k chars, so 100k == 33 pages which is a reasonable max
Variables ¶
var ContextTraceIDField string
ContextTraceIDField - used to find the trace id in the gin.Context - optional
Functions ¶
func CxtRequestID ¶
CxtRequestID - if not already set, then add logrus Field to the entry with the tracing ID for the request. then return the trace/request id
func GetCtxLogger ¶
GetCtxLogger - get the *logrus.Entry for this request from the gin.Context
func GetCxtRequestID ¶
GetCxtRequestID - dig the request ID out of the *logrus.Entry in the gin.Context
func SetCtxLogger ¶
SetCtxLogger - used when you want to set the *logrus.Entry with new logrus.WithFields{} for this request in the gin.Context so it can be used going forward for the request
func SetCtxLoggerHeader ¶
SetCtxLoggerHeader - if aggregate logging, add header info... otherwise just info log the data passed
func WithTracing ¶
func WithTracing( logger loggerEntryWithFields, useBanner bool, timeFormat string, utc bool, logrusFieldNameForTraceID string, traceIDHeader []byte, contextTraceIDField []byte, opt ...Option) gin.HandlerFunc
WithTracing returns a gin.HandlerFunc (middleware) that logs requests using logrus.
Requests with errors are logged using logrus.Error(). Requests without errors are logged using logrus.Info().
It receives:
- A logrus.Entry with fields
- A boolean stating whether to use a BANNER in the log entry
- A time package format string (e.g. time.RFC3339).
- A boolean stating whether to use UTC time zone or local.
- A string to use for Trace ID the Logrus log field.
- A []byte for the request header that contains the trace id
- A []byte for "getting" the requestID out of the gin.Context
- A list of possible ginlogrus.Options to apply
Types ¶
type LogBuffer ¶
type LogBuffer struct { Buff strings.Builder AddBanner bool MaxSize uint // contains filtered or unexported fields }
LogBuffer - implement io.Writer inferface to append to a string
func NewBuffer ¶
NewBuffer - create a new aggregate logging buffer for the *logrus.Entry , which can be flushed by the consumer how-to, when to use this:
the request level log entry is written when the request is over, so you need this thing to write go routine logs that complete AFTER the request is completed. careful: the loggers will share a ref to the same Header (writes to one will affect the other)
example:
go func() { buff := NewBuffer(logger) // logger is an existing *logrus.Entry // do somem work here and write some logs via the logger. Like logger.Info("hi mom! I'm a go routine that finished after the request") fmt.Printf(buff.String()) // this will write the aggregated buffered logs to stdout }()
func NewLogBuffer ¶
func NewLogBuffer(opt ...LogBufferOption) LogBuffer
NewLogBuffer - create a LogBuffer and initialize it
func (*LogBuffer) DeleteHeader ¶
DeleteHeader - delete a header
func (*LogBuffer) GetAllHeaders ¶
GetAllHeaders - return all the headers
func (*LogBuffer) SetCustomBanner ¶
SetCustomBanner allows a custom banner to be set after the NewLogBuffer() has been used
func (*LogBuffer) StoreHeader ¶
StoreHeader - store a header
type LogBufferOption ¶
type LogBufferOption func(*logBufferOptions)
LogBufferOption - define options for LogBuffer
func WithBanner ¶
func WithBanner(a bool) LogBufferOption
WithBanner - define an Option func for passing in an optional add Banner
func WithCustomBanner ¶
func WithCustomBanner(b string) LogBufferOption
WithCustomBanner allows users to define their own custom banner
func WithHeader ¶
func WithHeader(k string, v interface{}) LogBufferOption
WithHeader - define an Option func for passing in a set of optional header
func WithMaxSize ¶
func WithMaxSize(s uint) LogBufferOption
WithMaxSize specifies the bounded max size the logBuffer can grow to
type Option ¶
type Option func(*options)
Option - define options for WithTracing()
func WithAggregateLogging ¶
WithAggregateLogging - define an Option func for passing in an optional aggregateLogging
func WithEmptyAggregateEntries ¶ added in v2.0.12
WithEmptyAggregateEntries - define an Option func for printing aggregate logs with empty entries
func WithLogCustomBanner ¶
WithLogCustomBanner allows users to define their own custom banner. There is some overlap with this name and the LogBufferOption.CustomBanner and yes, they are related options, but I didn't want to make a breaking API change to support this new option... so we'll have to live with a bit of confusion/overlap in option names
func WithLogLevel ¶
WithLogLevel - define an Option func for passing in an optional logLevel
func WithReducedLoggingFunc ¶ added in v2.0.12
func WithReducedLoggingFunc(a ReducedLoggingFunc) Option
WithReducedLoggingFunc - define an Option func for reducing logs based on a custom function
func WithWriter ¶
WithWriter allows users to define the writer used for middlware aggregagte logging, the default writer is os.Stdout
type ReducedLoggingFunc ¶ added in v2.0.12
Function definition for reduced logging. The return value of this function will be used to determine whether or not a log will be output.