summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLibravatar Daenney <daenney@users.noreply.github.com>2023-05-21 17:12:47 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-21 16:12:47 +0100
commit107237c8e84c541d7f24095dcce7abaf5d973a7e (patch)
treeb8721da6c56172dad5bb290516f7a0526bbc2236 /internal
parent[bugfix] Start + stop caches properly for testrig + pruning (#1804) (diff)
downloadgotosocial-107237c8e84c541d7f24095dcce7abaf5d973a7e.tar.xz
[feature] Make client IP logging configurable (#1799)
Diffstat (limited to 'internal')
-rw-r--r--internal/config/config.go1
-rw-r--r--internal/config/defaults.go2
-rw-r--r--internal/config/helpers.gen.go25
-rw-r--r--internal/gtscontext/log_hooks.go2
-rw-r--r--internal/middleware/logger.go7
5 files changed, 31 insertions, 6 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index b5c228a35..7119fc4a7 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -46,6 +46,7 @@ func fieldtag(field, tag string) string {
type Configuration struct {
LogLevel string `name:"log-level" usage:"Log level to run at: [trace, debug, info, warn, fatal]"`
LogDbQueries bool `name:"log-db-queries" usage:"Log database queries verbosely when log-level is trace or debug"`
+ LogClientIP bool `name:"log-client-ip" usage:"Include the client IP in logs"`
ApplicationName string `name:"application-name" usage:"Name of the application, used in various places internally"`
LandingPageUser string `name:"landing-page-user" usage:"the user that should be shown on the instance's landing page"`
ConfigPath string `name:"config-path" usage:"Path to a file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments"`
diff --git a/internal/config/defaults.go b/internal/config/defaults.go
index 54672c159..6e2d141be 100644
--- a/internal/config/defaults.go
+++ b/internal/config/defaults.go
@@ -198,4 +198,6 @@ var Defaults = Configuration{
AdminMediaPruneDryRun: true,
RequestIDHeader: "X-Request-Id",
+
+ LogClientIP: true,
}
diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go
index b635f7b8e..9993d7aaf 100644
--- a/internal/config/helpers.gen.go
+++ b/internal/config/helpers.gen.go
@@ -3679,3 +3679,28 @@ func GetRequestIDHeader() string { return global.GetRequestIDHeader() }
// SetRequestIDHeader safely sets the value for global configuration 'RequestIDHeader' field
func SetRequestIDHeader(v string) { global.SetRequestIDHeader(v) }
+
+// GetLogClientIP safely fetches the Configuration value for state's 'LogClientIP' field
+func (st *ConfigState) GetLogClientIP() (v bool) {
+ st.mutex.Lock()
+ v = st.config.LogClientIP
+ st.mutex.Unlock()
+ return
+}
+
+// SetLogClientIP safely sets the Configuration value for state's 'LogClientIP' field
+func (st *ConfigState) SetLogClientIP(v bool) {
+ st.mutex.Lock()
+ defer st.mutex.Unlock()
+ st.config.LogClientIP = v
+ st.reloadToViper()
+}
+
+// LogClientIPFlag returns the flag name for the 'LogClientIP' field
+func LogClientIPFlag() string { return "log-client-ip" }
+
+// GetLogClientIP safely fetches the value for global configuration 'LogClientIP' field
+func GetLogClientIP() bool { return global.GetLogClientIP() }
+
+// SetLogClientIP safely sets the value for global configuration 'LogClientIP' field
+func SetLogClientIP(v bool) { global.SetLogClientIP(v) }
diff --git a/internal/gtscontext/log_hooks.go b/internal/gtscontext/log_hooks.go
index 2fe43e488..94300cbb7 100644
--- a/internal/gtscontext/log_hooks.go
+++ b/internal/gtscontext/log_hooks.go
@@ -34,7 +34,7 @@ func init() {
}
return kvs
})
- // Client IP middleware hook.
+ // Public Key ID middleware hook.
log.Hook(func(ctx context.Context, kvs []kv.Field) []kv.Field {
if id := PublicKeyID(ctx); id != "" {
return append(kvs, kv.Field{K: "pubKeyID", V: id})
diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go
index 50e5542c3..8acb742fb 100644
--- a/internal/middleware/logger.go
+++ b/internal/middleware/logger.go
@@ -31,7 +31,7 @@ import (
)
// Logger returns a gin middleware which provides request logging and panic recovery.
-func Logger() gin.HandlerFunc {
+func Logger(logClientIP bool) gin.HandlerFunc {
return func(c *gin.Context) {
// Initialize the logging fields
fields := make(kv.Fields, 5, 7)
@@ -72,10 +72,7 @@ func Logger() gin.HandlerFunc {
fields[2] = kv.Field{"method", c.Request.Method}
fields[3] = kv.Field{"statusCode", code}
fields[4] = kv.Field{"path", path}
- if includeClientIP := true; includeClientIP {
- // TODO: make this configurable.
- //
- // Include clientIP if enabled.
+ if logClientIP {
fields = append(fields, kv.Field{
"clientIP", c.ClientIP(),
})