logrus

package
v1.8.10 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 10 Imported by: 6

README

logrus.v1

Example

package main

import (
	"context"

	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
)

func main() {
	ctx := context.Background()

	//example use logrus
	log.SetGlobalLogger(logrus.NewLogger())

	logger := log.WithField("main_field", "example")

	logger.Info("main method.")
	//output: INFO[2021/05/14 17:15:04.757] main method. main_field=example

	ctx = logger.ToContext(ctx)

	foo(ctx)

	withoutContext()
}

func foo(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")
	//output: INFO[2021/05/14 17:15:04.757] foo method. foo_field=example main_field=example

	ctx = logger.ToContext(ctx)
	bar(ctx)
}

func bar(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")

	logger.Infof("%s method.", "bar")
	//output: INFO[2021/05/14 17:15:04.757] bar method. bar_field=example foo_field=example main_field=example
}

func withoutContext() {
	log.Info("withoutContext method")
	//output: INFO[2021/05/14 17:15:04.757] withoutContext method
}

default options:

option value
Formatter text.New()
ConsoleEnabled true
ConsoleLevel "INFO"
FileEnabled false
FileLevel "INFO"
FilePath "/tmp"
FileName "application.log"
FileMaxSize 100
FileCompress true
FileMaxAge 28
TimeFormat "2006/01/02 15:04:05.000"
ErrorFieldName "err"

The package accepts a default constructor:

// default constructor
logger := logrus.NewLogger()

Or a constructor with Options:

logger := logrus.NewLoggerWithOptions(&logrus.Options{})

Or a constructor with multiple parameters using optional pattern:

// multiple optional parameters constructor
logger := logrus.NewLogger(
	logrus.WithFormatter(json.New())
	logrus.WithConsoleEnabled(true),
	logrus.WithFilePath("/tmp"),
	...
)

This is the list of all the configuration functions supported by package:

WithFormatter

sets output format of the logs. Using TEXT/JSON/CLOUDWATCH.

import (
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1/formatter/text"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1/formatter/json"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1/formatter/cloudwatch"
	...
)

// text formatter
logger := logrus.NewLogger(logrus.WithFormatter(text.New()))

// json formatter
logger := logrus.NewLogger(logrus.WithFormatter(json.New()))

// cloudwatch formatter
logger := logrus.NewLogger(logrus.WithFormatter(cloudwatch.New()))
WithTimeFormat

sets the format used for marshaling timestamps.

// time format
logger := logrus.NewLogger(logrus.WithTimeFormat("2006/01/02 15:04:05.000"))
WithConsoleEnabled

sets whether the standard logger output will be in console. Accepts multi writing (console and file).

// console enable true
logger := logrus.NewLogger(logrus.WithConsoleEnabled(true))

// console enable false
logger := logrus.NewLogger(logrus.WithConsoleEnabled(false))
WithConsoleLevel

sets console logging level to any of these options below on the standard logger.

// log level DEBUG
logger := logrus.NewLogger(logrus.WithConsoleLevel("DEBUG"))

// log level WARN
logger := logrus.NewLogger(logrus.WithConsoleLevel("WARN"))

// log level FATAL
logger := logrus.NewLogger(logrus.WithConsoleLevel("FATAL"))

// log level ERROR
logger := logrus.NewLogger(logrus.WithConsoleLevel("ERROR"))

// log level TRACE
logger := logrus.NewLogger(logrus.WithConsoleLevel("TRACE"))

// log level INFO
logger := logrus.NewLogger(logrus.WithConsoleLevel("INFO"))
WithHook

sets a hook to be fired when logging on the logging levels.

import (
	"log/syslog"

	"github.com/americanas-go/log"
	"github.com/americanas-go/log/contrib/sirupsen/logrus.v1"
	syshooklg "github.com/sirupsen/logrus/hooks/syslog"
)

func main() {
	hook, _ := syshooklg.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
	logger := logrus.NewLogger(logrus.WithHook(hook))
	...
}
WithFileEnabled

sets whether the standard logger output will be in file. Accepts multi writing (file and console).

// file enable true
logger := logrus.NewLogger(logrus.WithFileEnabled(true))

// file enable false
logger := logrus.NewLogger(logrus.WithFileEnabled(false))
WithFilePath

sets the path where the file will be saved.

// file path
logger := logrus.NewLogger(logrus.WithFilePath("/tmp"))
WithFileName

sets the name of the file.

// file name
logger := logrus.NewLogger(logrus.WithFileName("application.log"))
WithFileMaxSize

sets the maximum size in megabytes of the log file. It defaults to 100 megabytes.

// file max size
logger := logrus.NewLogger(logrus.WithFileMaxSize(100))
WithFileCompress

sets whether the log files should be compressed.

// file compress true
logger := logrus.NewLogger(logrus.WithFileCompress(true))

// file compress false
logger := logrus.NewLogger(logrus.WithFileCompress(false))
WithFileMaxAge

sets the maximum number of days to retain old log files based on the timestamp encoded in their filename.

// file max age
logger := logrus.NewLogger(logrus.WithFileMaxAge(10))
WithErrorFieldName

sets the field name used on WithError

logger := logrus.NewLogger(logrus.WithErrorFieldName("error"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(option ...Option) log.Logger

NewLogger constructs a new Logger from provided variadic Option.

func NewLoggerWithOptions added in v1.2.0

func NewLoggerWithOptions(options *Options) log.Logger

NewLoggerWithOptions constructs a new Logger from provided Options.

Types

type Option added in v1.2.0

type Option func(options *Options)

func WithConsoleEnabled added in v1.2.0

func WithConsoleEnabled(value bool) Option

func WithConsoleLevel added in v1.2.0

func WithConsoleLevel(value string) Option

func WithErrorFieldName added in v1.5.0

func WithErrorFieldName(value string) Option

func WithFileCompress added in v1.2.0

func WithFileCompress(value bool) Option

func WithFileEnabled added in v1.2.0

func WithFileEnabled(value bool) Option

func WithFileLevel added in v1.2.0

func WithFileLevel(value string) Option

func WithFileMaxAge added in v1.2.0

func WithFileMaxAge(value int) Option

func WithFileMaxSize added in v1.2.0

func WithFileMaxSize(value int) Option

func WithFileName added in v1.2.0

func WithFileName(value string) Option

func WithFilePath added in v1.2.0

func WithFilePath(value string) Option

func WithFormatter added in v1.2.0

func WithFormatter(value logrus.Formatter) Option

func WithHook added in v1.2.0

func WithHook(value logrus.Hook) Option

func WithTimeFormat added in v1.2.0

func WithTimeFormat(value string) Option

type Options

type Options struct {
	Formatter      logrus.Formatter // formatter TEXT/JSON/CLOUDWATCH
	ErrorFieldName string           // define field name for error logging
	Time           struct {
		Format string // date and time formats
	}
	Console struct {
		Enabled bool   // enable/disable console logging
		Level   string // console log level
	}
	Hooks []logrus.Hook
	File  struct {
		Enabled  bool   // enable/disable file logging
		Level    string // file log level
		Path     string // file log path
		Name     string // log filename
		MaxSize  int    // log file max size (MB)
		Compress bool   // enabled/disable file compress
		MaxAge   int    // file max age
	}
}

Directories

Path Synopsis
formatter

Jump to

Keyboard shortcuts

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