log

package
v0.0.0-...-a807e99 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

GoDoc

log

log is a simple wrapper for logrus.

Usage

To start use the log package add import:

...
import (
  "git.ooo.ua/pub/armory/log" // imports as package "log"
)
...
  • Fill config structure:
Field Type Description
AppName string identifier of the app
Level string level of the logging output
Sentry string dsn string for the sentry hook
AddTrace bool enable the inclusion of the file name and line number in the log
JSON bool enable json formatted output
  • Call init function:
logger, err := log.Init(config)

A once-initialized logrus.Entry can then be used anywhere. To access it, use the getter:

logger := log.Get()
Example
package main

import (
    "fmt"
    "git.ooo.ua/pub/armory/log" // imports as package "log"
)

var config = log.Config{
    AppName: "my-app",
    Level: "warn",
    Sentry: "http://....",
    AddTrace: true,  
}

logger, err := log.Init(config)
if err != nil {
    fmt.Println(err)
    panic("unable to init log Entry")
}

logger.Info("log initialized")
logger.Error("log test error")

logger2 := log.Get()

fmt.Println(logger == logger2) // true

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default *logrus.Entry // nolint:gochecknoglobals

Default is a log.Entry singleton. DEPRECATED

Functions

func AddFilenameHook

func AddFilenameHook()

AddFilenameHook adds hook that includes filename and line number into the log.

func AddSentryHook

func AddSentryHook(dsn string)

AddSentryHook adds hook that sends all error, fatal and panic log lines to the sentry service.

func DefaultForRequest

func DefaultForRequest(r *http.Request) *logrus.Entry

DefaultForRequest returns default logger with included http.Request details.

func Get

func Get() *logrus.Entry

Get is a getter for the `logrus.Entry` singleton.

func GetLogEntry

func GetLogEntry(r *http.Request) logrus.FieldLogger

func IncludeRequest

func IncludeRequest(log *logrus.Entry, r *http.Request) *logrus.Entry

IncludeRequest includes http.Request details into the log.Entry.

func Init

func Init(config Config) (*logrus.Entry, error)

Init initializes a default logger configuration by passed configuration.

func LogEntrySetField

func LogEntrySetField(r *http.Request, key string, value interface{})

LogEntrySetField add field to logger in request context.

func LogEntrySetFields

func LogEntrySetFields(r *http.Request, fields map[string]interface{})

LogEntrySetField add fields to logger in request context.

func NewRequestLogger

func NewRequestLogger(logger *logrus.Logger) func(next http.Handler) http.Handler

NewRequestLogger returns new RequestLogger middleware.

Types

type Config

type Config struct {
	// AppName identifier of the app.
	AppName string `json:"app_name" yaml:"app_name" mapstructure:"log.app_name"`
	// Level is a string representation of the `logrus.Level`.
	Level string `json:"level" yaml:"level" mapstructure:"log.level"`
	// Sentry is a DSN string for sentry hook.
	Sentry string `json:"sentry" yaml:"sentry" mapstructure:"log.sentry"`
	// AddTrace enable adding of the filename field into log.
	AddTrace bool `json:"add_trace" yaml:"add_trace" mapstructure:"log.add_trace"`
	// JSON enable json formatted output.
	JSON bool `json:"json" yaml:"json" mapstructure:"log.json"`
}

Config is a options for the initialization of the default logrus.Entry.

type NConfig

type NConfig struct {
	// AppName identifier of the app.
	AppName string `yaml:"app_name"`
	// Level is a string representation of the `logrus.Level`.
	Level noble.Secret `yaml:"level"`
	// Sentry is a DSN string for sentry hook.
	Sentry noble.Secret `yaml:"sentry"`
	// AddTrace enable adding of the filename field into log.
	AddTrace bool `yaml:"add_trace"`
	// JSON enable json formatted output.
	JSON bool `yaml:"json"`
}

NConfig is a options for the initialization of the default logrus.Entry. N stands for Noble.

func (NConfig) Validate

func (cfg NConfig) Validate() error

Validate is an implementation of Validatable interface from ozzo-validation.

type RequestLogger

type RequestLogger struct {
	Logger *logrus.Logger
}

RequestLogger is a simple, but powerful implementation of a custom structured logger backed on logrus. I encourage users to copy it, adapt it and make it their own. Also take a look at https://github.com/pressly/lg for a dedicated pkg based on this work, designed for context-based http routers.

func (*RequestLogger) NewLogEntry

func (l *RequestLogger) NewLogEntry(r *http.Request) middleware.LogEntry

NewLogEntry sets log fields for http.Request.

type RequestLoggerEntry

type RequestLoggerEntry struct {
	Logger logrus.FieldLogger
}

RequestLoggerEntry is an implementation http.Request logger baked with logrus.

func (*RequestLoggerEntry) Panic

func (l *RequestLoggerEntry) Panic(v interface{}, stack []byte)

func (*RequestLoggerEntry) Write

func (l *RequestLoggerEntry) Write(status, bytes int, _ http.Header, elapsed time.Duration, _ interface{})

Jump to

Keyboard shortcuts

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