@@ -20,7 +20,10 @@ import (
2020 "io/ioutil"
2121 "net"
2222 "net/http"
23+ "os"
2324 "path/filepath"
25+ "sync"
26+ "time"
2427
2528 "github.com/go-kit/log"
2629 "github.com/go-kit/log/level"
@@ -30,13 +33,18 @@ import (
3033)
3134
3235var (
33- errNoTLSConfig = errors .New ("TLS config is not present" )
36+ errNoTLSConfig = errors .New ("TLS config is not present" )
37+ timestampFormat = log .TimestampFormat (
38+ func () time.Time { return time .Now ().UTC () },
39+ "2006-01-02T15:04:05.000Z07:00" ,
40+ )
3441)
3542
3643type Config struct {
37- TLSConfig TLSStruct `yaml:"tls_server_config"`
38- HTTPConfig HTTPStruct `yaml:"http_server_config"`
39- Users map [string ]config_util.Secret `yaml:"basic_auth_users"`
44+ TLSConfig TLSStruct `yaml:"tls_server_config"`
45+ HTTPConfig HTTPStruct `yaml:"http_server_config"`
46+ RequestLogConfig RequestLogStruct `yaml:"request_log_config"`
47+ Users map [string ]config_util.Secret `yaml:"basic_auth_users"`
4048}
4149
4250type TLSStruct struct {
@@ -62,6 +70,15 @@ type HTTPStruct struct {
6270 HTTP2 bool `yaml:"http2"`
6371}
6472
73+ type RequestLogStruct struct {
74+ File string `yaml:"file"`
75+ HeaderForIp string `yaml:"header_for_ip"`
76+ }
77+
78+ func (r * RequestLogStruct ) SetDirectory (dir string ) {
79+ r .File = config_util .JoinDir (dir , r .File )
80+ }
81+
6582func getConfig (configPath string ) (* Config , error ) {
6683 content , err := ioutil .ReadFile (configPath )
6784 if err != nil {
@@ -73,10 +90,12 @@ func getConfig(configPath string) (*Config, error) {
7390 MaxVersion : tls .VersionTLS13 ,
7491 PreferServerCipherSuites : true ,
7592 },
76- HTTPConfig : HTTPStruct {HTTP2 : true },
93+ HTTPConfig : HTTPStruct {HTTP2 : true },
94+ RequestLogConfig : RequestLogStruct {File : "" , HeaderForIp : "" },
7795 }
7896 err = yaml .UnmarshalStrict (content , c )
7997 c .TLSConfig .SetDirectory (filepath .Dir (configPath ))
98+ c .RequestLogConfig .SetDirectory (filepath .Dir (configPath ))
8099 return c , err
81100}
82101
@@ -207,11 +226,33 @@ func Serve(l net.Listener, server *http.Server, tlsConfigPath string, logger log
207226 return err
208227 }
209228
210- server .Handler = & userAuthRoundtrip {
211- tlsConfigPath : tlsConfigPath ,
212- logger : logger ,
213- handler : handler ,
214- cache : newCache (),
229+ if c .RequestLogConfig .File != "" {
230+ f , err := os .OpenFile (c .RequestLogConfig .File , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0666 )
231+ if err != nil {
232+ return err
233+ }
234+
235+ defer f .Close ()
236+
237+ server .Handler = & userAuthRoundtrip {
238+ tlsConfigPath : tlsConfigPath ,
239+ logger : logger ,
240+ handler : handler ,
241+ cache : newCache (),
242+ requestLogger : log .With (log .NewJSONLogger (f ), "ts" , timestampFormat ),
243+ requestLoggerLock : sync.RWMutex {},
244+ }
245+
246+ level .Info (logger ).Log ("msg" , "Request logging is enabled." , "file" , c .RequestLogConfig .File , "headerForIp" , c .RequestLogConfig .HeaderForIp )
247+ } else {
248+ server .Handler = & userAuthRoundtrip {
249+ tlsConfigPath : tlsConfigPath ,
250+ logger : logger ,
251+ handler : handler ,
252+ cache : newCache (),
253+ }
254+
255+ level .Info (logger ).Log ("msg" , "Request logging is disabled." )
215256 }
216257
217258 config , err := ConfigToTLSConfig (& c .TLSConfig )
0 commit comments