targets

package
v2.0.21 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 17 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	DialTimeoutSecs             = 30
	WriteTimeoutSecs            = 30
	RetryBackoffMillis    int64 = 100
	MaxRetryBackoffMillis int64 = 30 * 1000 // 30 seconds
)

Variables

This section is empty.

Functions

func CreateTestLogger added in v2.0.11

func CreateTestLogger(t *testing.T, levels ...logr.Level) (logger logr.Logger, shutdown func() error)

CreateTestLogger creates a logger for unit tests. Log records are output to `(*testing.T).Log`. A new logger is returned along with a method to shutdown the new logger.

func GetCertPool

func GetCertPool(cert string) (*x509.CertPool, error)

GetCertPool returns a x509.CertPool containing the cert(s) from `cert`, which can be a path to a .pem or .crt file, or a base64 encoded cert.

Types

type File

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

File outputs log records to a file which can be log rotated based on size or age. Uses `https://github.com/natefinch/lumberjack` for rotation.

Example
lgr, _ := logr.New()
filter := &logr.StdFilter{Lvl: logr.Warn, Stacktrace: logr.Error}
formatter := &formatters.JSON{}
opts := targets.FileOptions{
	Filename:   "./logs/test_lumberjack.log",
	MaxSize:    1,
	MaxAge:     2,
	MaxBackups: 3,
	Compress:   false,
}
t := targets.NewFileTarget(opts)
_ = lgr.AddTarget(t, "test", filter, formatter, 1000)

logger := lgr.NewLogger().With(logr.String("name", "wiggin")).Sugar()

logger.Errorf("the erroneous data is %s", test.StringRnd(10))
logger.Warnf("strange data: %s", test.StringRnd(5))
logger.Debug("XXX")
logger.Trace("XXX")

err := lgr.Shutdown()
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}
Output:

func NewFileTarget

func NewFileTarget(opts FileOptions) *File

NewFileTarget creates a target capable of outputting log records to a rotated file.

func (*File) Init

func (f *File) Init() error

Init is called once to initialize the target.

func (*File) Shutdown

func (f *File) Shutdown() error

Shutdown is called once to free/close any resources. Target queue is already drained when this is called.

func (*File) Write

func (f *File) Write(p []byte, rec *logr.LogRec) (int, error)

Write outputs bytes to this file target.

type FileOptions

type FileOptions struct {
	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string `json:"filename"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"max_size"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"max_age"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"max_backups"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress"`
}

func (FileOptions) CheckValid

func (fo FileOptions) CheckValid() error

type Syslog

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

Syslog outputs log records to local or remote syslog.

Example
lgr, _ := logr.New()
filter := &logr.StdFilter{Lvl: logr.Warn, Stacktrace: logr.Error}
formatter := &formatters.Plain{Delim: " | "}
params := &targets.SyslogOptions{
	Host: "localhost",
	Port: 514,
	Tag:  "logrtest",
}
t, err := targets.NewSyslogTarget(params)
if err != nil {
	panic(err)
}
err = lgr.AddTarget(t, "syslogTest", filter, formatter, 1000)
if err != nil {
	panic(err)
}

logger := lgr.NewLogger().With(logr.String("name", "wiggin")).Sugar()

logger.Errorf("the erroneous data is %s", test.StringRnd(10))
logger.Warnf("strange data: %s", test.StringRnd(5))
logger.Debug("XXX")
logger.Trace("XXX")

err = lgr.Shutdown()
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}
Output:

func NewSyslogTarget

func NewSyslogTarget(params *SyslogOptions) (*Syslog, error)

NewSyslogTarget creates a target capable of outputting log records to remote or local syslog, with or without TLS.

func (*Syslog) Init

func (s *Syslog) Init() error

Init is called once to initialize the target.

func (*Syslog) Shutdown

func (s *Syslog) Shutdown() error

Shutdown is called once to free/close any resources. Target queue is already drained when this is called.

func (*Syslog) Write

