fix: logger syslog format update (#14700)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2022-07-23 12:40:42 +08:00
committed by GitHub
parent b2674489e9
commit 55c198982a
5 changed files with 53 additions and 5 deletions

View File

@@ -83,7 +83,7 @@ func (model *SResourceBase) GetIResourceModel() IResourceModel {
func (model *SResourceBase) MarkDelete() error {
model.Deleted = true
model.DeletedAt = timeutils.UtcNow()
model.DeletedAt = timeutils.UtcNow().Round(time.Second)
return nil
}

View File

@@ -26,7 +26,7 @@ import (
)
var (
sysLog *syslog.Writer
sysLog ISyslogWriter
syslogWorkerMan *appsrv.SWorkerManager
)
@@ -54,7 +54,7 @@ func InitSyslog(url string) error {
}
var err error
sysLog, err = syslog.Dial(proto, addr, syslog.LOG_ERR|syslog.LOG_INFO, tag)
sysLog, err = syslog.Dial(proto, addr, 0, tag)
if err != nil {
return errors.Wrap(err, "syslog.Dial")
}

34
pkg/logger/extern/utils.go vendored Normal file
View File

@@ -0,0 +1,34 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package extern
import (
"strings"
)
type ISyslogWriter interface {
Alert(m string) error
Crit(m string) error
Debug(m string) error
Emerg(m string) error
Err(m string) error
Info(m string) error
Notice(m string) error
Warning(m string) error
}
func FormatMsg(msg string, sep, fill string) string {
return strings.ReplaceAll(strings.ReplaceAll(msg, " ", fill), sep, fill)
}

View File

@@ -181,21 +181,30 @@ func (action *SActionlog) CustomizeCreate(ctx context.Context, userCred mcclient
func (self *SActionlog) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
self.SOpsLog.PostCreate(ctx, userCred, ownerId, query, data)
parts := []string{}
parts = append(parts, "0003")
// 厂商代码
parts = append(parts, options.Options.SyslogVendorCode)
// 事件ID
parts = append(parts, fmt.Sprintf("%d", self.Id))
// 用户名
parts = append(parts, self.User)
// 模块类型
parts = append(parts, self.Service)
// 事件时间
parts = append(parts, timeutils.MysqlTime(self.OpsTime))
// 事件类型
parts = append(parts, self.Action)
succ := "success"
if !self.Success {
succ = "fail"
}
// 事件结果
parts = append(parts, succ)
// 事件描述
notes := fmt.Sprintf("%s %s %s(%s)", self.Action, self.ObjType, self.ObjName, self.ObjId)
if len(self.Notes) > 0 {
notes = fmt.Sprintf("%s: %s", notes, self.Notes)
}
notes = extern.FormatMsg(notes, options.Options.SyslogSeparator, options.Options.SyslogSepEscape)
parts = append(parts, notes)
kind := ""
switch self.Kind {
@@ -206,10 +215,11 @@ func (self *SActionlog) PostCreate(ctx context.Context, userCred mcclient.TokenC
case api.KindIllegal:
kind = "2"
}
// 行为类别
parts = append(parts, kind)
parts = append(parts, "")
msg := strings.Join(parts, "*|")
msg := strings.Join(parts, options.Options.SyslogSeparator)
if len(self.Severity) == 0 {
if self.Success {
extern.Info(msg)

View File

@@ -33,6 +33,10 @@ type SLoggerOptions struct {
AuditorRoleNames []string `help:"role names of auditor admin" default:"sys_adtadmin,domain_adtadmin"`
ActionLogExceedCount int `help:"trigger notification when action log exceed count" default:"-1"`
ActionLogExceedCountNotifyInterval string `help:"trigger notification interval" default:"5m"`
SyslogVendorCode string `help:"vendor code of syslog" default:"0003"`
SyslogSeparator string `help:"syslog message field separator" default:","`
SyslogSepEscape string `help:"syslog message separate escape string" default:"+"`
}
var (