log

package
v6.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 6 Imported by: 0

Documentation

Overview

Inspired by https://www.goinggo.net/2013/11/using-log-package-in-go.html

Index

Constants

View Source
const (
	// LogLocationStdout indicates the stdout IO stream
	LogLocationStdout = "stdout"
	// LogLocationStderr indicates the stderr IO stream
	LogLocationStderr = "stderr"
	// LogLocationNull indicates the null IO stream (/dev/null)
	LogLocationNull = "null"
	//StaticFileDir is the directory that contains static html and js files.
	StaticFileDir = "/opt/traffic_monitor/static/"
)
View Source
const DebugFlags = log.Lshortfile
View Source
const DebugPrefix = "DEBUG: "
View Source
const ErrFlags = log.Lshortfile
View Source
const ErrPrefix = "ERROR: "
View Source
const EventFlags = 0
View Source
const EventPrefix = ""
View Source
const InfoFlags = log.Lshortfile
View Source
const InfoPrefix = "INFO: "
View Source
const WarnFlags = log.Lshortfile
View Source
const WarnPrefix = "WARNING: "

Variables

View Source
var (
	Debug   *log.Logger
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
	Event   *log.Logger
)

Functions

func Close

func Close(c io.Closer, context string)

Close calls `Close()` on the given Closer, and logs any error. On error, the context is logged, followed by a colon, the error message, and a newline. This is primarily designed to be used in `defer`, for example, `defer log.Close(resp.Body, "readData fetching /foo/bar")`.

func Closef

func Closef(c io.Closer, contextFormat string, v ...interface{})

Closef acts like Close, with a given format string and values, followed by a colon, the error message, and a newline. The given values are not coerced, concatenated, or printed unless an error occurs, so this is more efficient than `Close()`.

func Debugf

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

func Debugln

func Debugln(v ...interface{})

func Errorf

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

func Errorln

func Errorln(v ...interface{})

func EventRaw

func EventRaw(s string)

EventRaw writes to the event log with no prefix, and no newline. Go's Printf is slow, using this with string concatenation is by far the fastest way to log, and should be used for frequent logs.

func Eventf

func Eventf(t time.Time, format string, v ...interface{})

event log entries (TM event.log, TR access.log, etc)

func EventfRaw

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

EventfRaw writes to the event log with no prefix.

func GetLogWriter

func GetLogWriter(location LogLocation) (io.WriteCloser, error)

func Infof

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

func Infoln

func Infoln(v ...interface{})

func Init

func Init(eventW, errW, warnW, infoW, debugW io.WriteCloser)

Init initailizes the logs with the given io.WriteClosers. If `Init` was previously called, existing loggers are Closed. If you have loggers which are not Closers or which must not be Closed, wrap them with `log.NopCloser`.

func InitCfg

func InitCfg(cfg Config) error

func Logf

func Logf(logger *log.Logger, format string, v ...interface{})

Logf should generally be avoided, use the built-in Init or InitCfg and Errorf, Warnln, etc functions instead. It logs to the given logger, in the same format as the standard log functions. This should only be used in rare and unusual circumstances when the standard loggers and functions can't.

func Logln

func Logln(logger *log.Logger, v ...interface{})

Logln should generally be avoided, use the built-in Init or InitCfg and Errorf, Warnln, etc functions instead. It logs to the given logger, in the same format as the standard log functions. This should only be used in rare and unusual circumstances when the standard loggers and functions can't.

func NopCloser

func NopCloser(w io.Writer) io.WriteCloser

func StandardLogger

func StandardLogger(logger *log.Logger, prefix string) *log.Logger

StandardLogger returns a new Logger which will write the appropriate prefix for standard log Printf calls. This allows the given logger to prefix correctly when passed to third-party or standard library functions which only know about the standard Logger interface. It does this by wrapping logger's error print as a writer, and sets that as the new Logger's io.Writer.

prefix is a prefix to add, which will be added immediately before messages, but after any existing prefix on logger and the timestamp.

func Warnf

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

func Warnln

func Warnln(v ...interface{})

func Write

func Write(w io.Writer, b []byte, context string)

Write calls `Write()` on the given Writer, and logs any error. On error, the context is logged, followed by a colon, the error message, and a newline.

func Writef

func Writef(w io.Writer, b []byte, contextFormat string, v ...interface{})

Writef acts like Write, with a given format string and values, followed by a colon, the error message, and a newline. The given values are not coerced, concatenated, or printed unless an error occurs, so this is more efficient than `Write()`.

Types

type Config

type Config interface {
	ErrorLog() LogLocation
	WarningLog() LogLocation
	InfoLog() LogLocation
	DebugLog() LogLocation
	EventLog() LogLocation
}

type LogLocation

type LogLocation string

LogLocation is a location to log to. This may be stdout, stderr, null (/dev/null), or a valid file path.

Jump to

Keyboard shortcuts

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