-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
145 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
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
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
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
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
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
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
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
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 @@ | ||
go_library( | ||
name = "logging", | ||
srcs = [ | ||
"formatter.go", | ||
"logging.go", | ||
], | ||
visibility = ["PUBLIC"], | ||
deps = [ | ||
"//third_party/go:logrus", | ||
], | ||
) |
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,55 @@ | ||
package logging | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// Default log format - rich. | ||
richLogFormat = "%time%:%file%:%line%# [%level%] - %message%\n" | ||
|
||
// Raw log format - plain. | ||
rawLogFormat = "%message%\n" | ||
|
||
// Default timestamp format | ||
defaultTimestampFormat = time.StampNano | ||
) | ||
|
||
// Formatter implements the logrus.Formatter interface. | ||
// It has a very nice | ||
type Formatter struct { | ||
// Timestamp format | ||
TimestampFormat string | ||
// Available standard keys: time, msg, lvl | ||
// Also can include custom fields but limited to strings. | ||
// All of fields need to be wrapped inside %% i.e %time% %msg% | ||
LogFormat string | ||
} | ||
|
||
// Format implements a logrus formatter's Format method. | ||
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
output := f.LogFormat | ||
timestampFormat := f.TimestampFormat | ||
if timestampFormat == "" { | ||
timestampFormat = defaultTimestampFormat | ||
} | ||
|
||
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1) | ||
output = strings.Replace(output, "%file%", filepath.Base(entry.Caller.File), 1) | ||
output = strings.Replace(output, "%line%", strconv.Itoa(entry.Caller.Line), 1) | ||
output = strings.Replace(output, "%message%", entry.Message, 1) | ||
output = strings.Replace(output, "%level%", strings.ToUpper(entry.Level.String()), 1) | ||
|
||
for k, v := range entry.Data { | ||
if s, ok := v.(string); ok { | ||
output = strings.Replace(output, "%"+k+"%", s, 1) | ||
} | ||
} | ||
|
||
return []byte(output), nil | ||
} |
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,56 @@ | ||
package logging | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// PanicLevel level, highest level of severity. Logs and then calls panic with the message passed to Debug, Info, ... | ||
PanicLevel Level = iota | ||
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the logging level is set to Panic. | ||
FatalLevel | ||
// ErrorLevel level. Logs. Used for errors that should definitely be noted. | ||
// Commonly used for hooks to send errors to an error tracking service. | ||
ErrorLevel | ||
// WarnLevel level. Non-critical entries that deserve eyes. | ||
WarnLevel | ||
// InfoLevel level. General operational entries about what's going on inside the application. | ||
InfoLevel | ||
// DebugLevel level. Usually only enabled when debugging. Very verbose logging. | ||
DebugLevel | ||
// TraceLevel level. Designates finer-grained informational events than the Debug. | ||
TraceLevel | ||
) | ||
|
||
// Level represents the logger's logging severity level. | ||
type Level int | ||
|
||
var levels = map[Level]logrus.Level{ | ||
PanicLevel: logrus.PanicLevel, | ||
FatalLevel: logrus.FatalLevel, | ||
ErrorLevel: logrus.ErrorLevel, | ||
WarnLevel: logrus.WarnLevel, | ||
InfoLevel: logrus.InfoLevel, | ||
DebugLevel: logrus.DebugLevel, | ||
TraceLevel: logrus.TraceLevel, | ||
} | ||
|
||
// Logger is a wrapper around logrus. It is used by all micro-services for logging purposes. | ||
type Logger struct { | ||
*logrus.Logger | ||
} | ||
|
||
// NewLogger returns a new logger | ||
func NewLogger() *Logger { | ||
logrusLogger := &logrus.Logger{ | ||
Out: os.Stderr, | ||
Formatter: &Formatter{ | ||
LogFormat: richLogFormat, | ||
}, | ||
Level: logrus.InfoLevel, | ||
ReportCaller: true, | ||
} | ||
return &Logger{logrusLogger} | ||
} |