Browse Source

Fix JSON logging for module-level tmp loggers

Module-level loggers (e.g. agiLogger, smartLogger) are created as
package-level variables at init time before flags are parsed, so
per-instance PrintJSON was never set on them.

Introduce SetGlobalJSONOutput() in the logger package and call it
immediately after flag.Parse() in main(). PrintAndLog now checks
globalPrintJSON so every Logger instance — including all the tmp
loggers — respects the --log_format flag without needing changes at
each call site.

https://claude.ai/code/session_01TvkmdBDMMeaz3qu5FGDapg
Claude 2 tuần trước cách đây
mục cha
commit
8492d18178
3 tập tin đã thay đổi với 10 bổ sung3 xóa
  1. 1 1
      src/main.go
  2. 9 1
      src/mod/info/logger/logger.go
  3. 0 1
      src/startup.go

+ 1 - 1
src/main.go

@@ -71,6 +71,7 @@ func executeShutdownSequence() {
 func main() {
 	//Parse startup flags and paramters
 	flag.Parse()
+	logger.SetGlobalJSONOutput(*log_format == "json")
 
 	//Handle version printing
 	if *show_version {
@@ -92,7 +93,6 @@ func main() {
 	// Initialize a temporary stdout-only logger so calls before RunStartup are safe.
 	// RunStartup will replace this with a file-backed logger.
 	systemWideLogger, _ = logger.NewTmpLogger()
-	systemWideLogger.PrintJSON = (*log_format == "json")
 
 	//Print copyRight information
 	systemWideLogger.PrintAndLog("System", "ArozOS(C) "+strconv.Itoa(time.Now().Year())+" "+deviceVendor+".", nil)

+ 9 - 1
src/mod/info/logger/logger.go

@@ -17,6 +17,14 @@ import (
 	and replace the ton of log.Println in the system core
 */
 
+// globalPrintJSON is set once at startup via SetGlobalJSONOutput and applies to
+// every Logger instance, including the package-level tmp loggers in each module.
+var globalPrintJSON bool
+
+func SetGlobalJSONOutput(enabled bool) {
+	globalPrintJSON = enabled
+}
+
 type Logger struct {
 	LogToFile      bool     //Set enable write to file
 	PrintJSON      bool     //Print console output as JSON lines instead of plain text
@@ -76,7 +84,7 @@ func (l *Logger) PrintAndLog(title string, message string, originalError error)
 	go func() {
 		l.Log(title, message, originalError)
 	}()
-	if l.PrintJSON {
+	if l.PrintJSON || globalPrintJSON {
 		level := "info"
 		if originalError != nil {
 			level = "error"

+ 0 - 1
src/startup.go

@@ -17,7 +17,6 @@ import (
 
 func RunStartup() {
 	systemWideLogger, _ = logger.NewLogger("system", "system/logs/system/", true)
-	systemWideLogger.PrintJSON = (*log_format == "json")
 	//1. Initiate the main system database
 
 	//Check if system or web both not exists and web.tar.gz exists. Unzip it for the user