cloudcommon: 添加--log-file-prefix选项

指定日志文件名,rotate 10,每个100MB
This commit is contained in:
Yousong Zhou
2019-04-28 01:53:39 +00:00
parent 3f69495497
commit fc73a8d811
17 changed files with 95 additions and 10 deletions

5
Gopkg.lock generated
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",

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()
}

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()
}

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()
}

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()
}

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()
}

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()
}

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
{

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()
}

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()
}

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()
}

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)
}

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")

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()
}

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()
}

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)

7
vendor/yunion.io/x/log/log.go generated vendored
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))