1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-25 09:36:24 -07:00

Separate access info into separate log file

This commit is contained in:
Eggbertx 2022-09-18 15:32:07 -07:00
parent c005f01b0a
commit 2c3429256f
5 changed files with 45 additions and 23 deletions

View file

@ -64,10 +64,7 @@ func (s gochanServer) serveFile(writer http.ResponseWriter, request *http.Reques
// serve the requested file
fileBytes, _ = os.ReadFile(filePath)
gcutil.Logger().Info().
Str("access", request.URL.Path).
Int("status", 200).
Str("IP", gcutil.GetRealIP(request)).Send()
gcutil.LogAccess(request).Int("status", 200).Send()
writer.Write(fileBytes)
}
@ -187,14 +184,12 @@ func utilHandler(writer http.ResponseWriter, request *http.Request) {
}
if action == "" && deleteBtn != "Delete" && reportBtn != "Report" && editBtn != "Edit" && doEdit != "1" {
gcutil.Logger().Info().
Str("access", request.URL.Path).
Str("IP", gcutil.GetRealIP(request)).
Msg("Received invalid /util request")
gcutil.LogAccess(request).Int("status", 400).Msg("received invalid /util request")
if wantsJSON {
writer.WriteHeader(400)
serverutil.ServeJSON(writer, map[string]interface{}{"error": "Invalid /util request"})
} else {
http.Redirect(writer, request, path.Join(systemCritical.WebRoot, "/"), http.StatusFound)
http.Redirect(writer, request, path.Join(systemCritical.WebRoot, "/"), http.StatusBadRequest)
}
return
}

View file

@ -268,6 +268,10 @@ func InitConfig(versionStr string) {
fmt.Println(err.Error())
os.Exit(1)
}
if err = gcutil.InitAccessLog(path.Join(cfg.LogDir, "gochan_access.log")); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if cfg.Port == 0 {
cfg.Port = 80

View file

@ -26,24 +26,24 @@ func createLuaLogFunc(which string) lua.LGFunction {
args = append(args, l.Get(v))
}
switch which {
case "access":
case "info":
gcutil.LogInfo().
Interface("pluginAccess", args)
Interface("pluginInfo", args)
case "warn":
gcutil.LogWarning().
Interface("pluginWarning", args)
case "error":
gcutil.LogError(nil).
Interface("pluginError", args)
case "staff":
gcutil.LogInfo().
Interface("pluginStaff", args)
}
return 0
}
}
func registerLuaFunctions() {
lState.Register("access_log", createLuaLogFunc("access"))
lState.Register("info_log", createLuaLogFunc("info"))
lState.Register("warn_log", createLuaLogFunc("warn"))
lState.Register("error_log", createLuaLogFunc("error"))
lState.Register("staff_log", createLuaLogFunc("staff"))
lState.SetGlobal("_GOCHAN_VERSION", lua.LString(config.GetVersion().String()))
}

View file

@ -1,14 +1,17 @@
package gcutil
import (
"net/http"
"os"
"github.com/rs/zerolog"
)
var (
logFile *os.File
logger zerolog.Logger
logFile *os.File
accessFile *os.File
logger zerolog.Logger
accessLogger zerolog.Logger
)
type logHook struct{}
@ -24,7 +27,7 @@ func InitLog(logPath string) (err error) {
// log file already initialized, skip
return nil
}
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
return err
}
@ -32,6 +35,19 @@ func InitLog(logPath string) (err error) {
return nil
}
func InitAccessLog(logPath string) (err error) {
if accessFile != nil {
// access log already initialized, skip
return nil
}
accessFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
return err
}
accessLogger = zerolog.New(accessFile).Hook(&logHook{})
return nil
}
func Logger() *zerolog.Logger {
return &logger
}
@ -44,6 +60,16 @@ func LogWarning() *zerolog.Event {
return logger.Warn()
}
func LogAccess(request *http.Request) *zerolog.Event {
ev := accessLogger.Info()
if request != nil {
return ev.
Str("access", request.URL.Path).
Str("IP", GetRealIP(request))
}
return ev
}
func LogError(err error) *zerolog.Event {
if err != nil {
return logger.Err(err).Caller(1)

View file

@ -58,10 +58,7 @@ func ServeNotFound(writer http.ResponseWriter, request *http.Request) {
} else {
MinifyWriter(writer, errorPage, "text/html")
}
gcutil.Logger().Info().
Str("access", request.URL.Path).
Int("status", 404).
Str("IP", gcutil.GetRealIP(request)).Send()
gcutil.LogAccess(request).Int("status", 404).Msg("requested page or resource not found")
}
// DeleteCookie deletes the given cookie if it exists. It returns true if it exists and false