Skip to content

Commit

Permalink
✨ feat: add new option FilePerm for custom perm on create file. issues
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 11, 2023
1 parent dc45762 commit 699ecd8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
16 changes: 15 additions & 1 deletion handler/config.go
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"io"
"io/fs"

"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/fsutil"
Expand Down Expand Up @@ -31,6 +32,9 @@ type Config struct {
// Logfile for write logs
Logfile string `json:"logfile" yaml:"logfile"`

// FilePerm for create log file. default rotatefile.DefaultFilePerm
FilePerm fs.FileMode `json:"file_perm" yaml:"file_perm"`

// LevelMode for filter log record. default LevelModeList
LevelMode uint8 `json:"level_mode" yaml:"level_mode"`

Expand Down Expand Up @@ -149,6 +153,9 @@ func (c *Config) CreateWriter() (output SyncCloseWriter, err error) {
if c.Logfile == "" {
return nil, errorx.Raw("slog: logfile cannot be empty for create writer")
}
if c.FilePerm == 0 {
c.FilePerm = rotatefile.DefaultFilePerm
}

// create a rotate config.
if c.MaxSize > 0 || c.RotateTime > 0 {
Expand All @@ -157,6 +164,8 @@ func (c *Config) CreateWriter() (output SyncCloseWriter, err error) {
// has locked on logger.write()
rc.CloseLock = true
rc.Filepath = c.Logfile
rc.FilePerm = c.FilePerm

// copy settings
rc.MaxSize = c.MaxSize
rc.RotateTime = c.RotateTime
Expand All @@ -171,7 +180,7 @@ func (c *Config) CreateWriter() (output SyncCloseWriter, err error) {
// create a rotating writer
output, err = rc.Create()
} else {
output, err = fsutil.QuickOpenFile(c.Logfile)
output, err = fsutil.OpenAppendFile(c.Logfile, c.FilePerm)
}

if err != nil {
Expand Down Expand Up @@ -215,6 +224,11 @@ func WithLogfile(logfile string) ConfigFn {
return func(c *Config) { c.Logfile = logfile }
}

// WithFilePerm setting
func WithFilePerm(filePerm fs.FileMode) ConfigFn {
return func(c *Config) { c.FilePerm = filePerm }
}

// WithLevelMode setting
func WithLevelMode(mode uint8) ConfigFn {
return func(c *Config) { c.LevelMode = mode }
Expand Down
8 changes: 2 additions & 6 deletions handler/file_test.go
Expand Up @@ -14,9 +14,7 @@ import (

func TestNewFileHandler(t *testing.T) {
testFile := "testdata/file.log"
assert.NoErr(t, fsutil.DeleteIfFileExist(testFile))

h, err := handler.NewFileHandler(testFile)
h, err := handler.NewFileHandler(testFile, handler.WithFilePerm(0644))
assert.NoErr(t, err)

l := slog.NewWithHandlers(h)
Expand All @@ -27,10 +25,9 @@ func TestNewFileHandler(t *testing.T) {

assert.True(t, fsutil.IsFile(testFile))

bts, err := ioutil.ReadFile(testFile)
str, err := fsutil.ReadStringOrErr(testFile)
assert.NoErr(t, err)

str := string(bts)
assert.Contains(t, str, "[INFO]")
assert.Contains(t, str, "info message")
assert.Contains(t, str, "[WARNING]")
Expand All @@ -41,7 +38,6 @@ func TestNewFileHandler(t *testing.T) {

func TestMustFileHandler(t *testing.T) {
testFile := "testdata/file-must.log"
assert.NoErr(t, fsutil.DeleteIfFileExist(testFile))

h := handler.MustFileHandler(testFile)
assert.NotEmpty(t, h.Writer())
Expand Down
27 changes: 2 additions & 25 deletions handler/handler.go
Expand Up @@ -56,38 +56,15 @@ type SyncCloseWriter interface {
// - only support set one log level
//
// Deprecated: please use slog.LevelWithFormatter instead.
type LevelWithFormatter struct {
slog.FormattableTrait
// Level for log message. if current level <= Level will log message
Level slog.Level
}

// IsHandling Check if the current level can be handling
func (h *LevelWithFormatter) IsHandling(level slog.Level) bool {
return h.Level.ShouldHandling(level)
}
type LevelWithFormatter = slog.LevelWithFormatter

// LevelsWithFormatter struct definition
//
// - support set log formatter
// - support setting multi log levels
//
// Deprecated: please use slog.LevelsWithFormatter instead.
type LevelsWithFormatter struct {
slog.FormattableTrait
// Levels for log message
Levels []slog.Level
}

// IsHandling Check if the current level can be handling
func (h *LevelsWithFormatter) IsHandling(level slog.Level) bool {
for _, l := range h.Levels {
if l == level {
return true
}
}
return false
}
type LevelsWithFormatter = slog.LevelsWithFormatter

// NopFlushClose no operation.
//
Expand Down
6 changes: 6 additions & 0 deletions handler/handler_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"github.com/gookit/goutil"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/slog"
Expand All @@ -19,6 +20,11 @@ var (
}
)

func TestMain(m *testing.M) {
goutil.PanicErr(fsutil.RemoveSub("./testdata", fsutil.ExcludeNames(".keep")))
m.Run()
}

func TestConfig_CreateWriter(t *testing.T) {
cfg := handler.NewEmptyConfig()

Expand Down
5 changes: 5 additions & 0 deletions rotatefile/config.go
Expand Up @@ -146,6 +146,9 @@ type Config struct {
// Filepath the log file path, will be rotating
Filepath string `json:"filepath" yaml:"filepath"`

// FilePerm for create log file. default DefaultFilePerm
FilePerm os.FileMode `json:"file_perm" yaml:"file_perm"`

// MaxSize file contents max size, unit is bytes.
// If is equals zero, disable rotate file by size
//
Expand Down Expand Up @@ -247,6 +250,7 @@ func NewDefaultConfig() *Config {
BackupTime: DefaultBackTime,
RenameFunc: DefaultFilenameFn,
TimeClock: DefaultTimeClockFn,
FilePerm: DefaultFilePerm,
}
}

Expand All @@ -265,6 +269,7 @@ func EmptyConfigWith(fns ...ConfigFn) *Config {
c := &Config{
RenameFunc: DefaultFilenameFn,
TimeClock: DefaultTimeClockFn,
FilePerm: DefaultFilePerm,
}

return c.With(fns...)
Expand Down
2 changes: 1 addition & 1 deletion rotatefile/writer.go
Expand Up @@ -223,7 +223,7 @@ func (d *Writer) ReopenFile() error {

// ReopenFile the log file
func (d *Writer) openFile() error {
file, err := fsutil.OpenFile(d.cfg.Filepath, DefaultFileFlags, DefaultFilePerm)
file, err := fsutil.OpenFile(d.cfg.Filepath, DefaultFileFlags, d.cfg.FilePerm)
if err != nil {
return err
}
Expand Down

0 comments on commit 699ecd8

Please sign in to comment.