xlog

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2021 License: Apache-2.0 Imports: 15 Imported by: 3

Documentation

Index

Constants

View Source
const HTTPRequestKey = "http:request"

HTTPRequestKey key used in fields to log http request data

Variables

View Source
var NoHTTPOpt = cgerrors.ErrInvalidArgument("logging - no http request found")

NoHTTPOpt means no request ke was found

Functions

func AddHook

func AddHook(hook logrus.Hook)

AddHook adds a hook to the standard xlog hooks.

func Ctx

func Ctx(ctx context.Context) *logrus.Entry

Ctx will extract log Entry from context

func CtxFields

func CtxFields(ctx context.Context, fields logrus.Fields) context.Context

CtxFields will add xlog fields

func CtxPut

func CtxPut(ctx context.Context, entry *logrus.Entry) context.Context

CtxPut will put log into context

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard xlog.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at level Debug on the standard xlog.

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard xlog.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard xlog.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at level Error on the standard xlog.

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard xlog.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard xlog then the process will exit with status set to 1.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a message at level Fatal on the standard xlog then the process will exit with status set to 1.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard xlog then the process will exit with status set to 1.

func GetLevel

func GetLevel() logrus.Level

GetLevel returns the standard xlog level.

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard xlog.

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at level Info on the standard xlog.

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard xlog.

func IsLevelEnabled

func IsLevelEnabled(level logrus.Level) bool

IsLevelEnabled checks if the log level of the standard xlog is greater than the level param

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard xlog.

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a message at level Panic on the standard xlog.

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard xlog.

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard xlog.

func Printf

func Printf(format string, args ...interface{})

Printf logs a message at level Info on the standard xlog.

func PrintfHTTPReq

func PrintfHTTPReq(ctx context.Context, req HTTPRequest)

PrintfHTTPReq will log request on specific level

func Println

func Println(args ...interface{})

Println logs a message at level Info on the standard xlog.

func Req

func Req(req *http.Request) *logrus.Entry

Req will pull xlog from request context

func ReqWrap

func ReqWrap(req *http.Request, entry *logrus.Entry) *http.Request

ReqWrap will put xlog into request context

func SetFormatter

func SetFormatter(formatter logrus.Formatter)

SetFormatter sets the standard xlog formatter.

func SetLevel

func SetLevel(level logrus.Level)

SetLevel sets the standard xlog level.

func SetOutput

func SetOutput(out io.Writer)

SetOutput sets the standard xlog output.

func SetReportCaller

func SetReportCaller(include bool)

SetReportCaller sets whether the standard xlog will include the calling method as a field.

func Trace

func Trace(args ...interface{})

Trace logs a message at level Trace on the standard xlog.

func Tracef

func Tracef(format string, args ...interface{})

Tracef logs a message at level Trace on the standard xlog.

func Traceln

func Traceln(args ...interface{})

Traceln logs a message at level Trace on the standard xlog.

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the standard xlog.

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a message at level Warn on the standard xlog.

func Warning

func Warning(args ...interface{})

Warning logs a message at level Warn on the standard xlog.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a message at level Warn on the standard xlog.

func Warningln

func Warningln(args ...interface{})

Warningln logs a message at level Warn on the standard xlog.

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at level Warn on the standard xlog.

func WithContext

func WithContext(ctx context.Context) *logrus.Entry

WithContext returns a new log entry with given context.

func WithErr

func WithErr(err error) *logrus.Entry

WithErr creates an entry from the standard xlog and adds an error to it, using the value defined in ErrorKey as key.

func WithField

func WithField(key string, value interface{}) *logrus.Entry

WithField creates an entry from the standard xlog and adds a field to it. If you want multiple fields, use `WithFields`.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields logrus.Fields) *logrus.Entry

WithFields creates an entry from the standard xlog and adds multiple fields to it. This is simply a helper for `WithField`, invoking it once for each field.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithTime

func WithTime(t time.Time) *logrus.Entry

WithTime creats an entry from the standard xlog and overrides the time of logs generated with it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

Types

type CallerHook

type CallerHook struct {
	// contains filtered or unexported fields
}

CallerHook is the logrus.Hook that sets up the logrus.Entry.Caller field. Sets up the entry.Logger

func DefaultCallerHook

func DefaultCallerHook() *CallerHook

DefaultCallerHook gets the default caller hook that sets up the caller for the level fatal, error, panic, warn and trace.

func NewCallerHook

func NewCallerHook(levels []logrus.Level) *CallerHook

NewCallerHook creates a new CallerHook.

func (CallerHook) Fire

func (c CallerHook) Fire(entry *logrus.Entry) error

Fire sets up the Caller field in the logrus.Entry. Implements logrus.Hook.

func (CallerHook) Levels

func (c CallerHook) Levels() []logrus.Level

Levels gets the levels that this hook is available for. Implements logrus.Hook.

type ColorScheme

type ColorScheme struct {
	InfoLevelStyle  string
	WarnLevelStyle  string
	ErrorLevelStyle string
	FatalLevelStyle string
	PanicLevelStyle string
	DebugLevelStyle string
	PrefixStyle     string
	TimestampStyle  string
}

