target

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2020 License: MIT Imports: 8 Imported by: 16

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	logr.Basic
	// 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
package main

import (
	"fmt"
	"os"

	"github.com/mattermost/logr"
	"github.com/mattermost/logr/format"
	"github.com/mattermost/logr/target"
	"github.com/mattermost/logr/test"
)

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

	logger := lgr.NewLogger().WithField("name", "wiggin")

	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(filter logr.Filter, formatter logr.Formatter, opts FileOptions, maxQueue int) *File

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

func (*File) Shutdown

func (f *File) Shutdown(ctx context.Context) error

Shutdown flushes any remaining log records and closes the file.

func (*File) Write

func (f *File) Write(rec *logr.LogRec) error

Write converts the log record to bytes, via the Formatter, and outputs to a file.

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

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

	// 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

	// 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

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

type Syslog

type Syslog struct {
	logr.Basic
	// contains filtered or unexported fields
}

Syslog outputs log records to local or remote syslog.

Example
package main

import (
	"fmt"
	"log/syslog"
	"os"

	"github.com/mattermost/logr"
	"github.com/mattermost/logr/format"
	"github.com/mattermost/logr/target"
	"github.com/mattermost/logr/test"
)

func main() {
	lgr := &logr.Logr{}
	filter := &logr.StdFilter{Lvl: logr.Warn, Stacktrace: logr.Error}
	formatter := &format.Plain{Delim: " | "}
	params := &target.SyslogParams{Network: "", Raddr: "", Priority: syslog.LOG_WARNING | syslog.LOG_DAEMON, Tag: "logrtest"}
	t, err := target.NewSyslogTarget(filter, formatter, params, 1000)
	if err != nil {
		panic(err)
	}
	_ = lgr.AddTarget(t)

	logger := lgr.NewLogger().WithField("name", "wiggin")

	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(filter logr.Filter, formatter logr.Formatter, params *SyslogParams, maxQueue int) (*Syslog, error)

NewSyslogTarget creates a target capable of outputting log records to remote or local syslog.

func (*Syslog) Shutdown

func (s *Syslog) Shutdown(ctx context.Context) error

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

func (*Syslog) Write

func (s *Syslog) Write(rec *logr.LogRec) error

Write converts the log record to bytes, via the Formatter, and outputs to syslog.

type SyslogParams

type SyslogParams struct {
	Network  string
	Raddr    string
	Priority syslog.Priority
	Tag      string
}

SyslogParams provides parameters for dialing a syslog daemon.

type Writer

type Writer struct {
	logr.Basic
	// contains filtered or unexported fields
}

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

Example
package main

import (
	"fmt"
	"os"

	"github.com/mattermost/logr"
	"github.com/mattermost/logr/format"
	"github.com/mattermost/logr/target"
	"github.com/mattermost/logr/test"
)

func main() {
	lgr := &logr.Logr{}
	buf := &test.Buffer{}
	filter := &logr.StdFilter{Lvl: logr.Warn, Stacktrace: logr.Error}
	formatter := &format.Plain{Delim: " | "}
	t := target.NewWriterTarget(filter, formatter, buf, 1000)
	_ = lgr.AddTarget(t)

	logger := lgr.NewLogger().WithField("name", "wiggin")

	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(filter logr.Filter, formatter logr.Formatter, out io.Writer, maxQueue int) *Writer

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

func (*Writer) Write

func (w *Writer) Write(rec *logr.LogRec) error

Write converts the log record to bytes, via the Formatter, and outputs to the io.Writer.

Jump to

Keyboard shortcuts

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