fix: Lock block and system log out error

This commit is contained in:
Roi Feng
2025-04-28 21:37:49 -04:00
parent c1efa9a0ca
commit c0524c56e7
4 changed files with 14 additions and 15 deletions

View File

@ -14,9 +14,8 @@ import (
type LogxManager struct {
basePath string
access *os.File
loggers map[string]*zap.Logger
mu sync.Mutex
mu sync.RWMutex
}
func NewManager(base string) *LogxManager {
@ -25,24 +24,18 @@ func NewManager(base string) *LogxManager {
if err := os.MkdirAll(m.basePath, 0744); err != nil {
log.Printf("failed to create base log dir %s: %v", m.basePath, err)
}
accessPath := filepath.Join(m.basePath, "access.log")
f, err := os.OpenFile(accessPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Sprintf("failed to open access.log: %v", err))
}
m.access = f
log.SetOutput(f)
return m
}
func (m *LogxManager) getLogger(host string) *zap.Logger {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
if lg, ok := m.loggers[host]; ok {
m.mu.RUnlock()
return lg
}
m.mu.RUnlock()
m.mu.Lock()
defer m.mu.Unlock()
dir := filepath.Join(m.basePath, host)
if err := os.MkdirAll(dir, 0744); err != nil {
log.Printf("failed to create log dir %s: %v", dir, err)