rlog

package module
v0.0.0-...-6f7f3bf Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: MIT Imports: 10 Imported by: 2

README

rlog

GoDoc

Package rlog provides a reimplementation of the log package in the standard library. This package provides simple logging with loglevels and an interface quite similar to the standard library. Supported loglevels are equivalent to the syslog standard, RFC5424.

Quickstart

package main

import "git.sr.ht/~rumpelsepp/rlog"

func main() {
    rlog.SetLogLevel(log.DEBUG)
    rlog.Debugln("debug message")
    rlog.Infoln("info message")
    rlog.Noticeln("info message")
    rlog.Warnln("warn message")
    rlog.Errln("error message")
    rlog.Critln("critical message, which exits the application")
}

Documentation

Overview

Package rlog provides a partial reimplementation of the log package in the standard library. This package provides simple logging with loglevels and an interface quite similar to the standard library. The following environment variables are supported:

GO_RLOG_LEVEL GO_RLOG_JOURNAL

In addition to the SetLogLevel() methods, the loglevel can be set globally via an environment variable. The recognized values are: CRIT, ERR, WARN, NOTICE, INFO, and DEBUG. These constants are defined in RFC5435, Section 6.2.1.

For your convenience, there is a package level logger available, which can be accessed using the relevant methods. The package level logger is there for programs that do not need several loggers. It aims to simplify this particular usecase. If you need several loggers, please instantiate them as needed and do not use the package level logger.

Timestamps are not supported by design, since this is almost always not needed. On the terminal, you can pipe the output through ts(1) from moreutils. Using a logging service, the timestamps are already handled perfectly. This avoids the annoying problem of doubled timestamps in the log database, such as: Aug 10 07:24:37 host service[pid]: TIMESTAMP MESSAGE.

Index

Constants

View Source
const (
	EMERG = iota
	ALERT
	CRIT
	ERR
	WARNING
	NOTICE
	INFO
	DEBUG
)

RFC5424 Section 6.2.1

Variables

View Source
var DefaultLogLevel = INFO

DefaultLogLevel holds the default loglevel of this package. It is exported in order to be overwritable at compile time by -ldflags="-X ...".

Functions

func Crit

func Crit(v ...interface{})

Crit is the same as Crit, but for the package level default logger.

func Critf

func Critf(format string, v ...interface{})

Critf is the same as Critf, but for the package level default logger.

func Critln

func Critln(v ...interface{})

Critln is the same as Critln, but for the package level default logger.

func Debug

func Debug(v ...interface{})

Debug is the same as Debug, but for the package level default logger.

func Debugf

func Debugf(format string, v ...interface{})

Debugf is the same as Debugf, but for the package level default logger.

func Debugln

func Debugln(v ...interface{})

Debugln is the same as Debugln, but for the package level default logger.

func EnableJournalPrio

func EnableJournalPrio(enabled bool)

EnableJournalPrio is the same as EnableJournalPrio, but for the package level default logger.

func EnableLines

func EnableLines(calldepth int)

EnableLines is the same as EnableLines, but for the package level default logger.

func EnablePrefix

func EnablePrefix(enabled bool)

EnablePrefix is the same as EnablePrefix, but for the package level default logger.

func EnableStacktrace

func EnableStacktrace(enabled bool)

EnableStacktrace is the same as EnableLines, but for the package level default logger.

func Err

func Err(v ...interface{})

Err is the same as Err, but for the package level default logger.

func Errf

func Errf(format string, v ...interface{})

Errf is the same as Errf, but for the package level default logger.

func Errln

func Errln(v ...interface{})

Errln is the same as Errln, but for the package level default logger.

func GetLogLevel

func GetLogLevel() int

GetLogLevel is the same as GetLogLevel, but for the package level default logger.

func Info

func Info(v ...interface{})

Info is the same as Info, but for the package level default logger.

func Infof

func Infof(format string, v ...interface{})

Infof is the same as Infof, but for the package level default logger.

func Infoln

func Infoln(v ...interface{})

Infoln is the same as Infoln, but for the package level default logger.

func Notice

func Notice(v ...interface{})

Notice is the same as Notice, but for the package level default logger.

func Noticef

func Noticef(format string, v ...interface{})

Noticef is the same as Noticef, but for the package level default logger.

