Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
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)
}
}
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.
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 ¶
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)
}
}
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.
type SyslogParams ¶
SyslogParams provides parameters for dialing a syslog daemon.
type Writer ¶
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)
}
Click to show internal directories.
Click to hide internal directories.