logger

package
v0.0.0-...-92eee8c Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 8 Imported by: 5

Documentation

Index

Constants

View Source
const (
	TypeMetric = "metric"
)

Variables

This section is empty.

Functions

func InList

func InList(haystack []Severity, needle Severity) bool

Types

type AppendableContext

type AppendableContext struct {
	Name    string      // Name for the context
	Context interface{} // Actual context information.
}

AppendableContext is used to provide better naming to the parent context.

func NewContext

func NewContext(name string, value interface{}) *AppendableContext

NewContext creates a new AppendableContext. This can be added as a context to the Contextual logger to provide a better key/name for the log.

type AppendableContextualEntry

type AppendableContextualEntry struct {
	Context interface{}   // The context to return
	Parent  []interface{} // Any parent data.
	Log     interface{}   // The log to return
}

AppendableContextualEntry describes a context to which information can be appended. It implements ContextualEntry. It is also the default context for the logger.

Usage: ```

 l.Log(
	logger.SeverityInfo,
	NewContextualPayload("my log"),
 )

```

For more details, see SetContext.

func NewContextualPayload

func NewContextualPayload(log interface{}) *AppendableContextualEntry

NewContextualPayload creates a new log payload for a Contextual logger.

func (*AppendableContextualEntry) Add

func (e *AppendableContextualEntry) Add(context interface{}) *AppendableContextualEntry

Add sets the current context

func (*AppendableContextualEntry) GetContext

func (e *AppendableContextualEntry) GetContext() interface{}

func (*AppendableContextualEntry) GetLog

func (e *AppendableContextualEntry) GetLog() interface{}

func (*AppendableContextualEntry) SetContext

func (e *AppendableContextualEntry) SetContext(ctx interface{})

SetContext sets the context for the current entry. Note that this entry may already have a context. Therefore, appending is always necessary.

If the context provided is a map, then we add the map keys to the currently provided context.

If the context provided is *not* a map, it adds the context into a "Parent" key.

type Contextual

type Contextual struct {
	Logger
	Context interface{}
}

Contextual allows the user to input extra context information into their logs. It takes in a logger that actually does the logging work, but amends the payload to include some context information.

To use the Contextual, wrap another .

For example:

logger := &Contextual{
   Logger: baseLogger,
   Context: map[string]interface{} {...}
}

... logger.Log(LogSeverityInfo, "Some log") should present a log that has:

{
   Context: ...
   Log: "Some log"
}

In the case that the provided log is a struct that implements the ContextualLogEntry interface, it will instead use the SetContext method.

logger.Log(LogSeverityInfo, LogMetricEntry{...})

{
   ... parts of LogMetricEntry
   Context: {...}  // This variable is defined in LogMetricEntry and is used to store the Context for that entry.
}

func (*Contextual) Log

func (l *Contextual) Log(severity Severity, payload interface{})

type ContextualEntry

type ContextualEntry interface {
	SetContext(interface{})
	GetContext() interface{}
	GetLog() interface{}
}

ContextualEntry is a log that can have a context. It will be used by the Contextual logger to insert a context. This is if the log should have a specific structure regardless of whether a context is provided or not.

type Default

type Default interface {
	SetDefault()
}

Default defines a log that is able to set defaults

type Google

type Google struct {
	// BaseEntry is used to define parameters other than Severity and Payload on the Google Logger.
	BaseEntry logging.Entry
	BlockList []Severity

	// Logger here is what is actually doing the logging work. This interface is primarily for testing purposes.
	// For example, we need to make sure that the logger discards all debug logs.
	Logger interface {
		Log(entry logging.Entry)
	}
}

Google logs to the Google cloud console logging platform.

func NewGoogle

func NewGoogle(client *logging.Client, name string) *Google

NewGoogle returns a new Google logger using the provided client and name. Note that this method creates a new *logging.Logger which is expensive (as each *logging.Logger can store up to 1000 entries and 1 MB of data before sending the logs and clearing its own cache).

func (*Google) Log

func (l *Google) Log(severity Severity, payload interface{})

Log an entry to the Google cloud logging console.

func (*Google) WithBaseEntry

func (l *Google) WithBaseEntry(b logging.Entry) *Google

WithBaseEntry populates the base entry for the google log, which will be used for all google logs.

func (*Google) WithBlockList

func (l *Google) WithBlockList(s ...Severity) *Google

type GoogleHTTPRequest

type GoogleHTTPRequest struct {
	Logger
	LogRequest *logging.HTTPRequest
	// contains filtered or unexported fields
}

GoogleHTTPRequest is a logger that stores information into the LogRequest variable. It should be used in conjunction with the Google logger.

e.g.,

req := &logging.HTTPRequest{Request: req}

logger := &logger.GoogleHTTPRequest{
   Logger: &MultiLogger{ // Can be any logger as long as Google is somewhere in here.
      Loggers: []Logger{
         &logger.Standard{}, // or ContextLogger,
         logger.NewGoogle(...).WithBaseEntry(req)
      },
      LogRequest: req,
   }
}

func (*GoogleHTTPRequest) SetCached

func (l *GoogleHTTPRequest) SetCached(cached bool)

SetCached - see HTTPRequest.SetCached

func (*GoogleHTTPRequest) SetLatency

func (l *GoogleHTTPRequest) SetLatency()

