Documentation ¶
Overview ¶
Inspired by https://www.goinggo.net/2013/11/using-log-package-in-go.html
Index ¶
- Constants
- Variables
- func Close(c io.Closer, context string)
- func Closef(c io.Closer, contextFormat string, v ...interface{})
- func Debugf(format string, v ...interface{})
- func Debugln(v ...interface{})
- func Errorf(format string, v ...interface{})
- func Errorln(v ...interface{})
- func EventRaw(s string)
- func Eventf(t time.Time, format string, v ...interface{})
- func EventfRaw(format string, v ...interface{})
- func GetLogWriter(location LogLocation) (io.WriteCloser, error)
- func GetLogWriters(cfg Config) (io.WriteCloser, io.WriteCloser, io.WriteCloser, io.WriteCloser, io.WriteCloser, ...)
- func Infof(format string, v ...interface{})
- func Infoln(v ...interface{})
- func Init(eventW, errW, warnW, infoW, debugW io.WriteCloser)
- func InitCfg(cfg Config) error
- func Logf(logger *log.Logger, format string, v ...interface{})
- func Logln(logger *log.Logger, v ...interface{})
- func NopCloser(w io.Writer) io.WriteCloser
- func StandardLogger(logger *log.Logger, prefix string) *log.Logger
- func Warnf(format string, v ...interface{})
- func Warnln(v ...interface{})
- func Write(w io.Writer, b []byte, context string)
- func Writef(w io.Writer, b []byte, contextFormat string, v ...interface{})
- type Config
- type LogLocation
Constants ¶
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/" )
const DebugFlags = log.Lshortfile
const DebugPrefix = "DEBUG: "
const ErrFlags = log.Lshortfile
const ErrPrefix = "ERROR: "
const EventFlags = 0
const EventPrefix = ""
const InfoFlags = log.Lshortfile
const InfoPrefix = "INFO: "
const WarnFlags = log.Lshortfile
const WarnPrefix = "WARNING: "
Variables ¶
Functions ¶
func Close ¶
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 ¶
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 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 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 GetLogWriters ¶
func GetLogWriters(cfg Config) (io.WriteCloser, io.WriteCloser, io.WriteCloser, io.WriteCloser, io.WriteCloser, error)
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 Logf ¶
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 ¶
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 StandardLogger ¶
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 Write ¶
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.
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.