新增日志命令参数

This commit is contained in:
boyce
2020-12-16 17:59:25 +08:00
parent bcd1e9969d
commit 04ff1cc4fb
2 changed files with 85 additions and 8 deletions

View File

@@ -10,6 +10,8 @@ import (
"time"
)
var OpenConsole bool = true
// levels
const (
debugLevel = 0
@@ -31,6 +33,7 @@ type Logger struct {
filePath string
logTime time.Time
level int
stdLogger *log.Logger
baseLogger *log.Logger
baseFile *os.File
flag int
@@ -77,10 +80,10 @@ func New(strLevel string, pathname string, flag int) (*Logger, error) {
} else {
baseLogger = log.New(os.Stdout, "", flag)
}
// new
logger := new(Logger)
logger.level = level
logger.stdLogger = log.New(os.Stdout, "", flag)
logger.baseLogger = baseLogger
logger.baseFile = baseFile
logger.logTime = now
@@ -131,7 +134,9 @@ func (logger *Logger) doPrintf(level int, printLevel string, format string, a ..
format = printLevel + format
logger.baseLogger.Output(3, fmt.Sprintf(format, a...))
if OpenConsole == true {
logger.stdLogger.Output(3, fmt.Sprintf(format, a...))
}
if level == fatalLevel {
os.Exit(1)
}

View File

@@ -1,6 +1,7 @@
package node
import (
"errors"
"fmt"
"github.com/duanhf2012/origin/cluster"
"github.com/duanhf2012/origin/console"
@@ -9,6 +10,7 @@ import (
"github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/util/timer"
"io/ioutil"
slog "log"
"net/http"
_ "net/http/pprof"
"os"
@@ -26,6 +28,8 @@ var preSetupService []service.IService //预安装
var profilerInterval time.Duration
var bValid bool
var configDir = "./config/"
var logLevel string = "debug"
var logPath string
func init() {
@@ -33,11 +37,14 @@ func init() {
sig = make(chan os.Signal, 3)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
console.RegisterCommandBool("help",false,"This help.",usage)
console.RegisterCommandString("start","","Run originserver.",startNode)
console.RegisterCommandBool("stop",false,"Stop originserver process",stopNode)
console.RegisterCommandString("config","","Configuration file path.",setConfigPath)
console.RegisterCommandString("pprof","","Open performance analysis.",setPprof)
console.RegisterCommandBool("help",false,"<-help> This help.",usage)
console.RegisterCommandString("start","","<-start nodeid=nodeid> Run originserver.",startNode)
console.RegisterCommandBool("stop",false,"<-stop> Stop originserver process.",stopNode)
console.RegisterCommandString("config","","<-config path> Configuration file path.",setConfigPath)
console.RegisterCommandString("console", "", "<-console true|false> Turn on or off screen log output.", openConsole)
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
console.RegisterCommandString("pprof","","<-pprof ip:port> Open performance analysis.",setPprof)
}
func usage(val interface{}) error{
@@ -47,7 +54,7 @@ func usage(val interface{}) error{
}
fmt.Fprintf(os.Stderr, `orgin version: orgin/2.14.20201029
Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]
Usage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...
`)
console.PrintDefaults()
return nil
@@ -144,6 +151,20 @@ func initNode(id int){
service.Init(closeSig)
}
func initLog() error{
if logPath == ""{
setLogPath("./log")
}
logger,err := log.New(logLevel,logPath,slog.LstdFlags|slog.Lshortfile)
if err != nil {
fmt.Printf("cannot create log file!\n")
return err
}
log.Export(logger)
return nil
}
func Start() {
err := console.Run(os.Args)
if err!=nil {
@@ -186,6 +207,11 @@ func startNode(args interface{}) error{
return fmt.Errorf("invalid option %s",param)
}
err = initLog()
if err != nil {
return err
}
timer.StartTimer(10*time.Millisecond,100000)
log.Release("Start running server.")
//2.初始化node
@@ -254,3 +280,49 @@ func OpenProfilerReport(interval time.Duration){
profilerInterval = interval
}
func openConsole(args interface{}) error{
if args == "" {
return nil
}
strOpen := strings.ToLower(strings.TrimSpace(args.(string)))
if strOpen == "false" {
log.OpenConsole = false
}else if strOpen == "true" {
log.OpenConsole = true
}else{
return errors.New("Parameter console error!")
}
return nil
}
func setLevel(args interface{}) error{
if args==""{
return nil
}
logLevel = strings.TrimSpace(args.(string))
if logLevel!= "debug" && logLevel!="release"&& logLevel!="warning"&&logLevel!="error"&&logLevel!="fatal" {
return errors.New("unknown level: " + logLevel)
}
return nil
}
func setLogPath(args interface{}) error{
if args == ""{
return nil
}
logPath = strings.TrimSpace(args.(string))
dir, err := os.Stat(logPath) //这个文件夹不存在
if err == nil && dir.IsDir()==false {
return errors.New("Not found dir "+logPath)
}
if err != nil {
err = os.Mkdir(logPath, os.ModePerm)
if err != nil {
return errors.New("Cannot create dir "+logPath)
}
}
return nil
}