1
0

Instantiate log file handle and an instance of log.Logger. Also remove reopen SIGHUP handler. Log file will be truncated with logrotate instead of recreating.

This commit is contained in:
Alexander Andreev 2023-05-25 02:25:43 +04:00
parent fccb81d3a5
commit afbaad971a
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -2,7 +2,6 @@ package main
import ( import (
"dwelling-upload/internal/http" "dwelling-upload/internal/http"
"dwelling-upload/pkg/logging"
"dwelling-upload/pkg/utils" "dwelling-upload/pkg/utils"
"dwelling-upload/pkg/watcher" "dwelling-upload/pkg/watcher"
"flag" "flag"
@ -60,11 +59,14 @@ func main() {
} }
logFilePath := path.Join(os.Getenv("LOGS_DIRECTORY"), "file.log") logFilePath := path.Join(os.Getenv("LOGS_DIRECTORY"), "file.log")
logFile, err := logging.New(logFilePath)
logFileFd, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil { if err != nil {
log.Fatalln("file logger:", err) log.Fatalln("failed to open file.log:", err)
} }
defer logFile.Close() defer logFileFd.Close()
logFile := log.New(logFileFd, "", log.LstdFlags)
watcha, err := watcher.NewInotifyWatcher() watcha, err := watcher.NewInotifyWatcher()
if err != nil { if err != nil {
@ -108,14 +110,10 @@ func main() {
doneSignal := make(chan os.Signal, 1) doneSignal := make(chan os.Signal, 1)
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
logReopenSignal := make(chan os.Signal, 1)
signal.Notify(logReopenSignal, syscall.SIGHUP)
go func() { go func() {
for { for {
select { select {
case <-logReopenSignal:
logFile.Reopen(logFilePath)
case <-uploadDirNotify: case <-uploadDirNotify:
uploadDirSize, err = utils.DirectorySize(*uploadDir) uploadDirSize, err = utils.DirectorySize(*uploadDir)
if err != nil { if err != nil {