apilogger

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2020 License: MIT Imports: 9 Imported by: 1

README

apilogger

Simple api logger for golang with the purpose of log basic information like api-key, clinet ip, request-id.

Installation

Run go get github.com/disturb16/apilogger

Usage

ctx := context.Background()

// spects basic information in context: api-key, x-request-id, remote-address,
// second argument is the path to log file (if needed).
logger := apilogger.New(ctx, "")

logger.Info(LogCatDebug, "This is an info message")
logger.Warn(LogCatDebug, "This is a warning message")
logger.Error(LogCatDebug, "This is an error message")

logger.Infof(LogCatDebug, "This is an info message with %s", "some variable")
logger.Warnf(LogCatDebug, "This is a warning message with format, some number: %d", 100)
logger.Errorf(LogCatDebug, "This is an error message with format, %v", errors.New("An error"))

logger.InfoWF(LogCatDebug, &Fields{"message": "my message"})
logger.WarnWF(LogCatDebug, &Fields{"warning": "my warning", "other": "another message"})
logger.ErrorWF(LogCatDebug, &Fields{"error": errors.New("my error message")})

Outputs

INFO 2020/07/31 15:11:29 location="main.go:124", requestId="", clientIp="", apiKey="", sessionId="", ms="0.022536", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is an info message"
WARNING 2020/07/31 15:11:29 location="main.go:125", requestId="", clientIp="", apiKey="", sessionId="", ms="0.107880", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is a warning message"
ERROR 2020/07/31 15:11:29 location="main.go:126", requestId="", clientIp="", apiKey="", sessionId="", ms="0.141037", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is an error message"

INFO 2020/07/31 15:11:29 location="main.go:128", requestId="", clientIp="", apiKey="", sessionId="", ms="0.161709", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is an info message with some variable"
WARNING 2020/07/31 15:11:29 location="main.go:129", requestId="", clientIp="", apiKey="", sessionId="", ms="0.184334", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is a warning message with format, some number: 100"
ERROR 2020/07/31 15:11:29 location="main.go:130", requestId="", clientIp="", apiKey="", sessionId="", ms="0.204437", function="apilogger.MyFunction", code="DBG01", type="debug", message="This is an error message with format, An error"

INFO 2020/07/31 15:11:29 location="main.go:132", requestId="", clientIp="", apiKey="", sessionId="", ms="0.227874", function="apilogger.MyFunction", code="DBG01", type="debug", message="my message"
WARNING 2020/07/31 15:11:29 location="main.go:133", requestId="", clientIp="", apiKey="", sessionId="", ms="0.251661", function="apilogger.MyFunction", code="DBG01", type="debug", other="another message", warning="my warning"
ERROR 2020/07/31 15:11:29 location="main.go:134", requestId="", clientIp="", apiKey="", sessionId="", ms="0.280195", function="apilogger.MyFunction", code="DBG01", type="debug", error="my error message"

Documentation

Index

Constants

View Source
const (
	// APIKEY is the context key used to
	// access the api-key request header value
	APIKEY string = "api-key"

	// RequestIDKey is the context key used to
	// access the x-request-id request header value
	RequestIDKey string = "x-request-id"

	// RemoteAddrKey is the context key used to
	// access the remote-address request header value
	RemoteAddrKey string = "remote-address"

	// SessionIDKey is the context key used to
	// access the session request header value
	SessionIDKey string = "session"
)

Variables

