-- 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.
func AddFields(ctx context.Context, newFields Fields)
AddFields adds new fields to the logger contained in ctx. Existing fields might be overwritten.
func GetFields() Fields
GetFields returns the fields of the logger contained in ctx.
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(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(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(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(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(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(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() (second, minute float64)
Stats returns the number of written log lines for the last second and an average over the last minute.
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(ctx context.Context, w io.Writer, fields Fields) context.Context
WithLogger creates a new logger and attaches it to the Context.
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 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(request *http.Request) *HTTPRequest
FromStdHTTPRequest extracts all data from http.Request to the custom HTTPRequest type.
type Logger struct {
}
A Logger produces structured log output in the format defined by Google for GCP logs.
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(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 (l *Logger) AddFields(newFields Fields)
AddFields adds new fields to the logger. Existing fields might be overwritten.
func (l *Logger) Alert(msg string, req *HTTPRequest, fields Fields)
Alert outputs an alert log message.
func (l *Logger) Critical(msg string, req *HTTPRequest, fields Fields)
Critical outputs a critical log message.
func (l *Logger) Debug(msg string, req *HTTPRequest, fields Fields)
Debug outputs a debug log message.
func (l *Logger) Emergency(msg string, req *HTTPRequest, fields Fields)
Emergency outputs an emergency log message.
func (l *Logger) Error(msg string, req *HTTPRequest, fields Fields)
Error outputs an error log message.
func (l *Logger) Info(msg string, req *HTTPRequest, fields Fields)
Info outputs an info log message.
func (l *Logger) Notice(msg string, req *HTTPRequest, fields Fields)
Notice outputs a notice log message.
func (l *Logger) Warning(msg string, req *HTTPRequest, fields Fields)
Warning outputs a warning log message.