feat(elog) Option
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 16, 2014 at 02:13 UTC
d55dd16bf5950fb5f4e35632a687a94605d6ea78
1 file changed
+50
util/elog/option.go
new
+50
@@ -0,0 +1,50 @@
1
+package elog
2
+
3
+import (
4
+ "io"
5
+
6
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus"
7
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/gopkg.in/natefinch/lumberjack.v2"
8
+)
9
+
10
+type Option func()
11
+
12
+func SetOption(o Option) {
13
+ o()
14
+}
15
+
16
+var JSONFormatter = func() {
17
+ logrus.SetFormatter(&logrus.JSONFormatter{})
18
+}
19
+
20
+var TextFormatter = func() {
21
+ logrus.SetFormatter(&logrus.TextFormatter{})
22
+}
23
+
24
+type LogRotatorConfig struct {
25
+ File string
26
+ MaxSizeMB uint64
27
+ MaxBackups uint64
28
+ MaxAgeDays uint64
29
+}
30
+
31
+func Output(w io.Writer) Option {
32
+ return func() {
33
+ logrus.SetOutput(w)
34
+ // TODO return previous Output option
35
+ }
36
+}
37
+
38
+func OutputRotatingLogFile(config LogRotatorConfig) Option {
39
+ return func() {
40
+ logrus.SetOutput(
41
+ &lumberjack.Logger{
42
+ Filename: config.File,
43
+ MaxSize: int(config.MaxSizeMB),
44
+ MaxBackups: int(config.MaxBackups),
45
+ MaxAge: int(config.MaxAgeDays),
46
+ })
47
+ }
48
+}
49
+
50
+// TODO log levels? logrus.SetLevel(logrus.DebugLevel)