new - loglevel (#612)

This commit is contained in:
Ho3ein 2023-06-16 18:25:33 +03:30 committed by GitHub
parent 97925eeebe
commit 5188d516e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 20 deletions

View file

@ -18,6 +18,7 @@ type LogLevel string
const ( const (
Debug LogLevel = "debug" Debug LogLevel = "debug"
Info LogLevel = "info" Info LogLevel = "info"
Notice LogLevel = "notice"
Warn LogLevel = "warn" Warn LogLevel = "warn"
Error LogLevel = "error" Error LogLevel = "error"
) )

View file

@ -84,3 +84,15 @@ func Errorf(format string, args ...interface{}) {
logger.Errorf(format, args...) 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...)
}
}

View file

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

View file

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

View file

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

View file

@ -87,7 +87,7 @@
<a-col :span="12"> <a-col :span="12">
<a-select ref="selectLang" v-model="lang" @change="setLang(lang)" :dropdown-class-name="themeSwitcher.darkCardClass"> <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"> <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> &nbsp;&nbsp;<span v-text="l.name"></span>
</a-select-option> </a-select-option>
</a-select> </a-select>

View file

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

View file

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

View file

@ -242,7 +242,7 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmai
disAllowedIps = append(disAllowedIps, ips[limitIp:]...) disAllowedIps = append(disAllowedIps, ips[limitIp:]...)
for i := limitIp; i < len(ips); i++ { 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])
} }
} }
} }

View file

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