Skip to content

Commit

Permalink
new - loglevel (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
MHSanaei committed Jun 16, 2023
1 parent 97925ee commit 5188d51
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 20 deletions.
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions web/controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}

Expand Down
3 changes: 2 additions & 1 deletion web/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web/html/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h1 class="title">{{ i18n "pages.login.title" }}</h1>
<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>
Expand Down
23 changes: 18 additions & 5 deletions web/html/xui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -253,16 +253,28 @@ <h2>{{ i18n "pages.index.xraySwitchClickDesk"}}</h2>
<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>
<a-select-option value="50">50</a-select-option>
<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;"
Expand Down Expand Up @@ -397,6 +409,7 @@ <h2>{{ i18n "pages.index.xraySwitchClickDesk"}}</h2>
visible: false,
logs: '',
rows: 20,
logLevel: 'info',
show(logs, rows) {
this.visible = true;
this.rows = rows;
Expand Down Expand Up @@ -501,9 +514,9 @@ <h2>{{ i18n "pages.index.xraySwitchClickDesk"}}</h2>
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;
Expand Down
4 changes: 2 additions & 2 deletions web/html/xui/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h2 class="alert-msg">
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>
Expand Down Expand Up @@ -386,7 +386,7 @@ <h2 class="alert-msg">
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>
Expand Down
2 changes: 1 addition & 1 deletion web/job/check_client_ip_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions web/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5188d51

Please sign in to comment.