ソースを参照

Add configurable log timestamp format

pull/531/head
Mauro Marques Filho 1ヶ月前
コミット
5c4843f6ee

+ 14
- 0
example.config.toml ファイルの表示

@@ -15,6 +15,20 @@
15 15
 # you have any issue.
16 16
 debug = true
17 17
 
18
+# Configures timestamp format in logs. The default is "unix-ms", which keeps
19
+# the existing numeric Unix milliseconds format for log aggregators.
20
+#
21
+# Presets:
22
+#   "unix"         Unix seconds
23
+#   "unix-ms"      Unix milliseconds
24
+#   "unix-micro"   Unix microseconds
25
+#   "unix-nano"    Unix nanoseconds
26
+#   "rfc3339"      RFC3339 string, honoring host timezone
27
+#   "rfc3339-nano" RFC3339Nano string, honoring host timezone
28
+#
29
+# Any other value is passed to Go's time formatter as a layout string.
30
+# log-time-format = "unix-ms"
31
+
18 32
 # A secret (required). Please remember that mtg supports only FakeTLS
19 33
 # mode, legacy simple and secured mode are prohibited. For you it means
20 34
 # that secret should either be base64-encoded or starts with ee.

+ 21
- 1
internal/cli/run_proxy.go ファイルの表示

@@ -6,6 +6,7 @@ import (
6 6
 	"net"
7 7
 	"os"
8 8
 	"strings"
9
+	"time"
9 10
 
10 11
 	"github.com/9seconds/mtg/v2/antireplay"
11 12
 	"github.com/9seconds/mtg/v2/events"
@@ -24,7 +25,7 @@ import (
24 25
 )
25 26
 
26 27
 func makeLogger(conf *config.Config) mtglib.Logger {
27
-	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
28
+	zerolog.TimeFieldFormat = logTimeFormat(conf.LogTimeFormat)
28 29
 	zerolog.TimestampFieldName = "timestamp"
29 30
 	zerolog.LevelFieldName = "level"
30 31
 
@@ -39,6 +40,25 @@ func makeLogger(conf *config.Config) mtglib.Logger {
39 40
 	return logger.NewZeroLogger(baseLogger)
40 41
 }
41 42
 
43
+func logTimeFormat(value string) string {
44
+	switch strings.ToLower(strings.TrimSpace(value)) {
45
+	case "", "unix-ms":
46
+		return zerolog.TimeFormatUnixMs
47
+	case "unix":
48
+		return zerolog.TimeFormatUnix
49
+	case "unix-micro":
50
+		return zerolog.TimeFormatUnixMicro
51
+	case "unix-nano":
52
+		return zerolog.TimeFormatUnixNano
53
+	case "rfc3339":
54
+		return time.RFC3339
55
+	case "rfc3339-nano":
56
+		return time.RFC3339Nano
57
+	default:
58
+		return value
59
+	}
60
+}
61
+
42 62
 func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
43 63
 	resolver, err := network.GetDNS(conf.GetDNS())
44 64
 	if err != nil {

+ 31
- 0
internal/cli/run_proxy_test.go ファイルの表示

@@ -0,0 +1,31 @@
1
+package cli
2
+
3
+import (
4
+	"testing"
5
+	"time"
6
+
7
+	"github.com/rs/zerolog"
8
+	"github.com/stretchr/testify/require"
9
+)
10
+
11
+func TestLogTimeFormat(t *testing.T) {
12
+	cases := []struct {
13
+		value string
14
+		want  string
15
+	}{
16
+		{value: "", want: zerolog.TimeFormatUnixMs},
17
+		{value: "unix-ms", want: zerolog.TimeFormatUnixMs},
18
+		{value: "unix", want: zerolog.TimeFormatUnix},
19
+		{value: "unix-micro", want: zerolog.TimeFormatUnixMicro},
20
+		{value: "unix-nano", want: zerolog.TimeFormatUnixNano},
21
+		{value: "rfc3339", want: time.RFC3339},
22
+		{value: "rfc3339-nano", want: time.RFC3339Nano},
23
+		{value: "2006-01-02 15:04:05", want: "2006-01-02 15:04:05"},
24
+	}
25
+
26
+	for _, c := range cases {
27
+		t.Run(c.value, func(t *testing.T) {
28
+			require.Equal(t, c.want, logTimeFormat(c.value))
29
+		})
30
+	}
31
+}

+ 5
- 4
internal/config/config.go ファイルの表示

@@ -23,6 +23,7 @@ type ListConfig struct {
23 23
 
24 24
 type Config struct {
25 25
 	Debug                       TypeBool        `json:"debug"`
26
+	LogTimeFormat               string          `json:"logTimeFormat"`
26 27
 	AllowFallbackOnUnknownDC    TypeBool        `json:"allowFallbackOnUnknownDc"`
27 28
 	Secret                      mtglib.Secret   `json:"secret"`
28 29
 	BindTo                      TypeHostPort    `json:"bindTo"`
@@ -71,10 +72,10 @@ type Config struct {
71 72
 			Interval TypeDuration    `json:"interval"`
72 73
 			Count    TypeConcurrency `json:"count"`
73 74
 		} `json:"keepAlive"`
74
-		DOHIP            TypeIP         `json:"dohIp"`
75
-		DNS              TypeDNSURI     `json:"dns"`
76
-		Proxies          []TypeProxyURL `json:"proxies"`
77
-		TCPNotSentLowat  TypeBytes      `json:"tcpNotSentLowat"`
75
+		DOHIP           TypeIP         `json:"dohIp"`
76
+		DNS             TypeDNSURI     `json:"dns"`
77
+		Proxies         []TypeProxyURL `json:"proxies"`
78
+		TCPNotSentLowat TypeBytes      `json:"tcpNotSentLowat"`
78 79
 	} `json:"network"`
79 80
 	Stats struct {
80 81
 		StatsD struct {

+ 10
- 0
internal/config/config_test.go ファイルの表示

@@ -42,6 +42,16 @@ func (suite *ConfigTestSuite) TestParseMinimalConfig() {
42 42
 	suite.Equal("0.0.0.0:3128", conf.BindTo.String())
43 43
 }
44 44
 
45
+func (suite *ConfigTestSuite) TestParseLogTimeFormat() {
46
+	conf, err := config.Parse([]byte(`
47
+secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
48
+bind-to = "0.0.0.0:3128"
49
+log-time-format = "rfc3339"
50
+`))
51
+	suite.NoError(err)
52
+	suite.Equal("rfc3339", conf.LogTimeFormat)
53
+}
54
+
45 55
 func (suite *ConfigTestSuite) TestParsePublicIP() {
46 56
 	conf, err := config.Parse(suite.ReadConfig("public_ip.toml"))
47 57
 	suite.NoError(err)

+ 1
- 0
internal/config/parse.go ファイルの表示

@@ -10,6 +10,7 @@ import (
10 10
 
11 11
 type tomlConfig struct {
12 12
 	Debug                       bool   `toml:"debug" json:"debug,omitempty"`
13
+	LogTimeFormat               string `toml:"log-time-format" json:"logTimeFormat,omitempty"`
13 14
 	AllowFallbackOnUnknownDC    bool   `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
14 15
 	Secret                      string `toml:"secret" json:"secret"`
15 16
 	BindTo                      string `toml:"bind-to" json:"bindTo"`

読み込み中…
キャンセル
保存