From cd8fc638cc6b505f733e52aa13792ab11179580f Mon Sep 17 00:00:00 2001 From: Simon Eskildsen <sirup@sirupsen.com> Date: Sat, 26 Jul 2014 21:02:08 -0400 Subject: [PATCH] levels: add string helper --- .gitignore | 1 + logrus.go | 20 ++++++++++++++++++++ logrus_test.go | 9 +++++++++ 3 files changed, 30 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66be63a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +logrus diff --git a/logrus.go b/logrus.go index f41e1a4..79df39c 100644 --- a/logrus.go +++ b/logrus.go @@ -10,6 +10,26 @@ type Fields map[string]interface{} // Level type type Level uint8 +// Convert the Level to a string. E.g. PanicLevel becomes "panic". +func (level Level) String() string { + switch level { + case DebugLevel: + return "debug" + case InfoLevel: + return "info" + case WarnLevel: + return "warning" + case ErrorLevel: + return "error" + case FatalLevel: + return "fatal" + case PanicLevel: + return "panic" + } + + return "unknown" +} + // These are the different logging levels. You can set the logging level to log // on your instance of logger, obtained with `logrus.New()`. const ( diff --git a/logrus_test.go b/logrus_test.go index 82187ae..f14445c 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -128,3 +128,12 @@ func TestWithFieldsShouldAllowAssignments(t *testing.T) { assert.Equal(t, false, ok) assert.Equal(t, "value1", fields["key1"]) } + +func TestConvertLevelToString(t *testing.T) { + assert.Equal(t, "debug", DebugLevel.String()) + assert.Equal(t, "info", InfoLevel.String()) + assert.Equal(t, "warning", WarnLevel.String()) + assert.Equal(t, "error", ErrorLevel.String()) + assert.Equal(t, "fatal", FatalLevel.String()) + assert.Equal(t, "panic", PanicLevel.String()) +}