Documentation ¶
Overview ¶
Package log implements a logger that logs to a file or any io.Writer. If the io.Writer looks like syslog, then certain parts of the log line will not be generated, as syslog is expected to cover those.
This logger looks like syslog, in that it allows for specifying severities when logging. Unlike syslog, however, instead of setting a default severity for logging, a severity threshold is specified at logger creation time that limits logging to that severity and anything more important. This provides a convenient way to control the verbosity of logging.
The intention is to enable this to be a drop-in replacement, interface-wise, for the standard log/syslog or log. In addition, it provides most of the methods offered by the standard log interface.
You may create a new logger using New() or NewFromFile(), or, if you want to use the default logger, just call methods directly on the package, which will write to os.Stderr.
A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.
The interface for this logging package is in an alpha state. That is, it may have some non backward compatible changes in upcoming minor releases.
Installation:
go get github.com/cuberat/go-log
Index ¶
- func Alert(m string) error
- func Alertf(format string, v ...interface{}) error
- func Crit(m string) error
- func Critf(format string, v ...interface{}) error
- func Debug(m string) error
- func Debugf(format string, v ...interface{}) error
- func Emerg(m string) error
- func Emergf(format string, v ...interface{}) error
- func Err(m string) error
- func Errf(format string, v ...interface{}) error
- func Errorf(format string, v ...interface{}) error
- func ErrorfDepth(call_depth int, format string, v ...interface{}) error
- func Fatal(v ...interface{})
- func Fatalf(format string, v ...interface{})
- func Fatalln(v ...interface{})
- func Info(m string) error
- func Infof(format string, v ...interface{}) error
- func Notice(m string) error
- func Noticef(format string, v ...interface{}) error
- func Panic(v ...interface{})
- func Panicf(format string, v ...interface{})
- func Panicln(v ...interface{})
- func Print(v ...interface{}) error
- func Printf(format string, v ...interface{}) error
- func Println(v ...interface{}) error
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- func SetSeverityThreshold(sev_thresh Severity)
- func SetTimestampFunc(f TimestampFunc)
- func Warning(m string) error
- func Warningf(format string, v ...interface{}) error
- type Logger
- func (l *Logger) Alert(m string) error
- func (l *Logger) Alertf(format string, v ...interface{}) error
- func (l *Logger) Crit(m string) error
- func (l *Logger) Critf(format string, v ...interface{}) error
- func (l *Logger) Debug(m string) error
- func (l *Logger) Debugf(format string, v ...interface{}) error
- func (l *Logger) Emerg(m string) error
- func (l *Logger) Emergf(format string, v ...interface{}) error
- func (l *Logger) Err(m string) error
- func (l *Logger) Errf(format string, v ...interface{}) error
- func (l *Logger) Errorf(format string, v ...interface{}) error
- func (l *Logger) ErrorfDepth(call_depth int, format string, v ...interface{}) error
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalln(v ...interface{})
- func (l *Logger) Info(m string) error
- func (l *Logger) Infof(format string, v ...interface{}) error
- func (l *Logger) Notice(m string) error
- func (l *Logger) Noticef(format string, v ...interface{}) error
- func (l *Logger) Panic(v ...interface{})
- func (l *Logger) Panicf(format string, v ...interface{})
- func (l *Logger) Panicln(v ...interface{})
- func (l *Logger) Print(v ...interface{}) error
- func (l *Logger) Printf(format string, v ...interface{}) error
- func (l *Logger) Println(v ...interface{}) error
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) SetPrefix(prefix string)
- func (l *Logger) SetSeverityThreshold(sev_thresh Severity)
- func (l *Logger) SetTimestampFunc(f TimestampFunc)
- func (l *Logger) Warning(m string) error
- func (l *Logger) Warningf(format string, v ...interface{}) error
- func (l *Logger) Write(b []byte) (int, error)
- type Severity
- type SyslogLike
- type TimestampFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alertf ¶
Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.
func Critf ¶
Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.
func Debugf ¶
Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.
func Emergf ¶
Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.
func Errf ¶
Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.
func Errorf ¶ added in v1.1.0
Returns an error like `fmt.Errorf`, but prepended with the source file name and line number.
func ErrorfDepth ¶ added in v1.1.0
Returns an error like `Errorf`, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where `ErrorDepth` is called. `ErrorDepth(0, ...)` is equivalent to calling `Errorf`.
func Fatalf ¶
func Fatalf(format string, v ...interface{})
Equivalent to Printf() followed by a call to os.Exit(1).
func Fatalln ¶
func Fatalln(v ...interface{})
Equivalent to Println() followed by a call to os.Exit(1).
func Infof ¶
Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.
func Noticef ¶
Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.
func Panicf ¶
func Panicf(format string, v ...interface{})
Equivalent to Printf() followed by a call to panic().
func Panicln ¶
func Panicln(v ...interface{})
Equivalent to Println() followed by a call to panic().
func Print ¶
func Print(v ...interface{}) error
Prints to the logger. Arguments are handled in the manner of fmt.Print.
func Println ¶
func Println(v ...interface{}) error
Prints to the logger. Arguments are handled in the manner of fmt.Println.
func SetPrefix ¶
func SetPrefix(prefix string)
Sets the prefix to add to the beginning of each log line (after the timestamp) for the default logger.
func SetSeverityThreshold ¶
func SetSeverityThreshold(sev_thresh Severity)
Sets the severity threshold for the default logger. Anything less important (further down the list of severities) will not be logged.
func SetTimestampFunc ¶
func SetTimestampFunc(f TimestampFunc)
Sets the timestamp generator function for the default logger. This will be called to generate the timestamp for each log line.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method or to the Writer's severity-related method, if it implements the SyslogLike interface. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.
func NewFromFile ¶
Creates a logger that will write to the specified file path, with the given severity threshold and prefix string
func (*Logger) Alertf ¶
Logs a message with severity LOG_ALERT. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Critf ¶
Logs a message with severity LOG_CRIT. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Debugf ¶
Logs a message with severity LOG_DEBUG. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Emergf ¶
Logs a message with severity LOG_EMERG. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Errf ¶
Logs a message with severity LOG_ERR. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Errorf ¶ added in v1.1.0
Returns an error like `fmt.Errorf`, but prepended with the source file name and line number.
func (*Logger) ErrorfDepth ¶ added in v1.1.0
Returns an error like `Errorf`, but allows you to specify a call depth. For instance, passing a value of 1 as the call_depth will cause the source file and line number to correspond to where the enclosing function is called, instead of where `ErrorDepth` is called. `ErrorDepth(0, ...)` is equivalent to calling `Errorf`.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Equivalent to Print() followed by a call to os.Exit(1).
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(v ...interface{})
Equivalent to Println() followed by a call to os.Exit(1).
func (*Logger) Infof ¶
Logs a message with severity LOG_INFO. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Noticef ¶
Logs a message with severity LOG_NOTICE. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Panic ¶
func (l *Logger) Panic(v ...interface{})
Equivalent to Print() followed by a call to panic().
func (*Logger) Panicln ¶
func (l *Logger) Panicln(v ...interface{})
Equivalent to Println() followed by a call to panic().
func (*Logger) SetPrefix ¶
Sets the prefix to add to the beginning of each log line (after the timestamp).
func (*Logger) SetSeverityThreshold ¶
Sets the severity threshold. Anything less important (further down the list of severities) will not be logged.
func (*Logger) SetTimestampFunc ¶
func (l *Logger) SetTimestampFunc(f TimestampFunc)
Sets the timestamp generator function. This will be called to generate the timestamp for each log line.
type Severity ¶
type Severity int
The Severity type.
const ( LOG_EMERG Severity = iota LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG )
Severities to be passed to New() or SetSeverityThreshold().
func SeverityFromString ¶
Converts a severity name to a Severity that can be passed to New() and SetSeverityThreshold(). This is useful for allowing specification of the severity on the command line an creating a logger that uses that threshold.
type SyslogLike ¶
type SyslogLike interface { Alert(m string) error Crit(m string) error Debug(m string) error Emerg(m string) error Err(m string) error Info(m string) error Notice(m string) error Warning(m string) error Write(b []byte) (int, error) }
If the io.Writer passed to New() or SetOutput() implements the SyslogLike interface, it is treated as if it actually is a standard log/syslog logger.