logs

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPRequestProperties

type HTTPRequestProperties struct {
	Method     string
	Path       string
	RemoteAddr string
	UserAgent  string
	UseRegex   bool // If true, interprets all properties as regex. Otherwise matches on equality
}

HTTPRequestProperties is an entity which contains the properties of an HTTP request

func NewAwsHealthCheckHTTPRequestProperties

func NewAwsHealthCheckHTTPRequestProperties(path string) HTTPRequestProperties

NewAwsHealthCheckHTTPRequestProperties creates an HTTPRequestProperties object for a standard AWS ELB health checker

Path: The path that the health checks are performed on. If empty, "/version" is used as the default value.

func NewOpenShiftHealthCheckHTTPRequestProperties

func NewOpenShiftHealthCheckHTTPRequestProperties(path string) HTTPRequestProperties

NewOpenShiftHealthCheckHTTPRequestProperties creates an HTTPRequestProperties object for a standard OpenShift health checker

Path: The path that the health checks are performed on. If empty, "/version" is used as the default value.

func NewStandardHealthCheckHTTPRequestProperties

func NewStandardHealthCheckHTTPRequestProperties(path string) []HTTPRequestProperties

NewStandardHealthCheckHTTPRequestProperties creates a list of HTTPRequestProperties objects for known standard health checkers

Path: The path that the health checks are performed on. If empty, "/version" is used as the default value.

func (HTTPRequestProperties) Match

func (h HTTPRequestProperties) Match(r *http.Request) bool

Match returns true if the provided http.Request matches the defined request properties

func (HTTPRequestProperties) MatchEquality

func (h HTTPRequestProperties) MatchEquality(r *http.Request) bool

MatchEquality returns true if the provided http.Request matches the defined request properties by equality

func (HTTPRequestProperties) MatchRegex

func (h HTTPRequestProperties) MatchRegex(r *http.Request) bool

MatchRegex returns true if the provided http.Request matches the defined request properties by regex

type HTTPResponse

type HTTPResponse struct {
	ResponseCode int
	Headers      map[string][]string
	Body         []byte
	Cookies      []http.Cookie
}

HTTPResponse is an entity which contains the data to be sent in an HTTP response

func NewErrorHTTPResponse

func NewErrorHTTPResponse(body string, code int) HTTPResponse

NewErrorHTTPResponse generates an HTTPResponse with the correct headers for an error string

func NewHTTPResponse

func NewHTTPResponse(body []byte, headers map[string]string, code int) HTTPResponse

NewHTTPResponse generates an HTTPResponse with the provided data

func NewJSONErrorHTTPResponse

func NewJSONErrorHTTPResponse(body string, code int) HTTPResponse

NewJSONErrorHTTPResponse generates an HTTPResponse with the correct headers for a JSON encoded error

func (*HTTPResponse) SetCookie

func (h *HTTPResponse) SetCookie(cookie http.Cookie)

SetCookie appends the given cookie to the list of cookies in the response

type Log

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

Log struct defines a log object of a request

func (*Log) AddContext

func (l *Log) AddContext(fieldName string, value interface{}) error

AddContext adds any relevant unstructured data to context map If the provided key already exists in the context, an error is returned

func (*Log) Debug

func (l *Log) Debug(message string)

Debug prints the log at debug level with given message

func (*Log) DebugWithDetails

func (l *Log) DebugWithDetails(message string, details logutils.Fields)

DebugWithDetails prints the log at debug level with given fields and message

func (*Log) Debugf

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

Debugf prints the log at debug level with given formatted string

func (*Log) Error

func (l *Log) Error(message string)

Error prints the log at error level with given message Note: If possible, use LogError() instead

func (*Log) ErrorWithDetails

func (l *Log) ErrorWithDetails(message string, details logutils.Fields)

ErrorWithDetails prints the log at error level with given details and message

func (*Log) Errorf

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

