Selaa lähdekoodia

new - loglevel (#612)

Ho3ein 1 vuosi sitten
vanhempi
commit
5188d516e3

+ 5 - 4
config/config.go

@@ -16,10 +16,11 @@ var name string
 type LogLevel string
 
 const (
-	Debug LogLevel = "debug"
-	Info  LogLevel = "info"
-	Warn  LogLevel = "warn"
-	Error LogLevel = "error"
+	Debug  LogLevel = "debug"
+	Info   LogLevel = "info"
+	Notice LogLevel = "notice"
+	Warn   LogLevel = "warn"
+	Error  LogLevel = "error"
 )
 
 func GetVersion() string {

+ 12 - 0
logger/logger.go

@@ -84,3 +84,15 @@ func Errorf(format string, args ...interface{}) {
 		logger.Errorf(format, args...)
 	}
 }
+
+func Notice(args ...interface{}) {
+	if logger != nil {
+		logger.Notice(args...)
+	}
+}
+
+func Noticef(format string, args ...interface{}) {
+	if logger != nil {
+		logger.Noticef(format, args...)
+	}
+}

+ 2 - 0
main.go

@@ -27,6 +27,8 @@ func runWebServer() {
 		logger.InitLogger(logging.DEBUG)
 	case config.Info:
 		logger.InitLogger(logging.INFO)
+	case config.Notice:
+		logger.InitLogger(logging.NOTICE)
 	case config.Warn:
 		logger.InitLogger(logging.WARNING)
 	case config.Error:

+ 3 - 3
web/controller/index.go

@@ -64,7 +64,7 @@ func (a *IndexController) login(c *gin.Context) {
 	user := a.userService.CheckUser(form.Username, form.Password, form.LoginSecret)
 	timeStr := time.Now().Format("2006-01-02 15:04:05")
 	if user == nil {
-		logger.Infof("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
+		logger.Warningf("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
 		a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0)
 		pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
 		return
@@ -75,13 +75,13 @@ func (a *IndexController) login(c *gin.Context) {
 
 	sessionMaxAge, err := a.settingService.GetSessionMaxAge()
 	if err != nil {
-		logger.Infof("Unable to get session's max age from DB")
+		logger.Warningf("Unable to get session's max age from DB")
 	}
 
 	if sessionMaxAge > 0 {
 		err = session.SetMaxAge(c, sessionMaxAge*60)
 		if err != nil {
-			logger.Infof("Unable to set session's max age")
+			logger.Warningf("Unable to set session's max age")
 		}
 	}
 

+ 2 - 1
web/controller/server.go

@@ -118,7 +118,8 @@ func (a *ServerController) restartXrayService(c *gin.Context) {
 
 func (a *ServerController) getLogs(c *gin.Context) {
 	count := c.Param("count")
-	logs, err := a.serverService.GetLogs(count)
+	logLevel := c.PostForm("logLevel")
+	logs, err := a.serverService.GetLogs(count, logLevel)
 	if err != nil {
 		jsonMsg(c, "getLogs", err)
 		return

+ 1 - 1
web/html/login.html

@@ -87,7 +87,7 @@
                                 <a-col :span="12">
                                     <a-select ref="selectLang" v-model="lang" @change="setLang(lang)" :dropdown-class-name="themeSwitcher.darkCardClass">
                                         <a-select-option :value="l.value" :label="l.value" v-for="l in supportLangs">
-                                            <span role="img" aria-label="l.name" v-text="l.icon"></span>
+                                            <span role="img" :aria-label="l.name" v-text="l.icon"></span>
                                             &nbsp;&nbsp;<span v-text="l.name"></span>
                                         </a-select-option>
                                     </a-select>

+ 18 - 5
web/html/xui/index.html

@@ -86,7 +86,7 @@
                     <a-col :sm="24" :md="12">
                         <a-card hoverable :class="themeSwitcher.darkCardClass">
                             {{ i18n "menu.link" }}:
-                            <a-tag color="blue" style="cursor: pointer;" @click="openLogs(20)">{{ i18n "pages.index.logs" }}</a-tag>
+                            <a-tag color="blue" style="cursor: pointer;" @click="openLogs(logModal.rows, logModal.logLevel)">{{ i18n "pages.index.logs" }}</a-tag>
                             <a-tag color="blue" style="cursor: pointer;" @click="openConfig">{{ i18n "pages.index.config" }}</a-tag>
                             <a-tag color="blue" style="cursor: pointer;" @click="openBackup">{{ i18n "pages.index.backup" }}</a-tag>
                         </a-card>
@@ -253,7 +253,7 @@
             <a-form-item label="Count">
                 <a-select v-model="logModal.rows"
                 style="width: 80px"
-                @change="openLogs(logModal.rows)"
+                @change="openLogs(logModal.rows, logModal.logLevel)"
                 :dropdown-class-name="themeSwitcher.darkCardClass">
                     <a-select-option value="10">10</a-select-option>
                     <a-select-option value="20">20</a-select-option>
@@ -261,8 +261,20 @@
                     <a-select-option value="100">100</a-select-option>
                 </a-select>
             </a-form-item>
+            <a-form-item label="Log Level">
+                <a-select v-model="logModal.logLevel"
+                style="width: 120px"
+                @change="openLogs(logModal.rows, logModal.logLevel)"
+                :dropdown-class-name="themeSwitcher.darkCardClass">
+                    <a-select-option value="debug">Debug</a-select-option>
+                    <a-select-option value="info">Info</a-select-option>
+                    <a-select-option value="notice">Notice</a-select-option>
+                    <a-select-option value="warning">Warning</a-select-option>
+                    <a-select-option value="err">Error</a-select-option>
+                </a-select>
+            </a-form-item>
             <a-form-item>
-                <button class="ant-btn ant-btn-primary" @click="openLogs(logModal.rows)"><a-icon type="sync"></a-icon> Reload</button>
+                <button class="ant-btn ant-btn-primary" @click="openLogs(logModal.rows, logModal.logLevel)"><a-icon type="sync"></a-icon> Reload</button>
             </a-form-item>
             <a-form-item>
                 <a-button type="primary" style="margin-bottom: 10px;"
@@ -397,6 +409,7 @@
         visible: false,
         logs: '',
         rows: 20,
+        logLevel: 'info',
         show(logs, rows) {
             this.visible = true;
             this.rows = rows;
@@ -501,9 +514,9 @@
                     return;
                 }
             },
-            async openLogs(rows) {
+            async openLogs(rows, logLevel) {
                 this.loading(true);
-                const msg = await HttpUtil.post('server/logs/' + rows);
+                const msg = await HttpUtil.post('server/logs/' + rows, { logLevel: `${logLevel}` });
                 this.loading(false);
                 if (!msg.success) {
                     return;

+ 2 - 2
web/html/xui/settings.html

@@ -116,7 +116,7 @@
                                                     style="width: 100%"
                                                 >
                                                     <a-select-option :value="l.value" :label="l.value" v-for="l in supportLangs">
-                                                        <span role="img" aria-label="l.name" v-text="l.icon"></span>
+                                                        <span role="img" :aria-label="l.name" v-text="l.icon"></span>
                                                         &nbsp;&nbsp;<span v-text="l.name"></span>
                                                     </a-select-option>
                                                 </a-select>
@@ -386,7 +386,7 @@
                                                     style="width: 100%"
                                                 >
                                                     <a-select-option :value="l.value" :label="l.value" v-for="l in supportLangs">
-                                                        <span role="img" aria-label="l.name" v-text="l.icon"></span>
+                                                        <span role="img" :aria-label="l.name" v-text="l.icon"></span>
                                                         &nbsp;&nbsp;<span v-text="l.name"></span>
                                                     </a-select-option>
                                                 </a-select>

+ 1 - 1
web/job/check_client_ip_job.go

@@ -242,7 +242,7 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmai
 
 					disAllowedIps = append(disAllowedIps, ips[limitIp:]...)
 					for i := limitIp; i < len(ips); i++ {
-						logger.Info("[LIMIT_IP] Email=", clientEmail, " SRC=", ips[i])
+						logger.Notice("[LIMIT_IP] Email=", clientEmail, " SRC=", ips[i])
 					}
 				}
 			}

+ 4 - 3
web/service/server.go

@@ -380,16 +380,17 @@ func (s *ServerService) UpdateXray(version string) error {
 
 }
 
-func (s *ServerService) GetLogs(count string) ([]string, error) {
-	// Define the journalctl command and its arguments
+func (s *ServerService) GetLogs(count string, logLevel string) ([]string, error) {
 	var cmdArgs []string
 	if runtime.GOOS == "linux" {
 		cmdArgs = []string{"journalctl", "-u", "x-ui", "--no-pager", "-n", count}
+		if logLevel != "" {
+			cmdArgs = append(cmdArgs, "-p", logLevel)
+		}
 	} else {
 		return []string{"Unsupported operating system"}, nil
 	}
 
-	// Run the command
 	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
 	var out bytes.Buffer
 	cmd.Stdout = &out