Skip to content

Commit

Permalink
Merge pull request #37 from phires/issue-19-20
Browse files Browse the repository at this point in the history
Issue 19 20
  • Loading branch information
phires authored Jun 10, 2024
2 parents f81a546 + 02208c0 commit cac8807
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
5 changes: 3 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/phires/go-guerrilla/backends"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (d *Daemon) Shutdown() {
// Note: if d.Config is nil, the sets d.Config with the unmarshalled AppConfig which will be returned
func (d *Daemon) LoadConfig(path string) (AppConfig, error) {
var ac AppConfig
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return ac, fmt.Errorf("could not read config file: %s", err.Error())
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/go-sql-driver/mysql v1.8.1
github.com/gomodule/redigo v1.9.2
github.com/pires/go-proxyproto v0.7.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.24.0
Expand Down
28 changes: 20 additions & 8 deletions log/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package log

import (
"bufio"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"

log "github.com/sirupsen/logrus"
)

// Default file mode
const defaultFileMode = 0644

// custom logrus hook

// hookMu ensures all io operations are synced. Always on exported functions
Expand All @@ -26,10 +30,12 @@ type LogrusHook struct {
fd *os.File
// filename to the file descriptor
fname string
// filemode for the logfile. Defaults to 0644
fmode os.FileMode
// txtFormatter that doesn't use colors
plainTxtFormatter *log.TextFormatter

mu sync.Mutex
// unused
// mu sync.Mutex
}

// newLogrusHook creates a new hook. dest can be a file name or one of the following strings:
Expand All @@ -39,7 +45,7 @@ type LogrusHook struct {
func NewLogrusHook(dest string) (LoggerHook, error) {
hookMu.Lock()
defer hookMu.Unlock()
hook := LogrusHook{fname: dest}
hook := LogrusHook{fname: dest, fmode: defaultFileMode}
err := hook.setup(dest)
return &hook, err
}
Expand Down Expand Up @@ -90,7 +96,7 @@ func (hook *LogrusHook) setup(dest string) error {
} else if out == OutputStdout {
hook.w = os.Stdout
} else if out == OutputOff {
hook.w = ioutil.Discard
hook.w = io.Discard
} else {
if _, err := os.Stat(dest); err == nil {
// file exists open the file for appending
Expand All @@ -113,7 +119,7 @@ func (hook *LogrusHook) setup(dest string) error {

// openAppend opens the dest file for appending. Default to os.Stderr if it can't open dest
func (hook *LogrusHook) openAppend(dest string) (err error) {
fd, err := os.OpenFile(dest, os.O_APPEND|os.O_WRONLY, 0644)
fd, err := os.OpenFile(filepath.Clean(dest), os.O_APPEND|os.O_WRONLY, getFileMode(hook.fmode))
if err != nil {
log.WithError(err).Error("Could not open log file for appending")
hook.w = os.Stderr
Expand All @@ -127,7 +133,7 @@ func (hook *LogrusHook) openAppend(dest string) (err error) {

// openCreate creates a new dest file for appending. Default to os.Stderr if it can't open dest
func (hook *LogrusHook) openCreate(dest string) (err error) {
fd, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
fd, err := os.OpenFile(filepath.Clean(dest), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, getFileMode(hook.fmode))
if err != nil {
log.WithError(err).Error("Could not create log file")
hook.w = os.Stderr
Expand Down Expand Up @@ -187,5 +193,11 @@ func (hook *LogrusHook) Reopen() error {
return hook.openAppend(hook.fname)
}
return err
}

// getFileMode returns the file mode or defaultFileMode if fm is 0
// It only exists to be backward compatible to the old behavior, where
// the filemode was hardcoded.
func getFileMode(fm os.FileMode) os.FileMode {
return fm | os.FileMode(defaultFileMode)
}
6 changes: 4 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package log

import (
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net"
"os"
"sync"

log "github.com/sirupsen/logrus"
)

// The following are taken from logrus
Expand Down Expand Up @@ -74,7 +75,8 @@ type HookedLogger struct {
// destination, file name or "stderr", "stdout" or "off"
dest string

oo OutputOption
// unused
//oo OutputOption
}

type loggerKey struct {
Expand Down

0 comments on commit cac8807

Please sign in to comment.