mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
新增日志命令参数
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var OpenConsole bool = true
|
||||||
|
|
||||||
// levels
|
// levels
|
||||||
const (
|
const (
|
||||||
debugLevel = 0
|
debugLevel = 0
|
||||||
@@ -31,6 +33,7 @@ type Logger struct {
|
|||||||
filePath string
|
filePath string
|
||||||
logTime time.Time
|
logTime time.Time
|
||||||
level int
|
level int
|
||||||
|
stdLogger *log.Logger
|
||||||
baseLogger *log.Logger
|
baseLogger *log.Logger
|
||||||
baseFile *os.File
|
baseFile *os.File
|
||||||
flag int
|
flag int
|
||||||
@@ -77,10 +80,10 @@ func New(strLevel string, pathname string, flag int) (*Logger, error) {
|
|||||||
} else {
|
} else {
|
||||||
baseLogger = log.New(os.Stdout, "", flag)
|
baseLogger = log.New(os.Stdout, "", flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// new
|
// new
|
||||||
logger := new(Logger)
|
logger := new(Logger)
|
||||||
logger.level = level
|
logger.level = level
|
||||||
|
logger.stdLogger = log.New(os.Stdout, "", flag)
|
||||||
logger.baseLogger = baseLogger
|
logger.baseLogger = baseLogger
|
||||||
logger.baseFile = baseFile
|
logger.baseFile = baseFile
|
||||||
logger.logTime = now
|
logger.logTime = now
|
||||||
@@ -131,7 +134,9 @@ func (logger *Logger) doPrintf(level int, printLevel string, format string, a ..
|
|||||||
|
|
||||||
format = printLevel + format
|
format = printLevel + format
|
||||||
logger.baseLogger.Output(3, fmt.Sprintf(format, a...))
|
logger.baseLogger.Output(3, fmt.Sprintf(format, a...))
|
||||||
|
if OpenConsole == true {
|
||||||
|
logger.stdLogger.Output(3, fmt.Sprintf(format, a...))
|
||||||
|
}
|
||||||
if level == fatalLevel {
|
if level == fatalLevel {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
84
node/node.go
84
node/node.go
@@ -1,6 +1,7 @@
|
|||||||
package node
|
package node
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/cluster"
|
"github.com/duanhf2012/origin/cluster"
|
||||||
"github.com/duanhf2012/origin/console"
|
"github.com/duanhf2012/origin/console"
|
||||||
@@ -9,6 +10,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
"github.com/duanhf2012/origin/util/timer"
|
"github.com/duanhf2012/origin/util/timer"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
slog "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
@@ -26,6 +28,8 @@ var preSetupService []service.IService //预安装
|
|||||||
var profilerInterval time.Duration
|
var profilerInterval time.Duration
|
||||||
var bValid bool
|
var bValid bool
|
||||||
var configDir = "./config/"
|
var configDir = "./config/"
|
||||||
|
var logLevel string = "debug"
|
||||||
|
var logPath string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
||||||
@@ -33,11 +37,14 @@ func init() {
|
|||||||
sig = make(chan os.Signal, 3)
|
sig = make(chan os.Signal, 3)
|
||||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
|
||||||
|
|
||||||
console.RegisterCommandBool("help",false,"This help.",usage)
|
console.RegisterCommandBool("help",false,"<-help> This help.",usage)
|
||||||
console.RegisterCommandString("start","","Run originserver.",startNode)
|
console.RegisterCommandString("start","","<-start nodeid=nodeid> Run originserver.",startNode)
|
||||||
console.RegisterCommandBool("stop",false,"Stop originserver process",stopNode)
|
console.RegisterCommandBool("stop",false,"<-stop> Stop originserver process.",stopNode)
|
||||||
console.RegisterCommandString("config","","Configuration file path.",setConfigPath)
|
console.RegisterCommandString("config","","<-config path> Configuration file path.",setConfigPath)
|
||||||
console.RegisterCommandString("pprof","","Open performance analysis.",setPprof)
|
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{
|
func usage(val interface{}) error{
|
||||||
@@ -47,7 +54,7 @@ func usage(val interface{}) error{
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, `orgin version: orgin/2.14.20201029
|
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()
|
console.PrintDefaults()
|
||||||
return nil
|
return nil
|
||||||
@@ -144,6 +151,20 @@ func initNode(id int){
|
|||||||
service.Init(closeSig)
|
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() {
|
func Start() {
|
||||||
err := console.Run(os.Args)
|
err := console.Run(os.Args)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
@@ -186,6 +207,11 @@ func startNode(args interface{}) error{
|
|||||||
return fmt.Errorf("invalid option %s",param)
|
return fmt.Errorf("invalid option %s",param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = initLog()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
timer.StartTimer(10*time.Millisecond,100000)
|
timer.StartTimer(10*time.Millisecond,100000)
|
||||||
log.Release("Start running server.")
|
log.Release("Start running server.")
|
||||||
//2.初始化node
|
//2.初始化node
|
||||||
@@ -254,3 +280,49 @@ func OpenProfilerReport(interval time.Duration){
|
|||||||
profilerInterval = interval
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user