ColorScheme represents colors used with terminal

type CtxKey

type CtxKey string

CtxKey represents custom type for context

const CtxLogger CtxKey = "xlog:entry"

CtxLogger represents key in context

type HTTPRequest

type HTTPRequest struct {
	// The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`, `"POST"`.
	RequestMethod string `json:"requestMethod,omitempty"`
	// The scheme (http, https), the host name, the path and the query
	// portion of the URL that was requested.
	// Example: `"http://example.com/some/info?color=red"`.
	RequestURL string `json:"requestUrl,omitempty"`
	// The size of the HTTP request message in bytes, including the request
	// headers and the request body.
	RequestSize int64 `json:"requestSize,omitempty"`
	// The response code indicating the status of response.
	// Examples: 200, 404.
	Status int32 `json:"status,omitempty"`
	// The size of the HTTP response message sent back to the client, in bytes,
	// including the response headers and the response body.
	ResponseSize int64 `json:"responseSize,omitempty"`
	// The user agent sent by the client. Example:
	// `"Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Q312461; .NET CLR 1.0.3705)"`.
	UserAgent string `json:"userAgent,omitempty"`
	// The IP address (IPv4 or IPv6) of the client that issued the HTTP
	// request. Examples: `"192.168.1.1"`, `"FE80::0202:B3FF:FE1E:8329"`.
	RemoteIP string `json:"remoteIp,omitempty"`
	// The IP address (IPv4 or IPv6) of the origin server that the request was
	// sent to.
	ServerIP string `json:"serverIp,omitempty"`
	// The referer URL of the request, as defined in
	// [HTTP/1.1 Header Field Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
	Referer string `json:"referer,omitempty"`
	// The request processing latency on the server, from the time the request was
	// received until the response was sent.
	Latency string `json:"latency,omitempty"`
	// Whether or not a cache lookup was attempted.
	CacheLookup bool `json:"cacheLookup,omitempty"`
	// Whether or not an entity was served from cache
	// (with or without validation).
	CacheHit bool `json:"cacheHit,omitempty"`
	// Whether or not the response was validated with the origin server before
	// being served from cache. This field is only meaningful if `cache_hit` is
	// True.
	CacheValidatedWithOriginServer bool `json:"cacheValidatedWithOriginServer,omitempty"`
	// The number of HTTP response bytes inserted into cache. Set only when a
	// cache fill was attempted.
	CacheFillBytes int64 `json:"cacheFillBytes,omitempty"`
	// Protocol used for the request. Examples: "HTTP/1.1", "HTTP/2", "websocket"
	Protocol string `json:"protocol,omitempty"`
}

HTTPRequest for logging HTTP requests. Only contains semantics defined by the HTTP specification. Product-specific logging information MUST be defined in a separate message.

func ExtractHTTPField

func ExtractHTTPField(r logrus.Fields) (HTTPRequest, error)

ExtractHTTPField will extract http option

type Log

type Log struct {
	*logrus.Logger
}

Log is wrapper around logrus

func New

func New() *Log

New will create new instance if logrus

func StdLogger

func StdLogger() *Log

StdLogger returns standart xlog

func (*Log) NewEntry

func (logger *Log) NewEntry() *logrus.Entry

NewEntry creates

func (*Log) WithErr

func (logger *Log) WithErr(err error) *logrus.Entry

WithErr format error.

type Logger

type Logger interface {
	logrus.FieldLogger
}

Logger is interface which satisfies Entry and Log

type RequestIDHook

type RequestIDHook struct{}

RequestIDHook is a logging hook used to log the request id stored in the context metadata.

func (RequestIDHook) Fire

func (m RequestIDHook) Fire(entry *logrus.Entry) error

func (RequestIDHook) Levels

func (m RequestIDHook) Levels() []logrus.Level

type TextFormatter

type TextFormatter struct {
	// TimestampFormat sets the format used for marshaling timestamps.
	// The format to use is the same than for time.Format or time.Parse from the standard
	// library.
	// The standard Library already provides a set of predefined format.
	TimestampFormat string

	// Force disabling colors. For a TTY colors are enabled by default.
	UseColors bool

	// DisableTimestamp allows disabling automatic timestamps in output
	DisableTimestamp bool

	// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
	DataKey string

	// CallerPrettyfier can be set by the user to modify the content
	// of the function and file keys in the json data when ReportCaller is
	// activated. If any of the returned value is the empty string the
	// corresponding key will be removed from fields.
	CallerPrettyfier func(*runtime.Frame) (function string, file string)
	// contains filtered or unexported fields
}

TextFormatter is struct implementing Format interface this is useful for formatting logs in different environments. This formatter will format logs for terminal and testing.

func NewTextFormatter

func NewTextFormatter(colors bool) *TextFormatter

NewTextFormatter creates new logrus based text formatter.

func (*TextFormatter) Format

func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error)

Format implements `logrus.Formatter`.

type UserIDHook

type UserIDHook struct{}

UserIDHook is a hook that gets the user id from the context metadata and logs it on each log level.

func (UserIDHook) Fire

func (u UserIDHook) Fire(entry *logrus.Entry) error

func (UserIDHook) Levels

func (u UserIDHook) Levels() []logrus.Level

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL