| 77 | } |
| 78 | |
| 79 | func createDefaultLog() { |
| 80 | |
| 81 | const logname = "opensca.log" |
| 82 | defaultLogPaths := []string{} |
| 83 | |
| 84 | // 在工作目录生成日志 |
| 85 | if p, err := os.Getwd(); err == nil { |
| 86 | defaultLogPaths = append(defaultLogPaths, filepath.Join(p, logname)) |
| 87 | } |
| 88 | |
| 89 | // 在cli目录生成日志 |
| 90 | if execfile, err := os.Executable(); err == nil { |
| 91 | defaultLogPaths = append(defaultLogPaths, filepath.Join(filepath.Dir(execfile), logname)) |
| 92 | } |
| 93 | |
| 94 | // 在用户目录生成日志 |
| 95 | if user, err := user.Current(); err == nil { |
| 96 | defaultLogPaths = append(defaultLogPaths, filepath.Join(user.HomeDir, logname)) |
| 97 | } |
| 98 | |
| 99 | for _, p := range defaultLogPaths { |
| 100 | f, err := os.Create(p) |
| 101 | if err != nil { |
| 102 | Warnf("create log %s err: %s", p, err) |
| 103 | } else { |
| 104 | Debugf("log file: %s", p) |
| 105 | log.SetOutput(f) |
| 106 | LogFilePath = p |
| 107 | break |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | // RegisterOut 注册日志输出格式 |
| 114 | func RegisterOut(outFunc func(level Level, format string, v ...any)) { |