Merge pull request #13269 from swordqiu/feature/qj-log-to-syslog-server

feature: support log to remote syslog server
This commit is contained in:
Zexi Li
2022-01-21 10:12:05 +08:00
committed by GitHub
6 changed files with 128 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
// 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 "yunion.io/x/onecloud/pkg/logger/extern"
+67
View File
@@ -0,0 +1,67 @@
// 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 (
"log/syslog"
"strings"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
)
var (
sysLog *syslog.Writer
)
func InitSyslog(url string) error {
proto := "tcp"
addr := url
tag := "cloudaudit"
if strings.HasPrefix(url, "tcp://") {
proto = "tcp"
addr = url[6:]
} else if strings.HasPrefix(url, "udp://") {
proto = "udp"
addr = url[6:]
}
if strings.Contains(addr, "@") {
parts := strings.Split(addr, "@")
addr = parts[0]
tag = parts[1]
}
var err error
sysLog, err = syslog.Dial(proto, addr, syslog.LOG_ERR|syslog.LOG_INFO, tag)
if err != nil {
return errors.Wrap(err, "syslog.Dial")
}
log.Infof("Start syslog to %s://%s@%s", proto, addr, tag)
return nil
}
func Error(msg string) {
if sysLog != nil {
sysLog.Err(msg)
}
}
func Info(msg string) {
if sysLog != nil {
sysLog.Info(msg)
}
}
+11
View File
@@ -16,6 +16,7 @@ package models
import (
"context"
"fmt"
"time"
"yunion.io/x/jsonutils"
@@ -28,6 +29,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/logger/extern"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/logclient"
)
@@ -106,6 +108,15 @@ 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)
msg := fmt.Sprintf("#%d ", self.Id)
msg += fmt.Sprintf("%s %s %s %s %s ", self.Service, self.ObjType, self.ObjName, self.ObjId, self.Action)
msg += fmt.Sprintf("%s %s %s %s %s %s", self.Domain, self.Project, self.User, self.OwnerDomainId, self.OwnerProjectId, self.Notes)
if self.Success {
extern.Info(msg)
} else {
extern.Error(msg)
}
for k, v := range map[string]string{
"service": self.Service,
"action": self.Action,
+27
View File
@@ -16,14 +16,41 @@ package options
import (
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/logger/extern"
)
type SLoggerOptions struct {
common_options.CommonOptions
common_options.DBOptions
SyslogUrl string `help:"external syslog url, e.g. tcp://localhost:1234@cloud"`
}
var (
Options SLoggerOptions
)
func OnOptionsChange(oldOptions, newOptions interface{}) bool {
oldOpts := oldOptions.(*SLoggerOptions)
newOpts := newOptions.(*SLoggerOptions)
changed := false
if common_options.OnBaseOptionsChange(&oldOpts.BaseOptions, &newOpts.BaseOptions) {
changed = true
}
if common_options.OnDBOptionsChange(&oldOpts.DBOptions, &newOpts.DBOptions) {
changed = true
}
if oldOpts.SyslogUrl != newOpts.SyslogUrl {
err := extern.InitSyslog(newOpts.SyslogUrl)
if err != nil {
// reset syslog writer error, restart the service to take effect
changed = true
}
}
return changed
}
+2
View File
@@ -36,6 +36,8 @@ func initHandlers(app *appsrv.Application) {
}
for _, manager := range []db.IModelManager{
db.Metadata,
models.ActionLog,
models.BaremetalEventManager,
} {
+6
View File
@@ -26,6 +26,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/logger/extern"
"yunion.io/x/onecloud/pkg/logger/models"
"yunion.io/x/onecloud/pkg/logger/options"
_ "yunion.io/x/onecloud/pkg/logger/policy"
@@ -44,6 +45,7 @@ func StartService() {
app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
})
common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, "", options.OnOptionsChange)
app := app_common.InitApp(baseOpts, true)
@@ -56,5 +58,9 @@ func StartService() {
// models.StartNotifyToWebsocketWorker()
if len(opts.SyslogUrl) > 0 {
extern.InitSyslog(opts.SyslogUrl)
}
app_common.ServeForever(app, baseOpts)
}