Skip to content

fiskaly/golog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golog

-- import "github.com/fiskaly/golog"

Package golog implements structured logging for Google Cloud Platform as described in https://cloud.google.com/logging/docs/structured-logging. It allows attaching a logger to a context to print context-specific information to the log output.

Usage

func AddFields

func AddFields(ctx context.Context, newFields Fields)

AddFields adds new fields to the logger contained in ctx. Existing fields might be overwritten.

func GetFields

func GetFields() Fields

GetFields returns the fields of the logger contained in ctx.

func Alert

func Alert(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Alert outputs an alert log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Critical

func Critical(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Critical outputs a critical log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Debug

func Debug(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Debug outputs a debug log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Emergency

func Emergency(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Emergency outputs an emergency log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Error

func Error(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Error outputs an error log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Info

func Info(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Info outputs an info log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Notice

func Notice(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Notice outputs a notice log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func Stats

func Stats() (second, minute float64)

Stats returns the number of written log lines for the last second and an average over the last minute.

func Warning

func Warning(ctx context.Context, msg string, req *HTTPRequest, fields Fields)

Warning outputs a warning log message using the logger contained in ctx. If ctx doesn't contain a logger, it uses a new logger.

func WithLogger

func WithLogger(ctx context.Context, w io.Writer, fields Fields) context.Context

WithLogger creates a new logger and attaches it to the Context.

type Fields

type Fields map[string]interface{}

Fields contains additional key:value pairs to add to the log output. Any type which can be marshaled to JSON can be used as a value. Additionally, a func() string is allowed for dynamic values.

type HTTPRequest

type HTTPRequest struct {
	Method    string `json:"requestMethod,omitempty"`
	URL       string `json:"requestUrl,omitempty"`
	Status    int    `json:"status,omitempty"`
	UserAgent string `json:"userAgent,omitempty"`
	RemoteIP  string `json:"remoteIp,omitempty"`
	ServerIP  string `json:"serverIp,omitempty"`
	Referer   string `json:"referer,omitempty"`
	Latency   string `json:"latency,omitempty"`
}

An HTTPRequest contains information about an HTTP request and the response.

func FromStdHTTPRequest

func FromStdHTTPRequest(request *http.Request) *HTTPRequest

FromStdHTTPRequest extracts all data from http.Request to the custom HTTPRequest type.

type Logger

type Logger struct {
}

A Logger produces structured log output in the format defined by Google for GCP logs.

func GetLogger

func GetLogger(ctx context.Context) *Logger

GetLogger extracts the logger from a context. It returns a new empty logger if ctx doesn't contain a logger.

func NewLogger

func NewLogger(w io.Writer, fields Fields) *Logger

NewLogger creates a new logger which outputs to the given io.Writer. It allows setting fields which are included in every output log entry.

func (*Logger) AddFields

func (l *Logger) AddFields(newFields Fields)

AddFields adds new fields to the logger. Existing fields might be overwritten.

func (*Logger) Alert

func (l *Logger) Alert(msg string, req *HTTPRequest, fields Fields)

Alert outputs an alert log message.

func (*Logger) Critical

func (l *Logger) Critical(msg string, req *HTTPRequest, fields Fields)

Critical outputs a critical log message.

func (*Logger) Debug

func (l *Logger) Debug(msg string, req *HTTPRequest, fields Fields)

Debug outputs a debug log message.

func (*Logger) Emergency

func (l *Logger) Emergency(msg string, req *HTTPRequest, fields Fields)

Emergency outputs an emergency log message.

func (*Logger) Error

func (l *Logger) Error(msg string, req *HTTPRequest, fields Fields)

Error outputs an error log message.

func (*Logger) Info

func (l *Logger) Info(msg string, req *HTTPRequest, fields Fields)

Info outputs an info log message.

func (*Logger) Notice

func (l *Logger) Notice(msg string, req *HTTPRequest, fields Fields)

Notice outputs a notice log message.

func (*Logger) Warning

func (l *Logger) Warning(msg string, req *HTTPRequest, fields Fields)

Warning outputs a warning log message.