@@ -28,19 +28,21 @@ import (
2828// initLog initializes the application's logging system.
2929func initLog () error {
3030
31- // Refresh the global system configuration.
31+ // Step 1: Refresh the global system configuration.
3232 p , err := new (gs_conf.SysConfig ).Refresh ()
3333 if err != nil {
3434 return util .FormatError (err , "refresh error in source sys" )
3535 }
3636
37+ // Step 2: Load logging-related configuration parameters.
3738 var c struct {
3839 // LocalDir is the directory that contains configuration files.
3940 // Defaults to "./conf" if not provided.
4041 LocalDir string `value:"${spring.app.config-local.dir:=./conf}"`
4142
4243 // Profiles specifies the active application profile(s),
43- // such as "dev" or "prod".
44+ // such as "dev", "prod", etc.
45+ // Multiple profiles can be provided as a comma-separated list.
4446 Profiles string `value:"${spring.profiles.active:=}"`
4547 }
4648 if err = p .Bind (& c ); err != nil {
@@ -49,9 +51,10 @@ func initLog() error {
4951
5052 extensions := []string {".properties" , ".yaml" , ".yml" , ".xml" , ".json" }
5153
54+ // Step 3: Build a list of candidate configuration files.
5255 var files []string
5356 if profiles := strings .TrimSpace (c .Profiles ); profiles != "" {
54- for s := range strings .SplitSeq (profiles , "," ) {
57+ for s := range strings .SplitSeq (profiles , "," ) { // NOTE: range returns index
5558 if s = strings .TrimSpace (s ); s != "" {
5659 for _ , ext := range extensions {
5760 files = append (files , filepath .Join (c .LocalDir , "log-" + s + ext ))
@@ -63,25 +66,24 @@ func initLog() error {
6366 files = append (files , filepath .Join (c .LocalDir , "log" + ext ))
6467 }
6568
66- // Determine which log configuration file to use .
69+ // Step 4: Detect existing configuration files .
6770 var logFiles []string
6871 for _ , s := range files {
6972 if ok , err := util .PathExists (s ); err != nil {
7073 return err
71- } else if ! ok {
72- continue
74+ } else if ok {
75+ logFiles = append ( logFiles , s )
7376 }
74- logFiles = append (logFiles , s )
7577 }
7678
77- // If no configuration file exists, leave the logger as default.
78- if n := len (logFiles ); n == 0 {
79+ // Step 5: Apply logging configuration or fall back to defaults.
80+ switch n := len (logFiles ); {
81+ case n == 0 :
7982 log .Infof (nil , log .TagAppDef , "no log configuration file found, using default logger" )
8083 return nil
81- } else if n > 1 {
84+ case n > 1 :
8285 return util .FormatError (nil , "multiple log files found: %s" , logFiles )
86+ default :
87+ return log .RefreshFile (logFiles [0 ])
8388 }
84-
85- // Refresh the logger configuration from the selected file.
86- return log .RefreshFile (logFiles [0 ])
8789}
0 commit comments