Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( DialTimeoutSecs = 30 WriteTimeoutSecs = 30 RetryBackoffMillis int64 = 100 MaxRetryBackoffMillis int64 = 30 * 1000 // 30 seconds )
const (
DefaultCertKey = "LOGR_DEFAULT_CERT"
)
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 GetCertPoolOrNil ¶ added in v2.0.22
GetCertPoolOrNil returns a x509.CertPool containing the cert(s) from `cert`, or from the certs specified by the env var `LOGR_DEFAULT_CERT`, either of which can be a path to a .pem or .crt file, or a base64 encoded cert.
If a cert is specified by either `cert` or `LOGR_DEFAULT_CERT`, but the cert is invalid then an error is returned.
If no certs are specified by either `cert` or `LOGR_DEFAULT_CERT`, then nil is returned.
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)
}
func NewFileTarget ¶
func NewFileTarget(opts FileOptions) *File
NewFileTarget creates a target capable of outputting log records to a rotated 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 `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)
}
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.
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) Shutdown ¶
Shutdown stops processing log records after making best effort to flush queue.
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
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)
func NewWriterTarget ¶
NewWriterTarget creates a target capable of outputting log records to an io.Writer.