Skip to content

Commit a1860b9

Browse files
lvan100lianghuan
authored andcommitted
refactor(gs): supports multiple log file formats
1 parent 8e820e3 commit a1860b9

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

gs/log.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,62 @@ import (
2828
// initLog initializes the application's logging system.
2929
func 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 {
4749
return util.FormatError(err, "bind error in source sys")
4850
}
4951

50-
var (
51-
logFileDefault = filepath.Join(c.LocalDir, "log.xml")
52-
logFileProfile string
53-
)
52+
extensions := []string{".properties", ".yaml", ".yml", ".xml", ".json"}
5453

55-
// If one or more profiles are set, use the first profile to look
56-
// for a profile-specific log configuration file.
57-
if c.Profiles != "" {
58-
profile := strings.Split(c.Profiles, ",")[0]
59-
logFileProfile = filepath.Join(c.LocalDir, "log-"+profile+".xml")
54+
// Step 3: Build a list of candidate configuration files.
55+
var files []string
56+
if profiles := strings.TrimSpace(c.Profiles); profiles != "" {
57+
for s := range strings.SplitSeq(profiles, ",") { // NOTE: range returns index
58+
if s = strings.TrimSpace(s); s != "" {
59+
for _, ext := range extensions {
60+
files = append(files, filepath.Join(c.LocalDir, "log-"+s+ext))
61+
}
62+
}
63+
}
64+
}
65+
for _, ext := range extensions {
66+
files = append(files, filepath.Join(c.LocalDir, "log"+ext))
6067
}
6168

62-
// Determine which log configuration file to use.
63-
var logFile string
64-
for _, s := range []string{logFileProfile, logFileDefault} {
69+
// Step 4: Detect existing configuration files.
70+
var logFiles []string
71+
for _, s := range files {
6572
if ok, err := util.PathExists(s); err != nil {
6673
return err
67-
} else if !ok {
68-
continue
74+
} else if ok {
75+
logFiles = append(logFiles, s)
6976
}
70-
logFile = s
71-
break
7277
}
7378

74-
// If no configuration file exists, leave the logger as default.
75-
if logFile == "" {
79+
// Step 5: Apply logging configuration or fall back to defaults.
80+
switch n := len(logFiles); {
81+
case n == 0:
82+
log.Infof(nil, log.TagAppDef, "no log configuration file found, using default logger")
7683
return nil
84+
case n > 1:
85+
return util.FormatError(nil, "multiple log files found: %s", logFiles)
86+
default:
87+
return log.RefreshFile(logFiles[0])
7788
}
78-
79-
// Refresh the logger configuration from the selected file.
80-
return log.RefreshFile(logFile)
8189
}

0 commit comments

Comments
 (0)