Extract logic to build log filepath into a function
This will get re-used when parsing logs.
This commit is contained in:
parent
0a895b591e
commit
64b537d799
25
logger.go
25
logger.go
@ -14,7 +14,7 @@ type messageLogger struct {
|
|||||||
conn *upstreamConn
|
conn *upstreamConn
|
||||||
entity string
|
entity string
|
||||||
|
|
||||||
filename string
|
path string
|
||||||
file *os.File
|
file *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +25,16 @@ func newMessageLogger(uc *upstreamConn, entity string) *messageLogger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logPath(network *network, entity string, t time.Time) string {
|
||||||
|
user := network.user
|
||||||
|
srv := user.srv
|
||||||
|
|
||||||
|
// TODO: handle/forbid network/entity names with illegal path characters
|
||||||
|
year, month, day := t.Date()
|
||||||
|
filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
||||||
|
return filepath.Join(srv.LogPath, user.Username, network.GetName(), entity, filename)
|
||||||
|
}
|
||||||
|
|
||||||
func (ml *messageLogger) Append(msg *irc.Message) error {
|
func (ml *messageLogger) Append(msg *irc.Message) error {
|
||||||
s := formatMessage(msg)
|
s := formatMessage(msg)
|
||||||
if s == "" {
|
if s == "" {
|
||||||
@ -36,32 +46,29 @@ func (ml *messageLogger) Append(msg *irc.Message) error {
|
|||||||
// TODO: enforce maximum open file handles (LRU cache of file handles)
|
// TODO: enforce maximum open file handles (LRU cache of file handles)
|
||||||
// TODO: handle non-monotonic clock behaviour
|
// TODO: handle non-monotonic clock behaviour
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
year, month, day := now.Date()
|
path := logPath(ml.conn.network, ml.entity, now)
|
||||||
filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
if ml.path != path {
|
||||||
if ml.filename != filename {
|
|
||||||
if ml.file != nil {
|
if ml.file != nil {
|
||||||
ml.file.Close()
|
ml.file.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle/forbid network/entity names with illegal path characters
|
dir := filepath.Dir(path)
|
||||||
dir := filepath.Join(ml.conn.srv.LogPath, ml.conn.user.Username, ml.conn.network.GetName(), ml.entity)
|
|
||||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
return fmt.Errorf("failed to create logs directory %q: %v", dir, err)
|
return fmt.Errorf("failed to create logs directory %q: %v", dir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join(dir, filename)
|
|
||||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open log file %q: %v", path, err)
|
return fmt.Errorf("failed to open log file %q: %v", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ml.filename = filename
|
ml.path = path
|
||||||
ml.file = f
|
ml.file = f
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s)
|
_, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to log message to %q: %v", ml.filename, err)
|
return fmt.Errorf("failed to log message to %q: %v", ml.path, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user