Преглед на файлове

Add configurable log timestamp format

pull/531/head
Mauro Marques Filho преди 1 месец
родител
ревизия
5c4843f6ee
променени са 6 файла, в които са добавени 82 реда и са изтрити 5 реда
  1. 14
    0
      example.config.toml
  2. 21
    1
      internal/cli/run_proxy.go
  3. 31
    0
      internal/cli/run_proxy_test.go
  4. 5
    4
      internal/config/config.go
  5. 10
    0
      internal/config/config_test.go
  6. 1
    0
      internal/config/parse.go

+ 14
- 0
example.config.toml Целия файл

15
 # you have any issue.
15
 # you have any issue.
16
 debug = true
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
 # A secret (required). Please remember that mtg supports only FakeTLS
32
 # A secret (required). Please remember that mtg supports only FakeTLS
19
 # mode, legacy simple and secured mode are prohibited. For you it means
33
 # mode, legacy simple and secured mode are prohibited. For you it means
20
 # that secret should either be base64-encoded or starts with ee.
34
 # that secret should either be base64-encoded or starts with ee.

+ 21
- 1
internal/cli/run_proxy.go Целия файл

6
 	"net"
6
 	"net"
7
 	"os"
7
 	"os"
8
 	"strings"
8
 	"strings"
9
+	"time"
9
 
10
 
10
 	"github.com/9seconds/mtg/v2/antireplay"
11
 	"github.com/9seconds/mtg/v2/antireplay"
11
 	"github.com/9seconds/mtg/v2/events"
12
 	"github.com/9seconds/mtg/v2/events"
24
 )
25
 )
25
 
26
 
26
 func makeLogger(conf *config.Config) mtglib.Logger {
27
 func makeLogger(conf *config.Config) mtglib.Logger {
27
-	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
28
+	zerolog.TimeFieldFormat = logTimeFormat(conf.LogTimeFormat)
28
 	zerolog.TimestampFieldName = "timestamp"
29
 	zerolog.TimestampFieldName = "timestamp"
29
 	zerolog.LevelFieldName = "level"
30
 	zerolog.LevelFieldName = "level"
30
 
31
 
39
 	return logger.NewZeroLogger(baseLogger)
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
 func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
62
 func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
43
 	resolver, err := network.GetDNS(conf.GetDNS())
63
 	resolver, err := network.GetDNS(conf.GetDNS())
44
 	if err != nil {
64
 	if err != nil {

+ 31
- 0
internal/cli/run_proxy_test.go Целия файл

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
 
23
 
24
 type Config struct {
24
 type Config struct {
25
 	Debug                       TypeBool        `json:"debug"`
25
 	Debug                       TypeBool        `json:"debug"`
26
+	LogTimeFormat               string          `json:"logTimeFormat"`
26
 	AllowFallbackOnUnknownDC    TypeBool        `json:"allowFallbackOnUnknownDc"`
27
 	AllowFallbackOnUnknownDC    TypeBool        `json:"allowFallbackOnUnknownDc"`
27
 	Secret                      mtglib.Secret   `json:"secret"`
28
 	Secret                      mtglib.Secret   `json:"secret"`
28
 	BindTo                      TypeHostPort    `json:"bindTo"`
29
 	BindTo                      TypeHostPort    `json:"bindTo"`
71
 			Interval TypeDuration    `json:"interval"`
72
 			Interval TypeDuration    `json:"interval"`
72
 			Count    TypeConcurrency `json:"count"`
73
 			Count    TypeConcurrency `json:"count"`
73
 		} `json:"keepAlive"`
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
 	} `json:"network"`
79
 	} `json:"network"`
79
 	Stats struct {
80
 	Stats struct {
80
 		StatsD struct {
81
 		StatsD struct {

+ 10
- 0
internal/config/config_test.go Целия файл

42
 	suite.Equal("0.0.0.0:3128", conf.BindTo.String())
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
 func (suite *ConfigTestSuite) TestParsePublicIP() {
55
 func (suite *ConfigTestSuite) TestParsePublicIP() {
46
 	conf, err := config.Parse(suite.ReadConfig("public_ip.toml"))
56
 	conf, err := config.Parse(suite.ReadConfig("public_ip.toml"))
47
 	suite.NoError(err)
57
 	suite.NoError(err)

+ 1
- 0
internal/config/parse.go Целия файл

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

Loading…
Отказ
Запис