Errorf prints the log at error level with given formatted string Note: If possible, use LogError() instead

func (*Log) GetContext

func (l *Log) GetContext(fieldName string) interface{}

GetContext gets the provided context key

func (*Log) HTTPResponseError

func (l *Log) HTTPResponseError(message string, err error, code int, showDetails bool) HTTPResponse

HTTPResponseError logs the provided message and error and generates an HttpResponse

Params:
	message: The error message
	err: The error received from the application
	code: The HTTP response code to be set
	showDetails: Only provide 'message' not 'err' in HTTP response when false

func (*Log) HTTPResponseErrorAction

func (l *Log) HTTPResponseErrorAction(action logutils.MessageActionType, dataType logutils.MessageDataType, args logutils.MessageArgs, err error, code int, showDetails bool) HTTPResponse

HTTPResponseErrorAction logs an action message and error and generates an HttpResponse

action: The action that is occurring
dataType: The data type
args: Any args that should be included in the message (nil if none)
err: The error received from the application
code: The HTTP response code to be set
showDetails: Only generated message not 'err' in HTTP response when false

func (*Log) HTTPResponseErrorData

func (l *Log) HTTPResponseErrorData(status logutils.MessageDataStatus, dataType logutils.MessageDataType, args logutils.MessageArgs, err error, code int, showDetails bool) HTTPResponse

HTTPResponseErrorData logs a data message and error and generates an HttpResponse

status: The status of the data
dataType: The data type
args: Any args that should be included in the message (nil if none)
err: The error received from the application
code: The HTTP response code to be set
showDetails: Only provide 'msg' not 'err' in HTTP response when false

func (*Log) HTTPResponseSuccess

func (l *Log) HTTPResponseSuccess() HTTPResponse

HTTPResponseSuccess generates an HttpResponse with the message "Success" with status code 200, sets standard headers, and stores the status to the log context

func (*Log) HTTPResponseSuccessAction

func (l *Log) HTTPResponseSuccessAction(action logutils.MessageActionType, dataType logutils.MessageDataType, args logutils.MessageArgs) HTTPResponse

HTTPResponseSuccessAction generates an HttpResponse with the provided success action message with status code 200, sets standard headers, and stores the message to the log context

Params:
	action: The action that is occurring
	dataType: The data type that the action is occurring on
	args: Any args that should be included in the message (nil if none)

func (*Log) HTTPResponseSuccessBytes

func (l *Log) HTTPResponseSuccessBytes(bytes []byte, contentType string) HTTPResponse

HTTPResponseSuccessBytes generates an HttpResponse with the provided bytes as the HTTP response body with status code 200, sets standard headers, and stores the status to the log context

Params:
	bytes: Response data
	contentType: Content type header string

func (*Log) HTTPResponseSuccessJSON

func (l *Log) HTTPResponseSuccessJSON(json []byte) HTTPResponse

HTTPResponseSuccessJSON generates an HttpResponse with the provided JSON as the HTTP response body with status code 200, sets standard headers, and stores the status to the log context

Params:
	json: JSON encoded response data

func (*Log) HTTPResponseSuccessMessage

func (l *Log) HTTPResponseSuccessMessage(message string) HTTPResponse

HTTPResponseSuccessMessage generates an HttpResponse with the provided success message with status code 200, sets standard headers, and stores the message and status to the log context

Params:
	msg: The success message

func (*Log) HTTPResponseSuccessStatusAction

func (l *Log) HTTPResponseSuccessStatusAction(action logutils.MessageActionType, dataType logutils.MessageDataType, args logutils.MessageArgs, code int) HTTPResponse

HTTPResponseSuccessStatusAction generates an HttpResponse with the provided success action message and status code, sets standard headers, and stores the message to the log context

Params:
	action: The action that is occurring
	dataType: The data type that the action is occurring on
	args: Any args that should be included in the message (nil if none)
	code: The HTTP response code to be set