SetLatency - see HTTPRequest.SetLatency

func (*GoogleHTTPRequest) SetStatus

func (l *GoogleHTTPRequest) SetStatus(status int)

SetStatus - see HTTPRequest.SetStatus

func (*GoogleHTTPRequest) StartTimer

func (l *GoogleHTTPRequest) StartTimer()

StartTimer - see HTTPRequest.StartTimer

type HTTPRequest

type HTTPRequest interface {
	Logger

	// SetStatus sets the status in the log as the response status of the API
	SetStatus(status int)

	// SetCached sets the status of the response (whether the response that was sent to the client was cached or dynamic).
	SetCached(cached bool)

	// StartTimer starts the timer for latency checking
	StartTimer()

	// SetLatency sets the latency of the request. This requires StartTimer to be called first.
	SetLatency()
}

HTTPRequest is specific to handling request information. It allows the routes to quickly append information such as status / latency / whether the response was cached or not to the log

type HTTPRequestContext

type HTTPRequestContext struct {
	*logging.HTTPRequest
}

HTTPRequestContext helps with marshalling the *logging.HTTPRequest from Google. This can be used for the logger.Contextual so that the context is a proper JSON string.

func NewHTTPRequestContext

func NewHTTPRequestContext(req *logging.HTTPRequest) *HTTPRequestContext

func (*HTTPRequestContext) MarshalJSON

func (c *HTTPRequestContext) MarshalJSON() ([]byte, error)

MarshalJSON allows for custom JSON marshalling.

type Logger

type Logger interface {
	Log(severity Severity, payload interface{})
}

Logger logs with severity.

type MetricEntry

type MetricEntry struct {
	// Type is always "metric"
	Type string

	// Name is the name of the metric.
	Name string

	// Namespace should resolve to [connect, http] in this case
	Namespace []string

	// Value is the specific value of the metric
	Value int

	// Description of the metric, if any.
	Description string

	// Unit is the unit that the metric is counted in. (e.g., count, ms)
	Unit string

	// Any other context information.
	Context interface{}
}

MetricEntry is used to create a specific type of log for use in generating metrics through a cloud logging platform. To use it, create new functions, for example:

NewXXXLogMetric(value int) LogMetric {
   return LogMetric{
     Type: "metric",
     ....
   }
}

func (*MetricEntry) GetContext

func (l *MetricEntry) GetContext() interface{}

func (*MetricEntry) GetLog

func (l *MetricEntry) GetLog() interface{}

func (*MetricEntry) SetContext

func (l *MetricEntry) SetContext(context interface{})

SetContext is used to implement the ContextualEntry interface

func (*MetricEntry) SetDefault

func (l *MetricEntry) SetDefault()

type Multi

type Multi struct {
	Loggers []Logger
}

Multi will log to multiple outputs.

func (*Multi) Log

func (l *Multi) Log(severity Severity, payload interface{})

Log will log the payload into all provided loggers

type Severity

type Severity string
const (
	// SeverityDebug is for local development purposes only. If Debug is set to false in the CLI, the loggers should
	// ignore this.
	SeverityDebug Severity = "Debug"

	// SeverityInfo
	// These log lines contain useful information about the state or result of a current action.  Most logs with these
	// lines will typically contain the request and response status of a served request.
	SeverityInfo Severity = "Info"

	// SeverityNotice
	// These log lines typically contain information that you want specifically to stand out amongst the other lines.
	// These should be helpful messages. No redundant lines.
	SeverityNotice Severity = "Notice"

	// SeverityWarning
	// These log lines should contain information about an error that was encountered and recovered. There should also
	// be a corresponding error message that this resolved. These lines should also contain information that can allow
	//us to take preventative efforts for possible future errors.
	SeverityWarning Severity = "Warning"

	// SeverityError
	// All errors encountered should be logged with this level.
	SeverityError Severity = "Error"

	// SeverityCritical
	// Any system level error encountered either hardware related (file system errors, out of memory errors, database
	//errors) or in the application bootstrapping process that prevents the server from running
	SeverityCritical Severity = "Critical"
)

func (Severity) ToGoogleSeverity

func (s Severity) ToGoogleSeverity() logging.Severity

ToGoogleSeverity parses the LogSeverity into someting usable for Google.

type Standard

type Standard struct {
	PrintStack bool
	BlockList  []Severity
}

Standard prints to standard output.

Severity will be printed within square brackets (e.g., [Info]). If the payload is a string, it will print it inline.

Otherwise, it will attempt to JSON encode it (with indents) and will print the message on a separate line. If JSON marshalling fails, it will print the go representation of the payload.

func (*Standard) Log

func (l *Standard) Log(severity Severity, payload interface{})

Log logs the payload to the standard console. Debug logs will only be shown if debug is true.

type StandardContextEntry

type StandardContextEntry struct {
	Context interface{}
	Log     interface{}
}

StandardContextEntry is what will be passed into the logger by default.

func (StandardContextEntry) GetContext

func (e StandardContextEntry) GetContext() interface{}

func (StandardContextEntry) GetLog

func (e StandardContextEntry) GetLog() interface{}

func (StandardContextEntry) SetContext

func (e StandardContextEntry) SetContext(ctx interface{})

Jump to

Keyboard shortcuts

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