Commit fd5903b5fb171652011e522b9f333e8c277ad039

Parents: d2dc2e510a3f300a50dada4d8a2b4af16cbf84b8

From: Moritz Poldrack <git@moritz.sh>
Date: Thu Nov 2 21:08:59 2023 +0700

log: add option to log as JSON

		

Stats

internal/config/configs.go +5/-6
main.go +16/-2

Changeset

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
diff --git a/internal/config/configs.go b/internal/config/configs.go
index d250b196adf2471ac66eec5248ea2667dd172ae4..a504d2091124e6572a28cc3040a2a72c07f39064 100644
--- a/internal/config/configs.go
+++ b/internal/config/configs.go
@@ -16,6 +16,7 @@ 		Host string `toml:"host"`
 	} `toml:"general"`
 	Advanced struct {
 		EnableInstrumentation bool `toml:"enable-instrumentation"`
+		JSONLog               bool `toml:"json-log"`
 	} `toml:"advanced"`
 }{
 	General: struct {
@@ -23,18 +24,16 @@ 		Bind string `toml:"bind-to"`
 		Host string `toml:"host"`
 	}{
 		Bind: "127.1.2.4:1558",
-	},
-	Advanced: struct {
-		EnableInstrumentation bool `toml:"enable-instrumentation"`
-	}{
-		EnableInstrumentation: false,
 	},
 }
 
 var Client = struct {
 	Media struct {
 		Directories []string `toml:"directories"`
-	}
+	} `toml:"media"`
+	Advanced struct {
+		JSONLog bool `toml:"json-log"`
+	} `toml:"advanced"`
 }{
 	Media: struct {
 		Directories []string `toml:"directories"`
diff --git a/main.go b/main.go
index d76bb762716673f78d6c15bd028378f10414a08e..e9f6b880dc7b396cc06b7a476663fe27a415c1d9 100644
--- a/main.go
+++ b/main.go
@@ -37,12 +37,19 @@ 		Level:     levels[loglevel],
 	})))
 
 	if filepath.Base(os.Args[0]) == "univiewd" {
-		slog.Debug("starting in server mode")
-
 		err := config.Load(&config.Server, config.ServerPaths)
 		if err != nil {
 			slog.Warn("no config loaded", "error", err)
 		}
+
+		if config.Server.Advanced.JSONLog {
+			slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
+				AddSource: loglevel == "debug",
+				Level:     levels[loglevel],
+			})))
+		}
+
+		slog.Debug("starting in server mode")
 
 		err = startServer()
 		if err != nil {
@@ -80,6 +87,13 @@
 		err := config.Load(&config.Client, config.ClientPaths)
 		if err != nil {
 			slog.Warn("no config loaded", "error", err)
+		}
+
+		if config.Client.Advanced.JSONLog {
+			slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
+				AddSource: loglevel == "debug",
+				Level:     levels[loglevel],
+			})))
 		}
 
 		slog.Info("starting uniview", "version", buildinfo.VersionString())