func (*Log) HTTPResponseSuccessStatusBytes

func (l *Log) HTTPResponseSuccessStatusBytes(bytes []byte, contentType string, code int) HTTPResponse

HTTPResponseSuccessStatusBytes generates an HttpResponse with the provided bytes as the HTTP response body and status code, sets standard headers, and stores the status to the log context

Params:
	bytes: Response data
	contentType: Content type header string
	code: The HTTP response code to be set

func (*Log) HTTPResponseSuccessStatusJSON

func (l *Log) HTTPResponseSuccessStatusJSON(json []byte, code int) HTTPResponse

HTTPResponseSuccessStatusJSON generates an HttpResponse with the provided JSON as the HTTP response body and status code, sets standard headers, and stores the status to the log context

Params:
	json: JSON encoded response data
	code: The HTTP response code to be set

func (*Log) HTTPResponseSuccessStatusMessage

func (l *Log) HTTPResponseSuccessStatusMessage(message string, code int) HTTPResponse

HTTPResponseSuccessStatusMessage generates an HttpResponse with the provided success message and status code, sets standard headers, and stores the message and status to the log context

Params:
	msg: The success message
	code: The HTTP response code to be set

func (*Log) Info

func (l *Log) Info(message string)

Info prints the log at info level with given message

func (*Log) InfoWithDetails

func (l *Log) InfoWithDetails(message string, details logutils.Fields)

InfoWithDetails prints the log at info level with given fields and message

func (*Log) Infof

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

Infof prints the log at info level with given formatted string

func (*Log) LogError

func (l *Log) LogError(message string, err error) string

LogError prints the log at error level with given message and error

Returns combined error message as string

func (*Log) LogMessage

func (l *Log) LogMessage(level LogLevel, message string) string

LogMessage logs and returns a Message at the designated level

level: The log level (Info, Debug, Warn, Error)
message: The message to log

func (*Log) RequestComplete

func (l *Log) RequestComplete()

RequestComplete prints the context of a log object

func (*Log) RequestReceived

func (l *Log) RequestReceived()

RequestReceived prints the request context of a log object

func (*Log) SendHTTPResponse

func (l *Log) SendHTTPResponse(w http.ResponseWriter, response HTTPResponse)

SendHTTPResponse finalizes response data and sends the content of an HttpResponse to the provided http.ResponseWriter

Params:
	w: The http response writer for the active request
	response: The HttpResponse to be sent

func (*Log) SetContext

func (l *Log) SetContext(fieldName string, value interface{})

SetContext sets the provided context key to the provided value

func (*Log) SetRequestHeaders

func (l *Log) SetRequestHeaders(r *http.Request)

SetRequestHeaders sets the trace and span id headers for a request to another service

This function should always be called when making a request to another Rokwire service

func (*Log) SetResponseHeaders

func (l *Log) SetResponseHeaders(r *HTTPResponse)

SetResponseHeaders sets the trace id header for a response

This function should always be called when returning a response

func (*Log) SpanID

func (l *Log) SpanID() string

SpanID returns the Span ID of the log

func (*Log) TraceID

func (l *Log) TraceID() string

TraceID returns the Trace ID of the log

func (*Log) Warn

func (l *Log) Warn(message string)

Warn prints the log at warn level with given message

func (*Log) WarnError

func (l *Log) WarnError(message string, err error) string

WarnError prints the log at warn level with given message and error

Returns error message as string

func (*Log) WarnWithDetails

func (l *Log) WarnWithDetails(message string, details logutils.Fields)

WarnWithDetails prints the log at warn level with given details and message

func (*Log) Warnf

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

Warnf prints the log at warn level with given formatted string

type LogLevel

type LogLevel string

LogLevel represents the level of logging to be performed

const (

	// Info log level
	Info LogLevel = "Info"
	// Debug log level
	Debug LogLevel = "Debug"
	// Warn log level
	Warn LogLevel = "Warn"
	// Error log level
	Error LogLevel = "Error"
)

