mirror of https://github.com/sirupsen/logrus.git
levels: add to logger type
This commit is contained in:
parent
d3ee06cc28
commit
afde6aea11
16
README.md
16
README.md
|
@ -30,7 +30,7 @@ seen as a hint you want to add a field, however, you can still use the
|
||||||
#### Hooks
|
#### Hooks
|
||||||
|
|
||||||
You can add hooks for logging levels. For example to send errors to an exception
|
You can add hooks for logging levels. For example to send errors to an exception
|
||||||
tracking service on `LevelError`, `LevelFatal` and `LevelPanic`.
|
tracking service on `Error`, `Fatal` and `Panic`.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
log = logrus.New()
|
log = logrus.New()
|
||||||
|
@ -51,12 +51,12 @@ func (hook *AirbrakeHook) Fire(entry *Entry) (error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// `Levels()` returns a slice of `LevelTypes` the hook is fired for.
|
// `Levels()` returns a slice of `Levels` the hook is fired for.
|
||||||
func (hook *AirbrakeHook) Levels() []logrus.LevelType {
|
func (hook *AirbrakeHook) Levels() []logrus.Level {
|
||||||
return []logrus.LevelType{
|
return []logrus.Level{
|
||||||
logrus.LevelError,
|
logrus.Error,
|
||||||
logrus.LevelFatal,
|
logrus.Fatal,
|
||||||
logrus.LevelPanic
|
logrus.Panic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -78,7 +78,7 @@ You can set the logging level:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// Will log anything that is info or above, default.
|
// Will log anything that is info or above, default.
|
||||||
logrus.Level = LevelInfo
|
log.Level = logrus.Info
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Entries
|
#### Entries
|
||||||
|
|
37
entry.go
37
entry.go
|
@ -79,52 +79,45 @@ func (entry *Entry) log(level string, msg string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Debug(args ...interface{}) {
|
func (entry *Entry) Debug(args ...interface{}) {
|
||||||
if Level >= LevelDebug {
|
if entry.logger.Level >= Debug {
|
||||||
entry.log("debug", fmt.Sprint(args...))
|
entry.log("debug", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelDebug, entry)
|
entry.logger.Hooks.Fire(Debug, entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Info(args ...interface{}) {
|
func (entry *Entry) Info(args ...interface{}) {
|
||||||
if Level >= LevelInfo {
|
if entry.logger.Level >= Info {
|
||||||
entry.log("info", fmt.Sprint(args...))
|
entry.log("info", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelInfo, entry)
|
entry.logger.Hooks.Fire(Info, entry)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (entry *Entry) Print(args ...interface{}) {
|
|
||||||
if Level >= LevelInfo {
|
|
||||||
entry.log("info", fmt.Sprint(args...))
|
|
||||||
entry.logger.Hooks.Fire(LevelInfo, entry)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Warn(args ...interface{}) {
|
func (entry *Entry) Warn(args ...interface{}) {
|
||||||
if Level >= LevelWarn {
|
if entry.logger.Level >= Warn {
|
||||||
entry.log("warning", fmt.Sprint(args...))
|
entry.log("warning", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelWarn, entry)
|
entry.logger.Hooks.Fire(Warn, entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Error(args ...interface{}) {
|
func (entry *Entry) Error(args ...interface{}) {
|
||||||
if Level >= LevelError {
|
if entry.logger.Level >= Error {
|
||||||
entry.log("error", fmt.Sprint(args...))
|
entry.log("error", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelError, entry)
|
entry.logger.Hooks.Fire(Error, entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Fatal(args ...interface{}) {
|
func (entry *Entry) Fatal(args ...interface{}) {
|
||||||
if Level >= LevelFatal {
|
if entry.logger.Level >= Fatal {
|
||||||
entry.log("fatal", fmt.Sprint(args...))
|
entry.log("fatal", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelFatal, entry)
|
entry.logger.Hooks.Fire(Fatal, entry)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Panic(args ...interface{}) {
|
func (entry *Entry) Panic(args ...interface{}) {
|
||||||
if Level >= LevelPanic {
|
if entry.logger.Level >= Panic {
|
||||||
msg := entry.log("panic", fmt.Sprint(args...))
|
msg := entry.log("panic", fmt.Sprint(args...))
|
||||||
entry.logger.Hooks.Fire(LevelPanic, entry)
|
entry.logger.Hooks.Fire(Panic, entry)
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
panic(fmt.Sprint(args...))
|
panic(fmt.Sprint(args...))
|
||||||
|
@ -141,7 +134,7 @@ func (entry *Entry) Infof(format string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Printf(format string, args ...interface{}) {
|
func (entry *Entry) Printf(format string, args ...interface{}) {
|
||||||
entry.Print(fmt.Sprintf(format, args...))
|
entry.Info(fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
||||||
|
@ -153,7 +146,7 @@ func (entry *Entry) Warningf(format string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
||||||
entry.Print(fmt.Sprintf(format, args...))
|
entry.Error(fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||||
|
@ -175,7 +168,7 @@ func (entry *Entry) Infoln(args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Println(args ...interface{}) {
|
func (entry *Entry) Println(args ...interface{}) {
|
||||||
entry.Print(fmt.Sprint(args...))
|
entry.Info(fmt.Sprint(args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Warnln(args ...interface{}) {
|
func (entry *Entry) Warnln(args ...interface{}) {
|
||||||
|
|
6
hooks.go
6
hooks.go
|
@ -1,11 +1,11 @@
|
||||||
package logrus
|
package logrus
|
||||||
|
|
||||||
type Hook interface {
|
type Hook interface {
|
||||||
Levels() []LevelType
|
Levels() []Level
|
||||||
Fire(*Entry) error
|
Fire(*Entry) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type levelHooks map[LevelType][]Hook
|
type levelHooks map[Level][]Hook
|
||||||
|
|
||||||
func (hooks levelHooks) Add(hook Hook) {
|
func (hooks levelHooks) Add(hook Hook) {
|
||||||
for _, level := range hook.Levels() {
|
for _, level := range hook.Levels() {
|
||||||
|
@ -17,7 +17,7 @@ func (hooks levelHooks) Add(hook Hook) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hooks levelHooks) Fire(level LevelType, entry *Entry) error {
|
func (hooks levelHooks) Fire(level Level, entry *Entry) error {
|
||||||
for _, hook := range hooks[level] {
|
for _, hook := range hooks[level] {
|
||||||
if err := hook.Fire(entry); err != nil {
|
if err := hook.Fire(entry); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -10,6 +10,7 @@ type Logger struct {
|
||||||
Out io.Writer
|
Out io.Writer
|
||||||
Hooks levelHooks
|
Hooks levelHooks
|
||||||
Formatter Formatter
|
Formatter Formatter
|
||||||
|
Level Level
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ func New() *Logger {
|
||||||
Out: os.Stdout, // Default to stdout, change it if you want.
|
Out: os.Stdout, // Default to stdout, change it if you want.
|
||||||
Formatter: new(TextFormatter),
|
Formatter: new(TextFormatter),
|
||||||
Hooks: make(levelHooks),
|
Hooks: make(levelHooks),
|
||||||
|
Level: Info,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +76,7 @@ func (logger *Logger) Info(args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Print(args ...interface{}) {
|
func (logger *Logger) Print(args ...interface{}) {
|
||||||
NewEntry(logger).Print(args...)
|
NewEntry(logger).Info(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Warn(args ...interface{}) {
|
func (logger *Logger) Warn(args ...interface{}) {
|
||||||
|
|
16
logrus.go
16
logrus.go
|
@ -2,19 +2,17 @@ package logrus
|
||||||
|
|
||||||
type Fields map[string]interface{}
|
type Fields map[string]interface{}
|
||||||
|
|
||||||
type LevelType uint8
|
type Level uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LevelPanic LevelType = iota
|
Panic Level = iota
|
||||||
LevelFatal
|
Fatal
|
||||||
LevelError
|
Error
|
||||||
LevelWarn
|
Warn
|
||||||
LevelInfo
|
Info
|
||||||
LevelDebug
|
Debug
|
||||||
)
|
)
|
||||||
|
|
||||||
var Level LevelType = LevelInfo
|
|
||||||
|
|
||||||
// StandardLogger is what your logrus-enabled library should take, that way
|
// StandardLogger is what your logrus-enabled library should take, that way
|
||||||
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
||||||
// interface, this is the closest we get, unfortunately.
|
// interface, this is the closest we get, unfortunately.
|
||||||
|
|
Loading…
Reference in New Issue