func (s *Syslog) Write(p []byte, rec *logr.LogRec) (int, error)

Write outputs bytes to this file target.

type SyslogOptions

type SyslogOptions struct {
	IP       string `json:"ip,omitempty"` // deprecated (use Host instead)
	Host     string `json:"host"`
	Port     int    `json:"port"`
	TLS      bool   `json:"tls"`
	Cert     string `json:"cert"`
	Insecure bool   `json:"insecure"`
	Tag      string `json:"tag"`
}

SyslogOptions provides parameters for dialing a syslog daemon.

func (SyslogOptions) CheckValid

func (so SyslogOptions) CheckValid() error

type Tcp

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

Tcp outputs log records to raw socket server.

func NewTcpTarget

func NewTcpTarget(options *TcpOptions) *Tcp

NewTcpTarget creates a target capable of outputting log records to a raw socket, with or without TLS.

func (*Tcp) Init

func (tcp *Tcp) Init() error

Init is called once to initialize the target.

func (*Tcp) Shutdown

func (tcp *Tcp) Shutdown() error

Shutdown stops processing log records after making best effort to flush queue.

func (*Tcp) String

func (tcp *Tcp) String() string

String returns a string representation of this target.

func (*Tcp) Write

func (tcp *Tcp) Write(p []byte, rec *logr.LogRec) (int, error)

Write converts the log record to bytes, via the Formatter, and outputs to the socket. Called by dedicated target goroutine and will block until success or shutdown.

type TcpOptions

type TcpOptions struct {
	IP       string `json:"ip,omitempty"` // deprecated
	Host     string `json:"host"`
	Port     int    `json:"port"`
	TLS      bool   `json:"tls"`
	Cert     string `json:"cert"`
	Insecure bool   `json:"insecure"`
}

TcpOptions provides parameters for dialing a socket server.

func (TcpOptions) CheckValid

func (to TcpOptions) CheckValid() error

type Testing added in v2.0.11

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

Testing is a simple log target that writes to a (*testing.T) log.

func NewTestingTarget added in v2.0.11

func NewTestingTarget(t *testing.T) *Testing

func (*Testing) Init added in v2.0.11

func (tt *Testing) Init() error

Init is called once to initialize the target.

func (*Testing) Shutdown added in v2.0.11

func (tt *Testing) Shutdown() error

Shutdown is called once to free/close any resources. Target queue is already drained when this is called.

func (*Testing) Write added in v2.0.11

func (tt *Testing) Write(p []byte, rec *logr.LogRec) (int, error)

Write outputs bytes to this file target.

type Writer

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

Writer outputs log records to any `io.Writer`.

Example
lgr, _ := logr.New()
buf := &test.Buffer{}
filter := &logr.StdFilter{Lvl: logr.Warn, Stacktrace: logr.Error}
formatter := &formatters.Plain{Delim: " | "}
t := targets.NewWriterTarget(buf)
_ = lgr.AddTarget(t, "example", filter, formatter, 1000)

logger := lgr.NewLogger().With(logr.String("name", "wiggin")).Sugar()

logger.Errorf("the erroneous data is %s", test.StringRnd(10))
logger.Warnf("strange data: %s", test.StringRnd(5))
logger.Debug("XXX")
logger.Trace("XXX")

err := lgr.Shutdown()
if err != nil {
	fmt.Fprintln(os.Stderr, err)
}

output := buf.String()
fmt.Println(output)
Output:

func NewWriterTarget

func NewWriterTarget(out io.Writer) *Writer

NewWriterTarget creates a target capable of outputting log records to an io.Writer.

func (*Writer) Init

func (w *Writer) Init() error

Init is called once to initialize the target.

func (*Writer) Shutdown

func (w *Writer) Shutdown() error

Shutdown is called once to free/close any resources. Target queue is already drained when this is called.

func (*Writer) Write

func (w *Writer) Write(p []byte, rec *logr.LogRec) (int, error)

Write outputs bytes to this file target.

Jump to

Keyboard shortcuts

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