func Noticeln

func Noticeln(v ...interface{})

Noticeln is the same as Noticeln, but for the package level default logger.

func SetLogLevel

func SetLogLevel(logLevel int)

SetLogLevel is the same as SetLogLevel, but for the package level default logger.

func SetModule

func SetModule(module string)

SetModule is the same as SetModule, but for the package level default logger.

func SetWriter

func SetWriter(writer io.Writer)

SetWriter is the same as SetWriter, but for the package level default logger.

func Warning

func Warning(v ...interface{})

Warning is the same as Warning, but for the package level default logger.

func Warningf

func Warningf(format string, v ...interface{})

Warningf is the same as Warningf, but for the package level default logger.

func Warningln

func Warningln(v ...interface{})

Warningln is the same as Warningln, but for the package level default logger.

Types

type Logger

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

Logger is the handle to be used for emitting logmessages. Do not instantiate it manually, use NewLogger().

func NewLogger

func NewLogger(w io.Writer) *Logger

NewLogger creates a fresh instance if Logger. The environment variable GO_RLOG_LEVEL is checked whether it is equal to the strings EMERG, ALERT, CRIT, ERR, WARNING, INFO, DEBUG. In this case, the loglevel of the returned Logger is set accordingly. Otherwise, DefaultLogLevel is used. Logger by default logs to stderr.

func (*Logger) Crit

func (l *Logger) Crit(v ...interface{})

Crit logs a message and terminates the program with exit code 1.

func (*Logger) Critf

func (l *Logger) Critf(format string, v ...interface{})

Critf logs a message and terminates the program with exit code 1. It accepts a Printf style format string.

func (*Logger) Critln

func (l *Logger) Critln(v ...interface{})

Critln is the same as Crit.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug logs a message with priority debug. A newline is appended.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf logs a message wit priority debug. It accepts a Printf style format string.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln is the same as Debug.

func (*Logger) EnableJournalPrio

func (l *Logger) EnableJournalPrio(enabled bool)

EnableJournalPrio enables priority prefixes like <1>. If logging to systemd-journal(8), the priority is parsed and stored along the message.

func (*Logger) EnableLines

func (l *Logger) EnableLines(calldepth int)

EnableLines enables file names and line numbers in the log output.

func (*Logger) EnablePrefix

func (l *Logger) EnablePrefix(enabled bool)

EnablePrefix enables or disables the logging prefix, for instance "[d]".

func (*Logger) EnableStacktrace

func (l *Logger) EnableStacktrace(enabled bool)

EnableStacktrace enables printing a stacktrace for ERR and CRIT messages.

func (*Logger) Err

func (l *Logger) Err(v ...interface{})

Err logs a message with loglevel err. A newline is appended.

func (*Logger) Errf

func (l *Logger) Errf(format string, v ...interface{})

Errf logs a message wit loglevel err. It accepts a Printf style format string.

func (*Logger) Errln

func (l *Logger) Errln(v ...interface{})

Errln is the same as Err.

func (*Logger) GetLogLevel

func (l *Logger) GetLogLevel() int

GetLogLevel retrieves the loglevel from the logger.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info logs a message with priority info. A newline is appended.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof logs a message wit priority info. It accepts a Printf style format string.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln is the same as Infoln.

func (*Logger) Notice

func (l *Logger) Notice(v ...interface{})

Notice logs a message with priority notice. A newline is appended.

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{})

Noticef logs a message wit priority notice. It accepts a Printf style format string.

func (*Logger) Noticeln

func (l *Logger) Noticeln(v ...interface{})

Noticeln is the same as Notice.

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(logLevel int)

SetLogLevel sets the loglevel of the logger. Please use the exported constants to set the loglevel accordingly.

func (*Logger) SetModule

func (l *Logger) SetModule(module string)

SetModule sets the prefix of the logger.

func (*Logger) SetWriter

func (l *Logger) SetWriter(writer io.Writer)

SetWriter sets the output destination for the logger.

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

Warning logs a message with priority warning. A newline is appended.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf logs a message wit priority warning. It accepts a Printf style format string.

func (*Logger) Warningln

func (l *Logger) Warningln(v ...interface{})

Warningln is the same as Warning.

Jump to

Keyboard shortcuts

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