Merge pull request #670 from yousong/feature/yousong-log-file-prefix

cloudcommon: 添加--log-file-prefix选项
This commit is contained in:
yunion-ci-robot
2019-04-29 15:07:12 +08:00
committed by GitHub
20 changed files with 252 additions and 10 deletions
Generated
+3 -2
View File
@@ -1713,14 +1713,14 @@
[[projects]]
branch = "master"
digest = "1:b098c5b757b7622dfeed71f9cdcf0621a42051b88967286b0e1bcfb8a910fc86"
digest = "1:20590bdd812710ad3257e92c23c7d19d9595a48ed69b200ac1d23ac76eee848e"
name = "yunion.io/x/log"
packages = [
".",
"hooks",
]
pruneopts = "UT"
revision = "71a36f55dcfcfa2c8b2477c339e2c7ebd78c731d"
revision = "3c4f24289a1b2fd39b94a9ccbd40931b3a20aab7"
[[projects]]
branch = "master"
@@ -1895,6 +1895,7 @@
"k8s.io/kubernetes/pkg/scheduler/api/v1",
"yunion.io/x/jsonutils",
"yunion.io/x/log",
"yunion.io/x/log/hooks",
"yunion.io/x/pkg/gotypes",
"yunion.io/x/pkg/prettytable",
"yunion.io/x/pkg/trace",
+3
View File
@@ -16,8 +16,11 @@ package main
import (
"yunion.io/x/onecloud/pkg/baremetal/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.New().StartService()
}
+6 -1
View File
@@ -14,8 +14,13 @@
package main
import "yunion.io/x/onecloud/pkg/cloudir/service"
import (
"yunion.io/x/onecloud/pkg/cloudir/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+6 -1
View File
@@ -14,8 +14,13 @@
package main
import "yunion.io/x/onecloud/pkg/cloutpost/service"
import (
"yunion.io/x/onecloud/pkg/cloutpost/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+3
View File
@@ -16,8 +16,11 @@ package main
import (
"yunion.io/x/onecloud/pkg/image/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+6 -1
View File
@@ -14,8 +14,13 @@
package main
import "yunion.io/x/onecloud/pkg/hostimage"
import (
"yunion.io/x/onecloud/pkg/hostimage"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
hostimage.StartService()
}
+6 -1
View File
@@ -14,9 +14,14 @@
package main
import "yunion.io/x/onecloud/pkg/hostman"
import (
"yunion.io/x/onecloud/pkg/hostman"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
var srv = hostman.SHostService{}
srv.StartService()
}
+3
View File
@@ -26,9 +26,12 @@ import (
app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/lbagent"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
opts := &lbagent.Options{}
commonOpts := &opts.CommonOptions
{
+3
View File
@@ -16,8 +16,11 @@ package main
import (
"yunion.io/x/onecloud/pkg/logger/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+3
View File
@@ -34,6 +34,7 @@ import (
_ "github.com/mholt/caddy/startupshutdown"
_ "yunion.io/x/onecloud/pkg/dns"
"yunion.io/x/onecloud/pkg/util/atexit"
)
var directives = []string{
@@ -58,5 +59,7 @@ func init() {
}
func main() {
defer atexit.Handle()
coremain.Run()
}
+3
View File
@@ -16,8 +16,11 @@ package main
import (
"yunion.io/x/onecloud/pkg/compute/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+7 -4
View File
@@ -15,17 +15,20 @@
package main
import (
"os"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/scheduler/service"
"yunion.io/x/onecloud/pkg/util/atexit"
)
func main() {
exitCode := 0
defer func() {
atexit.Exit(exitCode)
}()
if err := service.StartService(); err != nil {
log.Errorln(err)
os.Exit(-1)
exitCode = -1
}
os.Exit(0)
}
+3
View File
@@ -33,6 +33,7 @@ import (
"yunion.io/x/pkg/util/version"
"yunion.io/x/structarg"
"yunion.io/x/onecloud/pkg/util/atexit"
"yunion.io/x/onecloud/pkg/util/fileutils2"
"yunion.io/x/onecloud/pkg/util/nodeid"
"yunion.io/x/onecloud/pkg/util/torrentutils"
@@ -62,6 +63,8 @@ func exitSignalHandlers(client *torrent.Client) {
}
func main() {
defer atexit.Handle()
options := Options{}
parser, err := structarg.NewArgumentParser(&options, "torrent-srv", "bit-torrent server", "2018")
+3
View File
@@ -15,9 +15,12 @@
package main
import (
"yunion.io/x/onecloud/pkg/util/atexit"
"yunion.io/x/onecloud/pkg/webconsole/service"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+3
View File
@@ -20,9 +20,12 @@ Yunion Conf Service
*/
import (
"yunion.io/x/onecloud/pkg/util/atexit"
"yunion.io/x/onecloud/pkg/yunionconf/service"
)
func main() {
defer atexit.Handle()
service.StartService()
}
+27
View File
@@ -16,16 +16,20 @@ package options
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"yunion.io/x/log"
"yunion.io/x/log/hooks"
"yunion.io/x/pkg/util/reflectutils"
"yunion.io/x/pkg/util/version"
"yunion.io/x/pkg/utils"
"yunion.io/x/structarg"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/util/atexit"
)
type CommonOptions struct {
@@ -34,6 +38,7 @@ type CommonOptions struct {
LogLevel string `help:"log level" default:"info" choices:"debug|info|warn|error"`
LogVerboseLevel int `help:"log verbosity level" default:"0"`
LogFilePrefix string `help:"prefix of log files"`
Region string `help:"Region name or ID" alias:"auth-region"`
AuthURL string `help:"Keystone auth URL" alias:"auth-uri"`
@@ -148,6 +153,28 @@ func ParseOptions(optStruct interface{}, args []string, configFileName string, s
if err != nil {
log.Fatalf("Set log level %q: %v", optionsRef.LogLevel, err)
}
if optionsRef.LogFilePrefix != "" {
dir, name := filepath.Split(optionsRef.LogFilePrefix)
h := &hooks.LogFileRotateHook{
RotateNum: 10,
RotateSize: 100 * 1024 * 1024,
LogFileHook: hooks.LogFileHook{
FileDir: dir,
FileName: name,
},
}
h.Init()
log.DisableColors()
log.Logger().AddHook(h)
log.Logger().Out = ioutil.Discard
atexit.Register(atexit.ExitHandler{
Prio: atexit.PRIO_LOG_CLOSE,
Reason: "deinit log rotate hook",
Func: func(atexit.ExitHandler) {
h.DeInit()
},
})
}
log.V(10).Debugf("Parsed options: %#v", optStruct)
+96
View File
@@ -0,0 +1,96 @@
package atexit
import (
"os"
"runtime/debug"
"sort"
"sync"
)
// ExitHandlerFunc is the type of handler func
type ExitHandlerFunc func(ExitHandler)
// ExitHandler defines the spec of handler
//
// Reason and Func are mandatory and must not be empty or nil
//
// Handlers with smaller Prio will be executed earlier than those with bigger
// Prio at exit time. Handler func will receive a copy of the ExitHandler
// struct previously registered
type ExitHandler struct {
Prio int
Reason string
Func ExitHandlerFunc
Value interface{}
}
var (
handlers = map[int][]ExitHandler{}
handlersLock = &sync.Mutex{}
once = &sync.Once{}
)
// Register registers ExitHandler
//
// Smaller prio number mean higher priority and exit handlers with higher
// priority will be executed first. For handlers with equal priorities, those
// registered first will be executed earlier at exit time
func Register(eh ExitHandler) {
if eh.Reason == "" {
panic("handler reason must not be empty")
}
if eh.Func == nil {
panic("handler func must not be nil")
}
handlersLock.Lock()
defer handlersLock.Unlock()
ehs, ok := handlers[eh.Prio]
if ok {
ehs = append(ehs, eh)
} else {
ehs = []ExitHandler{eh}
}
handlers[eh.Prio] = ehs
}
// Handle calls registered handlers sequentially according to priority and
// registration order
//
// Panics caused by handler func will be caught, recorded, then next func will
// be run
func Handle() {
once.Do(func() {
handlersLock.Lock()
defer handlersLock.Unlock()
prios := make([]int, 0, len(handlers))
for prio := range handlers {
prios = append(prios, prio)
}
sort.Ints(prios)
for _, prio := range prios {
ehs := handlers[prio]
for _, eh := range ehs {
print("atexit: prio=", prio, ", reason=", eh.Reason, "\n")
func() {
defer func() {
val := recover()
if val != nil {
print("panic ", val, "\n")
debug.PrintStack()
}
}()
eh.Func(eh)
}()
}
}
})
}
// Exit calls handlers then does os.Exit(code)
func Exit(code int) {
defer os.Exit(code)
Handle()
}
+56
View File
@@ -0,0 +1,56 @@
package atexit
import (
"testing"
)
func TestAtExit(t *testing.T) {
t.Run("empty reason", func(t *testing.T) {
func() {
defer func() {
val := recover()
if val == nil {
t.Errorf("should panic")
}
}()
Register(ExitHandler{})
}()
})
t.Run("empty func", func(t *testing.T) {
func() {
defer func() {
val := recover()
if val == nil {
t.Errorf("should panic")
}
}()
Register(ExitHandler{
Reason: "have reason",
})
}()
})
t.Run("order & prio", func(t *testing.T) {
verdict := ""
handler := func(eh ExitHandler) { verdict += eh.Reason }
Register(ExitHandler{
Prio: 2,
Reason: "2",
Func: handler,
})
Register(ExitHandler{
Prio: 0,
Reason: "0",
Func: handler,
})
Register(ExitHandler{
Prio: 0,
Reason: "1",
Func: handler,
})
Handle()
Handle()
if verdict != "012" {
t.Errorf("expecting %q, got %q", "012", verdict)
}
})
}
+5
View File
@@ -0,0 +1,5 @@
package atexit
const (
PRIO_LOG_CLOSE = 40000
)
+7
View File
@@ -127,6 +127,13 @@ func Fatalln(args ...interface{}) {
logrus.Fatalln(args...)
}
func DisableColors() {
formatter, ok := logger.Formatter.(*TextFormatter)
if ok {
formatter.DisableColors = true
}
}
func AddHookFormatter(logger *logrus.Logger) {
logger.Hooks.Add(new(hooks.CallerHook))