Skip to content

Commit

Permalink
Initial structure of log repo (#272)
Browse files Browse the repository at this point in the history
* 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
SupriyaKasten committed Sep 11, 2019
1 parent 6c70295 commit b32b53c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/log/log.go
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
}
11 changes: 11 additions & 0 deletions pkg/log/printer.go
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
}

0 comments on commit b32b53c

Please sign in to comment.