revert Config.LogDir for ops log

rotate other req dump logs
don't create the suspicious reqs log file when it is disabled
This commit is contained in:
Azareal 2021-03-24 20:50:22 +10:00
parent d47540391a
commit faa8be12bb
3 changed files with 38 additions and 27 deletions

View File

@ -146,7 +146,7 @@ WriteTimeout - The number of seconds that a route is allowed to run for before t
IdleTimeout - The number of seconds that a Keep-Alive connection will be kept open before being closed. You might to tweak this, if you use Cloudflare or similar. Defaults to 120. IdleTimeout - The number of seconds that a Keep-Alive connection will be kept open before being closed. You might to tweak this, if you use Cloudflare or similar. Defaults to 120.
LogDir - The directory in which logs are stored. Default: ./logs/ LogDir - The directory in which logs are stored, with the exception of ops log, until a related bug is resolved. Default: ./logs/
DisableSuspLog - Whether suspicious requests are logged in the suspicious request logs. Enabling this may make a site faster. Defaults to false. DisableSuspLog - Whether suspicious requests are logged in the suspicious request logs. Enabling this may make a site faster. Defaults to false.

View File

@ -357,7 +357,7 @@ func main() {
// TODO: Have a file for each run with the time/date the server started as the file name? // TODO: Have a file for each run with the time/date the server started as the file name?
// TODO: Log panics with recover() // TODO: Log panics with recover()
f, err := os.OpenFile(c.Config.LogDir+"ops-"+strconv.FormatInt(c.StartTime.Unix(), 10)+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755) f, err := os.OpenFile("./logs/ops-"+strconv.FormatInt(c.StartTime.Unix(), 10)+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -37,7 +37,6 @@ type GenRouter struct {
extraRoutes map[string]func(http.ResponseWriter, *http.Request, *c.User) c.RouteError extraRoutes map[string]func(http.ResponseWriter, *http.Request, *c.User) c.RouteError
reqLogger *log.Logger reqLogger *log.Logger
//suspReqLogger *log.Logger
reqLog2 *RouterLog reqLog2 *RouterLog
suspLog *RouterLog suspLog *RouterLog
@ -57,31 +56,40 @@ type RouterLog struct {
} }
func (r *GenRouter) DailyTick() error { func (r *GenRouter) DailyTick() error {
r.suspLog.Lock() rotateLog := func(l *RouterLog, name string) error {
defer r.suspLog.Unlock() l.Lock()
defer l.Unlock()
f := r.suspLog.FileVal.Load().(*os.File) f := l.FileVal.Load().(*os.File)
stat, err := f.Stat() stat, err := f.Stat()
if err != nil { if err != nil {
return nil
}
if stat.Size() < int64(c.Megabyte) {
return nil
}
if err = f.Close(); err != nil {
return err
}
stimestr := strconv.FormatInt(c.StartTime.Unix(), 10)
f, err = os.OpenFile(c.Config.LogDir+name+stimestr+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755)
if err != nil {
return err
}
lval := log.New(f, "", log.LstdFlags)
l.FileVal.Store(f)
l.LogVal.Store(lval)
return nil return nil
} }
if stat.Size() < int64(c.Megabyte) {
return nil
}
if err = f.Close(); err != nil {
return err
}
stimestr := strconv.FormatInt(c.StartTime.Unix(), 10) if !c.Config.DisableSuspLog {
f, err = os.OpenFile(c.Config.LogDir+"reqs-susp-"+stimestr+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755) err := rotateLog(r.suspLog, "reqs-susp-")
if err != nil { if err != nil {
return err return err
}
} }
l := log.New(f, "", log.LstdFlags) return rotateLog(r.reqLog2, "reqs-")
r.suspLog.FileVal.Store(f)
r.suspLog.LogVal.Store(l)
return nil
} }
func NewGenRouter(uploads http.Handler) (*GenRouter, error) { func NewGenRouter(uploads http.Handler) (*GenRouter, error) {
@ -102,9 +110,12 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
suspReqLog, err := createLog("reqs-susp", stimestr) var suspReqLog *RouterLog
if err != nil { if !c.Config.DisableSuspLog {
return nil, err suspReqLog, err = createLog("reqs-susp", stimestr)
if err != nil {
return nil, err
}
} }
f3, err := os.OpenFile(c.Config.LogDir+"reqs-misc-"+stimestr+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755) f3, err := os.OpenFile(c.Config.LogDir+"reqs-misc-"+stimestr+".log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755)
if err != nil { if err != nil {