func LogLevelFromString

func LogLevelFromString(level string) *LogLevel

LogLevelFromString takes a string (not case-sensitive) and returns the equivalent logLevel. Options are "debug", "info", "warn", and "error"

type Logger

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

Logger struct defines a wrapper for a logger object

func NewLogger

func NewLogger(serviceName string, opts *LoggerOpts) *Logger

NewLogger is constructor for a logger object with initial configuration at the service level Params:

serviceName: A meaningful service name to be associated with all logs
opts: Configuration options for the Logger

func (*Logger) Debug

func (l *Logger) Debug(message string)

Debug prints the log at debug level with given message

func (*Logger) DebugWithFields

func (l *Logger) DebugWithFields(message string, fields logutils.Fields)

DebugWithFields prints the log at debug level with given fields and message

func (*Logger) Debugf

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

Debugf prints the log at debug level with given formatted string

func (*Logger) Error

func (l *Logger) Error(message string)

Error prints the log at error level with given message

func (*Logger) ErrorWithFields

func (l *Logger) ErrorWithFields(message string, fields logutils.Fields)

ErrorWithFields prints the log at error level with given fields and message

func (*Logger) Errorf

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

Errorf prints the log at error level with given formatted string

func (*Logger) Fatal

func (l *Logger) Fatal(message string)

Fatal prints the log with a fatal error message and stops the service instance WARNING: Only use for critical error messages that should prevent the service from running

func (*Logger) Fatalf

func (l *Logger) Fatalf(message string, args ...interface{})

Fatalf prints the log with a fatal format error message and stops the service instance WARNING: Only use for critical error messages that should prevent the service from running

func (*Logger) Info

func (l *Logger) Info(message string)

Info prints the log at info level with given message

func (*Logger) InfoWithFields

func (l *Logger) InfoWithFields(message string, fields logutils.Fields)

InfoWithFields prints the log at info level with given fields and message

func (*Logger) Infof

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

Infof prints the log at info level with given formatted string

func (*Logger) NewLog

func (l *Logger) NewLog(traceID string, request RequestContext) *Log

NewLog is a constructor for a log object

func (*Logger) NewRequestLog

func (l *Logger) NewRequestLog(r *http.Request) *Log

NewRequestLog is a constructor for a log object for a request

func (*Logger) SetLevel

func (l *Logger) SetLevel(level LogLevel)

SetLevel sets the log level for the logger to the provided level

func (*Logger) Warn

func (l *Logger) Warn(message string)

Warn prints the log at warn level with given message

func (*Logger) WarnWithFields

func (l *Logger) WarnWithFields(message string, fields logutils.Fields)

WarnWithFields prints the log at warn level with given fields and message

func (*Logger) Warnf

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

Warnf prints the log at warn level with given formatted string

type LoggerOpts

type LoggerOpts struct {
	//JSONFmt: When true, logs will be output in JSON format. Otherwise logs will be in logfmt
	JSONFmt bool
	//SensitiveHeaders: A list of any headers that contain sensitive information and should not be logged
	//				    Defaults: Authorization, Csrf
	SensitiveHeaders []string
	//SuppressRequests: A list of HttpRequestProperties of requests that should not be logged
	//					Any "Warn" or higher severity logs will still be logged.
	//					This is useful to prevent info logs from health checks and similar requests from
	//					flooding the logs
	//					All specified fields in the provided HttpRequestProperties must match for the logs
	//					to be suppressed. Empty fields will be ignored.
	SuppressRequests []HTTPRequestProperties
}

LoggerOpts provides configuration options for the Logger type

type RequestContext

type RequestContext struct {
	Method     string
	Path       string
	Headers    map[string][]string
	PrevSpanID string
}

RequestContext defines the context of an HTTP request to be logged

func (RequestContext) String

func (r RequestContext) String() string

Jump to

Keyboard shortcuts

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