update
This commit is contained in:
parent
b3e74fdb80
commit
9cb6ae5105
|
@ -16,7 +16,7 @@
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
vendor/
|
||||||
|
|
||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -2,4 +2,4 @@ module git.pyer.club/kingecg/godaemon
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require git.pyer.club/kingecg/gologger v1.0.1 // indirect
|
require git.pyer.club/kingecg/gologger v1.0.4 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,2 +1,4 @@
|
||||||
git.pyer.club/kingecg/gologger v1.0.1 h1:snCb0ePlfDUglX+CHwNzq5MRK5uNTnPUks1Dnapl/p8=
|
git.pyer.club/kingecg/gologger v1.0.1 h1:snCb0ePlfDUglX+CHwNzq5MRK5uNTnPUks1Dnapl/p8=
|
||||||
git.pyer.club/kingecg/gologger v1.0.1/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
git.pyer.club/kingecg/gologger v1.0.1/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
||||||
|
git.pyer.club/kingecg/gologger v1.0.4 h1:uSStqVw1H1hPTbRiIhRBOxHMw/tRWMZEvsspIKnNpGk=
|
||||||
|
git.pyer.club/kingecg/gologger v1.0.4/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
||||||
|
|
19
godaemon.go
19
godaemon.go
|
@ -23,6 +23,7 @@ const (
|
||||||
|
|
||||||
type GoDaemon struct {
|
type GoDaemon struct {
|
||||||
pidFile string
|
pidFile string
|
||||||
|
taskPidFile string
|
||||||
flag *string
|
flag *string
|
||||||
sigChan chan os.Signal
|
sigChan chan os.Signal
|
||||||
state string
|
state string
|
||||||
|
@ -41,6 +42,14 @@ func (g *GoDaemon) GetPid() int {
|
||||||
}
|
}
|
||||||
return pid
|
return pid
|
||||||
}
|
}
|
||||||
|
func (g *GoDaemon) GetTaskPid() int {
|
||||||
|
pids, ferr := os.ReadFile(g.taskPidFile)
|
||||||
|
pid, err := strconv.Atoi(string(pids))
|
||||||
|
if err != nil || ferr != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return pid
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GoDaemon) Start() {
|
func (g *GoDaemon) Start() {
|
||||||
if g.flag == nil {
|
if g.flag == nil {
|
||||||
|
@ -93,8 +102,12 @@ func (g *GoDaemon) Start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
waiter := make(chan os.Signal, 1)
|
||||||
g.StartFn(g)
|
g.StartFn(g)
|
||||||
|
g.Info("daemon task is started")
|
||||||
|
<-waiter
|
||||||
|
g.Info("daemon task will be stopped")
|
||||||
|
g.StopFn(g)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +119,7 @@ func (g *GoDaemon) serveSignal() {
|
||||||
g.state = "restart"
|
g.state = "restart"
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Debug("Stop it")
|
g.Running.Process.Signal(syscall.SIGTERM)
|
||||||
g.StopFn(g)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GoDaemon) getDaemonProcess() *os.Process {
|
func (g *GoDaemon) getDaemonProcess() *os.Process {
|
||||||
|
@ -181,6 +193,7 @@ func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon {
|
||||||
execName = strings.TrimSuffix(execName, filepath.Ext(execName))
|
execName = strings.TrimSuffix(execName, filepath.Ext(execName))
|
||||||
}
|
}
|
||||||
godaemon.pidFile = execName + ".pid"
|
godaemon.pidFile = execName + ".pid"
|
||||||
|
godaemon.taskPidFile = execName + ".task.pid"
|
||||||
godaemon.StartFn = start
|
godaemon.StartFn = start
|
||||||
godaemon.StopFn = stop
|
godaemon.StopFn = stop
|
||||||
return godaemon
|
return godaemon
|
||||||
|
|
|
@ -34,7 +34,7 @@ func start(g *godaemon.GoDaemon) {
|
||||||
l := gologger.GetLogger("task")
|
l := gologger.GetLogger("task")
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
l.Debug("task running")
|
l.Debug("task running:", g.GetPid())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,3 +15,4 @@
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
|
log/
|
|
@ -15,6 +15,11 @@ const (
|
||||||
type ConsoleAppender struct {
|
type ConsoleAppender struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close implements LoggerAppender.
|
||||||
|
func (c *ConsoleAppender) Close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ConsoleAppender) GetName() string {
|
func (c *ConsoleAppender) GetName() string {
|
||||||
return "console"
|
return "console"
|
||||||
}
|
}
|
||||||
|
@ -35,9 +40,9 @@ func (c *ConsoleAppender) Append(logEvent LogEvent) {
|
||||||
fmt.Printf(TraceTemplate, logMsg)
|
fmt.Printf(TraceTemplate, logMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func makeConsoleAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
func makeConsoleAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
|
||||||
var appender LoggerAppender = &ConsoleAppender{}
|
var appender LoggerAppender = &ConsoleAppender{}
|
||||||
return appender
|
return &appender
|
||||||
}
|
}
|
||||||
func init() {
|
func init() {
|
||||||
RegistAppender("console", makeConsoleAppender)
|
RegistAppender("console", makeConsoleAppender)
|
||||||
|
|
|
@ -7,14 +7,24 @@ import (
|
||||||
|
|
||||||
type FileAppender struct {
|
type FileAppender struct {
|
||||||
filePath string
|
filePath string
|
||||||
|
lchan chan LogEvent
|
||||||
file *os.File
|
file *os.File
|
||||||
|
stopChan chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements LoggerAppender.
|
||||||
|
func (f *FileAppender) Close() {
|
||||||
|
//send stop signal
|
||||||
|
f.stopChan <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileAppender) GetName() string {
|
func (f *FileAppender) GetName() string {
|
||||||
return "FileAppender:" + f.filePath
|
return "FileAppender:" + f.filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileAppender) Append(logEvent LogEvent) {
|
func (f *FileAppender) start() {
|
||||||
|
f.lchan = make(chan LogEvent, 10)
|
||||||
|
f.stopChan = make(chan struct{})
|
||||||
if f.file == nil || int(f.file.Fd()) == -1 {
|
if f.file == nil || int(f.file.Fd()) == -1 {
|
||||||
dirName := filepath.Dir(f.filePath)
|
dirName := filepath.Dir(f.filePath)
|
||||||
_, err := os.Stat(dirName)
|
_, err := os.Stat(dirName)
|
||||||
|
@ -24,12 +34,26 @@ func (f *FileAppender) Append(logEvent LogEvent) {
|
||||||
f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer f.file.Close()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-f.stopChan:
|
||||||
|
return
|
||||||
|
case logEvent := <-f.lchan:
|
||||||
logMsg := format(logEvent)
|
logMsg := format(logEvent)
|
||||||
f.file.WriteString(logMsg)
|
f.file.WriteString(logMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
func (f *FileAppender) Append(logEvent LogEvent) {
|
||||||
|
|
||||||
|
f.lchan <- logEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeFileAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
func makeFileAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
|
||||||
var logfile interface{}
|
var logfile interface{}
|
||||||
var ok bool
|
var ok bool
|
||||||
logfile, ok = appenderConfig.Options["file"]
|
logfile, ok = appenderConfig.Options["file"]
|
||||||
|
@ -39,7 +63,10 @@ func makeFileAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
||||||
var ret LoggerAppender = &FileAppender{
|
var ret LoggerAppender = &FileAppender{
|
||||||
filePath: logfile.(string),
|
filePath: logfile.(string),
|
||||||
}
|
}
|
||||||
return ret
|
|
||||||
|
ret.(*FileAppender).start()
|
||||||
|
|
||||||
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -2,13 +2,21 @@ package gologger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logTemplate = "[%s] %s : %s - %s\n"
|
const logTemplate = "[%s] %s : %s - %s\n"
|
||||||
|
|
||||||
func format(logEvent LogEvent) string {
|
func format(logEvent LogEvent) string {
|
||||||
data := logEvent.Ts.Format("2006-01-02 15:04:05")
|
data := logEvent.Ts.Format("2006-01-02 15:04:05")
|
||||||
msg := fmt.Sprint(logEvent.Data...)
|
msg := ""
|
||||||
|
firstMsg := logEvent.Data[0]
|
||||||
|
if isFormatString(firstMsg) {
|
||||||
|
msg = fmt.Sprintf(firstMsg.(string), logEvent.Data[1:]...)
|
||||||
|
} else {
|
||||||
|
msg = sprint(logEvent.Data)
|
||||||
|
}
|
||||||
|
|
||||||
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
|
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
@ -16,8 +24,30 @@ func format(logEvent LogEvent) string {
|
||||||
func getLogLevelStr(level int) string {
|
func getLogLevelStr(level int) string {
|
||||||
for name, slevel := range logLevelMap {
|
for name, slevel := range logLevelMap {
|
||||||
if slevel == level {
|
if slevel == level {
|
||||||
return name
|
return strings.ToUpper(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isFormatString(f interface{}) bool {
|
||||||
|
s, ok := f.(string)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 尝试使用空接口来格式化字符串
|
||||||
|
m := fmt.Sprintf(s, []interface{}{}...)
|
||||||
|
return strings.Index(m, "MISSING") != -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func sprint(s []interface{}) string {
|
||||||
|
str := make([]any, len(s))
|
||||||
|
for i, v := range s {
|
||||||
|
if i > 0 {
|
||||||
|
str[i] = fmt.Sprintf(" %v", v)
|
||||||
|
} else {
|
||||||
|
str[i] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprint(str...)
|
||||||
|
}
|
||||||
|
|
|
@ -22,9 +22,9 @@ var logLevelMap map[string]int = map[string]int{
|
||||||
"debug": Debug,
|
"debug": Debug,
|
||||||
"trace": Trace,
|
"trace": Trace,
|
||||||
}
|
}
|
||||||
var loggerMap map[string]Logger = map[string]Logger{}
|
var loggerMap map[string]*Logger = map[string]*Logger{}
|
||||||
var appenderFactoryMap map[string]func(LogAppenderConfig) LoggerAppender = map[string]func(LogAppenderConfig) LoggerAppender{}
|
var appenderFactoryMap map[string]func(LogAppenderConfig) *LoggerAppender = map[string]func(LogAppenderConfig) *LoggerAppender{}
|
||||||
var appenders map[string]LoggerAppender = map[string]LoggerAppender{}
|
var appenders map[string]*LoggerAppender = map[string]*LoggerAppender{}
|
||||||
var loggerConfig LoggersConfig
|
var loggerConfig LoggersConfig
|
||||||
|
|
||||||
type LogAppenderConfig struct {
|
type LogAppenderConfig struct {
|
||||||
|
@ -43,7 +43,7 @@ type LoggersConfig struct {
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
category string
|
category string
|
||||||
level int
|
level int
|
||||||
appenders []LoggerAppender
|
appenders []*LoggerAppender
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogEvent struct {
|
type LogEvent struct {
|
||||||
|
@ -56,13 +56,14 @@ type LogEvent struct {
|
||||||
type LoggerAppender interface {
|
type LoggerAppender interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
Append(logEvent LogEvent)
|
Append(logEvent LogEvent)
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
||||||
var defaultLogger = &Logger{
|
var defaultLogger = &Logger{
|
||||||
|
|
||||||
level: Error,
|
level: Error,
|
||||||
appenders: []LoggerAppender{consoleAppender},
|
appenders: []*LoggerAppender{&consoleAppender},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) log(Level int, msg []interface{}) {
|
func (l *Logger) log(Level int, msg []interface{}) {
|
||||||
|
@ -71,7 +72,7 @@ func (l *Logger) log(Level int, msg []interface{}) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
logEvent := LogEvent{l.category, now, Level, msg}
|
logEvent := LogEvent{l.category, now, Level, msg}
|
||||||
for _, appender := range l.appenders {
|
for _, appender := range l.appenders {
|
||||||
appender.Append(logEvent)
|
(*appender).Append(logEvent)
|
||||||
}
|
}
|
||||||
// l.Appender.Append(logEvent)
|
// l.Appender.Append(logEvent)
|
||||||
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
|
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
|
||||||
|
@ -97,7 +98,7 @@ func (l *Logger) Debug(msg ...interface{}) {
|
||||||
func (l *Logger) Trace(msg ...interface{}) {
|
func (l *Logger) Trace(msg ...interface{}) {
|
||||||
l.log(Trace, msg)
|
l.log(Trace, msg)
|
||||||
}
|
}
|
||||||
func GetLogger(name string) Logger {
|
func GetLogger(name string) *Logger {
|
||||||
if logger, ok := loggerMap[name]; ok {
|
if logger, ok := loggerMap[name]; ok {
|
||||||
return logger
|
return logger
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,29 +107,30 @@ func GetLogger(name string) Logger {
|
||||||
return makeLogger(name, logConfig)
|
return makeLogger(name, logConfig)
|
||||||
}
|
}
|
||||||
if name == "default" {
|
if name == "default" {
|
||||||
return *defaultLogger
|
return defaultLogger
|
||||||
}
|
}
|
||||||
ret := GetLogger("default")
|
l := *GetLogger("default")
|
||||||
ret.category = name
|
l.category = name
|
||||||
return ret
|
loggerMap[name] = &l
|
||||||
|
return &l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeLogger(name string, config LogConfig) Logger {
|
func makeLogger(name string, config LogConfig) *Logger {
|
||||||
logger := &Logger{category: name}
|
logger := &Logger{category: name}
|
||||||
levelstr := strings.ToLower(config.Level)
|
levelstr := strings.ToLower(config.Level)
|
||||||
logger.level = logLevelMap[levelstr]
|
logger.level = logLevelMap[levelstr]
|
||||||
|
|
||||||
if config.Appenders == nil || len(config.Appenders) == 0 {
|
if config.Appenders == nil || len(config.Appenders) == 0 {
|
||||||
logger.appenders = []LoggerAppender{consoleAppender}
|
logger.appenders = []*LoggerAppender{&consoleAppender}
|
||||||
} else {
|
} else {
|
||||||
logger.appenders = make([]LoggerAppender, len(config.Appenders))
|
logger.appenders = make([]*LoggerAppender, len(config.Appenders))
|
||||||
for i, appenderName := range config.Appenders {
|
for i, appenderName := range config.Appenders {
|
||||||
logger.appenders[i] = appenders[appenderName]
|
logger.appenders[i] = appenders[appenderName]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loggerMap[name] = *logger
|
loggerMap[name] = logger
|
||||||
return *logger
|
return logger
|
||||||
}
|
}
|
||||||
func Configure(config LoggersConfig) {
|
func Configure(config LoggersConfig) {
|
||||||
loggerConfig = config
|
loggerConfig = config
|
||||||
|
@ -137,7 +139,7 @@ func Configure(config LoggersConfig) {
|
||||||
if ok {
|
if ok {
|
||||||
appenders[name] = appenderFactory(appenderConfig)
|
appenders[name] = appenderFactory(appenderConfig)
|
||||||
} else {
|
} else {
|
||||||
appenders[name] = &ConsoleAppender{}
|
appenders[name] = &consoleAppender
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for name, _ := range loggerConfig.Categories {
|
for name, _ := range loggerConfig.Categories {
|
||||||
|
@ -145,7 +147,7 @@ func Configure(config LoggersConfig) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) LoggerAppender) {
|
func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) *LoggerAppender) {
|
||||||
|
|
||||||
appenderFactoryMap[typeName] = appenderCreatCb
|
appenderFactoryMap[typeName] = appenderCreatCb
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
# git.pyer.club/kingecg/gologger v1.0.1
|
# git.pyer.club/kingecg/gologger v1.0.4
|
||||||
## explicit; go 1.19
|
## explicit; go 1.19
|
||||||
git.pyer.club/kingecg/gologger
|
git.pyer.club/kingecg/gologger
|
||||||
|
|
Loading…
Reference in New Issue