-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial structure of log repo (#272)
* Initial look of log repo * initial design of log repo, non-controversial * Address reviews and remove unused import * fix compliation errors * Fix errors * Address review changes * nit fix
- Loading branch information
1 parent
6c70295
commit b32b53c
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Level describes the current log level. | ||
type Level uint32 | ||
|
||
const ( | ||
// DebugLevel log level. | ||
DebugLevel Level = Level(logrus.DebugLevel) | ||
// InfoLevel log level. | ||
InfoLevel Level = Level(logrus.InfoLevel) | ||
// ErrorLevel log level. | ||
ErrorLevel Level = Level(logrus.ErrorLevel) | ||
) | ||
|
||
type logger struct { | ||
level Level | ||
entry *logrus.Entry | ||
ctx context.Context | ||
err error | ||
} | ||
|
||
func Info() Logger { | ||
return &logger{level: InfoLevel} | ||
} | ||
|
||
func Error() Logger { | ||
return &logger{level: ErrorLevel} | ||
} | ||
|
||
func Debug() Logger { | ||
return &logger{level: DebugLevel} | ||
} | ||
|
||
// Print adds `msg` to the log at `InfoLevel`. It is a wrapper for `Info().Print(msg)`, since this is the most common use case. | ||
func Print(msg string) { | ||
Info().Print(msg) | ||
} | ||
|
||
func WithContext(ctx context.Context) { | ||
Info().WithContext(ctx) | ||
} | ||
|
||
func (l *logger) Print(msg string) { | ||
switch l.level { | ||
case InfoLevel: | ||
logrus.Info(msg) | ||
case ErrorLevel: | ||
logrus.Error(msg) | ||
case DebugLevel: | ||
logrus.Debug(msg) | ||
} | ||
} | ||
|
||
func (l *logger) WithContext(ctx context.Context) Logger { | ||
l.ctx = ctx | ||
return l | ||
} | ||
|
||
func (l *logger) WithError(err error) Logger { | ||
l.err = err | ||
return l | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type Logger interface { | ||
Print(msg string) | ||
WithContext(ctx context.Context) Logger | ||
WithError(err error) Logger | ||
} |