Skip to content

Commit 0d817cc

Browse files
committed
Add log forrmat options
1 parent 1eb29f5 commit 0d817cc

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
docker-volume-loopback
66

77
plugin/rootfs
8-
tmp
8+

context/context.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"github.com/sirupsen/logrus"
77
"io"
88
"math/rand"
9+
"strings"
910
"time"
1011
)
1112

13+
// Log levels
1214
const (
1315
Error = 0
1416
Warning = 1
@@ -25,6 +27,23 @@ var allLevels = []logrus.Level{
2527
logrus.TraceLevel,
2628
}
2729

30+
// Log formats
31+
const (
32+
FormatJson = "json"
33+
FormatText = "text"
34+
FormatNice = "nice"
35+
)
36+
37+
var formats = map[string]logrus.Formatter{
38+
FormatJson: &logrus.JSONFormatter{},
39+
FormatText: &logrus.TextFormatter{
40+
DisableColors: true,
41+
FullTimestamp: true,
42+
},
43+
FormatNice: &logrus.TextFormatter{},
44+
}
45+
var allFormats = []string{FormatJson, FormatText, FormatNice}
46+
2847
type Fields map[string]interface{}
2948

3049
type Context struct {
@@ -35,8 +54,15 @@ type Context struct {
3554
fields map[string]interface{}
3655
}
3756

38-
func Init(level int, output io.Writer) {
39-
logrus.SetFormatter(&logrus.JSONFormatter{})
57+
func Init(level int, format string, output io.Writer) {
58+
formatter, valid := formats[format]
59+
if !valid {
60+
panic(
61+
fmt.Sprintf(
62+
"Wrong log format '%s' - only following are supported: %s",
63+
format, strings.Join(allFormats, ", ")))
64+
}
65+
logrus.SetFormatter(formatter)
4066
logrus.SetOutput(output)
4167
logrus.SetLevel(convertLevel(level))
4268
}

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
type config struct {
1515
Socket string `arg:"--socket,env:SOCKET,help:path to the plugin UNIX socket under /run/docker/plugins/"`
1616
LogLevel int `arg:"--log-level,env:LOG_LEVEL,help:set log level - from 0 to 4 for Error/Warning/Info/Debug/Trace"`
17+
LogFormat string `arg:"--log-format,env:LOG_FORMAT,help:set log format - json/text/nice"`
1718
StateDir string `arg:"--state-dir,env:STATE_DIR,help:dir used to keep track of currently mounted volumes"`
1819
DataDir string `arg:"--data-dir,env:DATA_DIR,help:dir used to store actual volume data"`
1920
MountDir string `arg:"--mount-dir,env:MOUNT_DIR,help:dir used to create mount-points"`
@@ -28,13 +29,14 @@ var (
2829
MountDir: "/mnt",
2930
DefaultSize: "1GiB",
3031
LogLevel: 0,
32+
LogFormat: context.FormatNice,
3133
}
3234
)
3335

3436
func main() {
3537
arg.MustParse(args)
3638

37-
context.Init(args.LogLevel, os.Stdout)
39+
context.Init(args.LogLevel, args.LogFormat, os.Stdout)
3840

3941
ctx := context.New()
4042

0 commit comments

Comments
 (0)