Skip to content

Commit

Permalink
Use logrus directly
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jun 26, 2024
1 parent 7399a97 commit 6198e8f
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 142 deletions.
10 changes: 5 additions & 5 deletions engine/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

// CommonLogFormat returns a line with the data that is available at the start
Expand Down Expand Up @@ -72,26 +72,26 @@ func (ac *Config) LogAccess(req *http.Request, statusCode int, byteSize int64) {
if ac.commonAccessLogFilename != "" {
f, err := os.OpenFile(ac.commonAccessLogFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Warnf("Can not open %s: %s", ac.commonAccessLogFilename, err)
logrus.Warnf("Can not open %s: %s", ac.commonAccessLogFilename, err)
return
}
defer f.Close()
_, err = f.WriteString(ac.CommonLogFormat(req, statusCode, byteSize) + "\n")
if err != nil {
log.Warnf("Can not write to %s: %s", ac.commonAccessLogFilename, err)
logrus.Warnf("Can not write to %s: %s", ac.commonAccessLogFilename, err)
return
}
}
if ac.combinedAccessLogFilename != "" {
f, err := os.OpenFile(ac.combinedAccessLogFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Warnf("Can not open %s: %s", ac.combinedAccessLogFilename, err)
logrus.Warnf("Can not open %s: %s", ac.combinedAccessLogFilename, err)
return
}
defer f.Close()
_, err = f.WriteString(ac.CombinedLogFormat(req, statusCode, byteSize) + "\n")
if err != nil {
log.Warnf("Can not write to %s: %s", ac.combinedAccessLogFilename, err)
logrus.Warnf("Can not write to %s: %s", ac.combinedAccessLogFilename, err)
return
}
}
Expand Down
32 changes: 16 additions & 16 deletions engine/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/xyproto/algernon/lua/convert"
"github.com/xyproto/algernon/utils"
lua "github.com/xyproto/gopher-lua"
Expand All @@ -41,23 +41,23 @@ func (ac *Config) LoadBasicSystemFunctions(L *lua.LState) {
L.SetGlobal("log", L.NewFunction(func(L *lua.LState) int {
buf := convert.Arguments2buffer(L, false)
// Log the combined text
log.Info(buf.String())
logrus.Info(buf.String())
return 0 // number of results
}))

// Log text with the "Warn" log type
L.SetGlobal("warn", L.NewFunction(func(L *lua.LState) int {
buf := convert.Arguments2buffer(L, false)
// Log the combined text
log.Warn(buf.String())
logrus.Warn(buf.String())
return 0 // number of results
}))

// Log text with the "Error" log type
L.SetGlobal("err", L.NewFunction(func(L *lua.LState) int {
buf := convert.Arguments2buffer(L, false)
// Log the combined text
log.Error(buf.String())
logrus.Error(buf.String())
return 0 // number of results
}))

Expand Down Expand Up @@ -142,7 +142,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("print", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"print\" after closing the connection")
logrus.Error("call to \"print\" after closing the connection")
}
return 0 // number of results
}
Expand All @@ -168,7 +168,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("pprint", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"pprint\" after closing the connection")
logrus.Error("call to \"pprint\" after closing the connection")
}
return 0 // number of results
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("flush", L.NewFunction(func(_ *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"flush\" after closing the connection")
logrus.Error("call to \"flush\" after closing the connection")
}
return 0 // number of results
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("content", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"content\" after closing the connection")
logrus.Error("call to \"content\" after closing the connection")
}
return 0 // number of results
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("setheader", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"setheader\" after closing the connection")
logrus.Error("call to \"setheader\" after closing the connection")
}
return 0 // number of results
}
Expand Down Expand Up @@ -323,7 +323,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("status", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"status\" after closing the connection")
logrus.Error("call to \"status\" after closing the connection")
}
return 0 // number of results
}
Expand All @@ -343,7 +343,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("error", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("call to \"error\" after closing the connection")
logrus.Error("call to \"error\" after closing the connection")
}
return 0 // number of results
}
Expand Down Expand Up @@ -452,7 +452,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
// Log error as warning if there are issues.
// An empty Value map will then be used.
if err != nil {
log.Error(err)
logrus.Error(err)
// return 0
}
} else {
Expand All @@ -475,7 +475,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("redirect", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("redirect after closing the connection")
logrus.Error("redirect after closing the connection")
}
return 0 // number of results
}
Expand All @@ -496,7 +496,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
L.SetGlobal("permanent_redirect", L.NewFunction(func(L *lua.LState) int {
if req.Close {
if ac.debugMode {
log.Error("permanent_redirect after closing the connection")
logrus.Error("permanent_redirect after closing the connection")
}
return 0 // number of results
}
Expand All @@ -516,11 +516,11 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
givenFilename := L.ToString(1)
luaFilename := filepath.Join(filepath.Dir(filename), givenFilename)
if !ac.fs.Exists(luaFilename) {
log.Error("Could not find:", luaFilename)
logrus.Error("Could not find:", luaFilename)
return 0 // number of results
}
if err := L.DoFile(luaFilename); err != nil {
log.Errorf("Error running %s: %s\n", luaFilename, err)
logrus.Errorf("Error running %s: %s\n", luaFilename, err)
return 0 // number of results
}
// Retrieve the returned value from the script
Expand Down
8 changes: 3 additions & 5 deletions engine/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"

"github.com/sirupsen/logrus"
"github.com/xyproto/algernon/cachemode"
"github.com/xyproto/algernon/themes"
"github.com/xyproto/datablock"
Expand Down Expand Up @@ -361,12 +360,11 @@ func (ac *Config) handleFlags(serverTempDir string) {

// CertMagic and Let's Encrypt
if ac.useCertMagic {
log.Info("Use Cert Magic")
logrus.Info("Use Cert Magic")
if dirEntries, err := os.ReadDir(ac.serverDirOrFilename); err != nil {
log.Error("Could not use Cert Magic:" + err.Error())
logrus.Error("Could not use Cert Magic:" + err.Error())
ac.useCertMagic = false
} else {
// log.Infof("Looping over %v files", len(files))
for _, dirEntry := range dirEntries {
basename := filepath.Base(dirEntry.Name())
dirOrSymlink := dirEntry.IsDir() || ((dirEntry.Type() & os.ModeSymlink) == os.ModeSymlink)
Expand Down
22 changes: 11 additions & 11 deletions engine/jsonfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/xyproto/algernon/lua/jnode"
"github.com/xyproto/datablock"
lua "github.com/xyproto/gopher-lua"
Expand Down Expand Up @@ -55,9 +55,9 @@ func jfileAdd(L *lua.LState) int {
err := jfile.AddJSON(jsonpath, []byte(jsondata))
if err != nil {
if top == 2 || strings.HasPrefix(err.Error(), "invalid character") {
log.Error("JSON data: ", err)
logrus.Error("JSON data: ", err)
} else {
log.Error(err)
logrus.Error(err)
}
}
L.Push(lua.LBool(err == nil))
Expand All @@ -74,7 +74,7 @@ func jfileGetString(L *lua.LState) int {
}
val, err := jfile.GetString(jsonpath)
if err != nil {
log.Error(err)
logrus.Error(err)
val = ""
}
L.Push(lua.LString(val))
Expand Down Expand Up @@ -121,14 +121,14 @@ func jfileGet(L *lua.LState) int {
retval = lua.LNil
} else if _, ok := node.CheckMap(); ok {
// Return the JNode instead of converting the map
log.Info("Returning a JSON node instead of a Lua map")
logrus.Info("Returning a JSON node instead of a Lua map")
ud := L.NewUserData()
ud.Value = node
L.SetMetatable(ud, L.GetTypeMetatable(jnode.Class))
retval = ud
// buf.WriteString(fmt.Sprintf("Map with %d elements.", len(m)))
} else if _, ok := node.CheckList(); ok {
log.Info("Returning a JSON node instead of a Lua map")
logrus.Info("Returning a JSON node instead of a Lua map")
// Return the JNode instead of converting the list
ud := L.NewUserData()
ud.Value = node
Expand All @@ -148,7 +148,7 @@ func jfileGet(L *lua.LState) int {
} else if f, ok := node.CheckFloat64(); ok {
retval = lua.LNumber(f)
} else {
log.Error("Unknown JSON node type")
logrus.Error("Unknown JSON node type")
return 0
}
// Return the LValue
Expand All @@ -170,7 +170,7 @@ func jfileSet(L *lua.LState) int {
}
err := jfile.SetString(jsonpath, sval)
if err != nil {
log.Error(err)
logrus.Error(err)
}
L.Push(lua.LBool(err == nil))
return 1 // number of results
Expand All @@ -186,7 +186,7 @@ func jfileDelKey(L *lua.LState) int {
}
err := jfile.DelKey(jsonpath)
if err != nil {
log.Error(err)
logrus.Error(err)
}
L.Push(lua.LBool(nil == err))
return 1 // number of results
Expand Down Expand Up @@ -219,7 +219,7 @@ func constructJFile(L *lua.LState, filename string, fperm os.FileMode, fs *datab
// Create a new JFile
jfile, err := jpath.NewFile(fullFilename)
if err != nil {
log.Error(err)
logrus.Error(err)
return nil, err
}
// Create a new userdata struct
Expand Down Expand Up @@ -256,7 +256,7 @@ func (ac *Config) LoadJFile(L *lua.LState, scriptdir string) {
// Construct a new JFile
userdata, err := constructJFile(L, filepath.Join(scriptdir, filename), ac.defaultPermissions, ac.fs)
if err != nil {
log.Error(err)
logrus.Error(err)
L.Push(lua.LString(err.Error()))
return 1 // Number of returned values
}
Expand Down
10 changes: 5 additions & 5 deletions engine/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"strconv"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/xyproto/algernon/cachemode"
"github.com/xyproto/algernon/lua/codelib"
"github.com/xyproto/algernon/lua/convert"
Expand Down Expand Up @@ -134,7 +134,7 @@ func (ac *Config) RunLua(w http.ResponseWriter, req *http.Request, filename stri
select {
case <-ctx.Done():
// Client is done
log.Warn("Connection to client closed")
logrus.Warn("Connection to client closed")
case <-done:
// We are done
return
Expand Down Expand Up @@ -373,18 +373,18 @@ func (ac *Config) LuaFunctionMap(w http.ResponseWriter, req *http.Request, luada
},
})
if ac.debugMode && ac.verboseMode {
log.Info(utils.Infostring(functionName, args) + " -> (map)")
logrus.Info(utils.Infostring(functionName, args) + " -> (map)")
}
case lv.Type() == lua.LTString:
// lv is a Lua String
retstr := L2.ToString(1)
retval = retstr
if ac.debugMode && ac.verboseMode {
log.Info(utils.Infostring(functionName, args) + " -> \"" + retstr + "\"")
logrus.Info(utils.Infostring(functionName, args) + " -> \"" + retstr + "\"")
}
default:
retval = ""
log.Warn("The return type of " + utils.Infostring(functionName, args) + " can't be converted")
logrus.Warn("The return type of " + utils.Infostring(functionName, args) + " can't be converted")
}
}

Expand Down
Loading

0 comments on commit 6198e8f

Please sign in to comment.