View Source
var (
	// LogCatStartUp usage: service startup logs
	LogCatStartUp = LogCat{Code: "STT01", Type: "service_startup"}

	// LogCatHealth usage: health check logs
	LogCatHealth = LogCat{Code: "HTH01", Type: "health_check"}

	// LogCatRouterInit usage: api router setup logs
	LogCatRouterInit = LogCat{Code: "RTR01", Type: "router_initialization"}

	// LogCatRepoInit usage: repository setup logs
	LogCatRepoInit = LogCat{Code: "RPO01", Type: "repo_initialization"}

	// LogCatRepoOutput usage: logs relating to
	// the results of repository layer functions/methods
	LogCatRepoOutput = LogCat{Code: "RPO02", Type: "repository_output"}

	// LogCatReadConfig usage: configuration reading/init logs
	LogCatReadConfig = LogCat{Code: "CNF01", Type: "read_configuration"}

	// LogCatDatastoreConnect usage: datastore connect
	// logs (more specific than LogCatRepoInit)
	LogCatDatastoreConnect = LogCat{Code: "DTA01", Type: "datastore_connect"}

	// LogCatDatastoreClose usage: close datastore connection logs
	LogCatDatastoreClose = LogCat{Code: "DTA02", Type: "datastore_close"}

	// LogCatDatabase usage: datastore interaction
	// logs, e.g. query exec or record read errors
	LogCatDatabase = LogCat{Code: "DTA03", Type: "datastore_interaction"}

	// LogCatMarshallJSON usage: JSON marshalling logs,
	// i.e. logs for events when converting data or data structures to JSON
	LogCatMarshallJSON = LogCat{Code: "JSN01", Type: "marshall_json"}

	// LogCatUnmarshalReq usage: request decoding logs,
	// i.e. events relating to the deserialization of incoming requests
	LogCatUnmarshalReq = LogCat{Code: "REQ01", Type: "unmarshal_request_payload"}

	// LogCatAPIKey usage: logs regarding the api-key of an incoming request
	LogCatAPIKey = LogCat{Code: "REQ02", Type: "request_apikey"}

	// LogCatReqPath usage: logs of the path of an incoming request
	LogCatReqPath = LogCat{Code: "REQ03", Type: "request_path"}

	// LogCatReqValid usage: logs regarding validating an incoming request
	LogCatReqValid = LogCat{Code: "REQ04", Type: "request_validation"}

	// LogCatDebug usage: debug-related logs
	LogCatDebug = LogCat{Code: "DBG01", Type: "debug"}

	// LogCatTypeConv usage: logs regarding the conversion
	// of data from one type to another e.g. string to integer conversion errors
	LogCatTypeConv = LogCat{Code: "CNV01", Type: "type_conversion"}

	// LogCatDateTimeParse usage: date/time string parsing logs
	LogCatDateTimeParse = LogCat{Code: "PRS01", Type: "datetime_parse"}

	// LogCatServiceOutput usage: logs relating to the
	// results of service layer functions/methods
	LogCatServiceOutput = LogCat{Code: "SRV01", Type: "service_layer_output"}

	// LogCatInputValidation usage: logs relating to validating
	// the input parameters of a method/function
	LogCatInputValidation = LogCat{Code: "VAL01", Type: "method_input_validation"}

	// LogCatTemplateExec usage: logs relating to "executing" on a golang template
	LogCatTemplateExec = LogCat{Code: "TMP01", Type: "template_execution"}

	// LogCatCacheInit usage: logs relating to cache initialization
	LogCatCacheInit = LogCat{Code: "CCH01", Type: "cache_initialize"}

	// LogCatCacheRead usage: logs relating to reading from a cache
	LogCatCacheRead = LogCat{Code: "CCH02", Type: "cache_read"}

	// LogCatCacheWrite usage: logs relating to writing to a cache
	LogCatCacheWrite = LogCat{Code: "CCH03", Type: "cache_write"}

	// LogCatImplStatus usage: logs relating to the implementation progress of any particular
	// service functionality. E.g. an endpoint exists and is hit, but the intended functionality has
	// not yet been implemented
	LogCatImplStatus = LogCat{Code: "STS01", Type: "implementation_status"}

	// LogCatUncategorized usage: for temporary use in development if there has not yet
	// been an adequate category added. If this is the case, please
	// create a pull request to the logging repo with an appropriate new
	// logging category
	LogCatUncategorized = LogCat{Code: "UNCAT", Type: "uncategorized"}
)

All log categories. Any additions or removals to be registered in the `allLogCats` slice below.

Functions

This section is empty.

Types

type Fields added in v1.1.0

type Fields map[string]interface{}

type LogCat

type LogCat struct {
	Code string
	Type string
}

LogCat holds data regarding a logging category. Log categories can and should be used for all logging levels (INFO, WARN, ERROR)

type Logger

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

Logger struct

func New

func New(ctx context.Context, outPath string) *Logger

New returns a new Logger instance.

func (*Logger) Error

func (l *Logger) Error(logCat LogCat, v ...interface{})

Error prints message at error level.

func (*Logger) ErrorWF added in v1.1.0

func (l *Logger) ErrorWF(logCat LogCat, fields *Fields)

ErrorWF prints message at error level using Fields with multiple key=value pairs.

func (*Logger) Errorf

func (l *Logger) Errorf(logCat LogCat, format string, v ...interface{})

Errorf prints message at error level.

func (*Logger) Fatal

func (l *Logger) Fatal(logCat LogCat, v ...interface{})

Fatal prints and calls os.exit(1).

func (*Logger) FatalWF added in v1.1.0

func (l *Logger) FatalWF(logCat LogCat, fields *Fields)

FatalWF prints and calls os.exit(1) with multiple key=value pairs.

func (*Logger) Fatalf

func (l *Logger) Fatalf(logCat LogCat, format string, v ...interface{})

Fatalf prints and calls os.exit(1).

func (*Logger) Info

func (l *Logger) Info(logCat LogCat, v ...interface{})

func (*Logger) InfoWF added in v1.1.0

func (l *Logger) InfoWF(logCat LogCat, fields *Fields)

InfoWF prints message using Fields struct to pass multiple key=value pairs.

func (*Logger) Infof

func (l *Logger) Infof(logCat LogCat, format string, v ...interface{})

Infof prints a message using the specified format.

func (*Logger) Warn

func (l *Logger) Warn(logCat LogCat, v ...interface{})

func (*Logger) WarnWF added in v1.1.0

func (l *Logger) WarnWF(logCat LogCat, fields *Fields)

WarnWF prints message with fields to use multiple key=value pairs.

func (*Logger) Warnf

func (l *Logger) Warnf(logCat LogCat, format string, v ...interface{})

Jump to

Keyboard shortcuts

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