Skip to content

Commit

Permalink
chore(embedded/logging): improve file base logging.
Browse files Browse the repository at this point in the history
- Implement size and age based log rotation
- Add a flag to enable lof of accesses

Signed-off-by: Stefano Scafiti <[email protected]>
  • Loading branch information
ostafen committed Sep 10, 2024
1 parent 67f4b0d commit 1e78388
Show file tree
Hide file tree
Showing 20 changed files with 730 additions and 100 deletions.
4 changes: 2 additions & 2 deletions cmd/immudb/command/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package immudb

import (
"bytes"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -228,7 +227,8 @@ func TestImmudbLogFile(t *testing.T) {
cmd.Flags().StringVar(&config, "config", "", "test")
setupDefaults(server.DefaultOptions())

viper.Set("logfile", filepath.Join(t.TempDir(), "override"))
viper.Set("dir", t.TempDir())
viper.Set("logfile", "override")

cl := Commandline{}

Expand Down
10 changes: 9 additions & 1 deletion cmd/immudb/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ func (cl *Commandline) setupFlags(cmd *cobra.Command, options *server.Options) {

cmd.PersistentFlags().StringVar(&cl.config.CfgFn, "config", "", "config file (default path are configs or $HOME. Default filename is immudb.toml)")
cmd.Flags().String("pidfile", options.Pidfile, "pid path with filename e.g. /var/run/immudb.pid")
cmd.Flags().String("logfile", options.Logfile, "log path with filename e.g. /tmp/immudb/immudb.log")
cmd.Flags().String("logdir", options.LogDir, "log path base dir /tmp/immudb/immulog")
cmd.Flags().String("logfile", options.Logfile, "filename e.g. immudb.log")
cmd.Flags().String("logformat", options.LogFormat, "log format e.g. text/json")
cmd.Flags().Int("log-rotation-size", options.LogRotationSize, "maximum size a log segment can reach before being rotated")
cmd.Flags().Duration("log-rotation-age", options.LogRotationAge, "maximum duration (age) of a log segment before it is rotated")
cmd.Flags().Bool("log-access", options.LogAccess, "log incoming requests information (username, IP, etc...)")
cmd.Flags().BoolP("mtls", "m", false, "enable mutual tls")
cmd.Flags().BoolP("auth", "s", false, "enable auth")
cmd.Flags().Int("max-recv-msg-size", options.MaxRecvMsgSize, "max message size in bytes the server can receive")
Expand Down Expand Up @@ -118,6 +122,10 @@ func setupDefaults(options *server.Options) {
viper.SetDefault("replica", false)
viper.SetDefault("pidfile", options.Pidfile)
viper.SetDefault("logfile", options.Logfile)
viper.SetDefault("logdir", options.LogDir)
viper.SetDefault("log-rotation-size", options.LogRotationSize)
viper.SetDefault("log-rotation-age", options.LogRotationAge)
viper.SetDefault("log-access", options.LogAccess)
viper.SetDefault("mtls", false)
viper.SetDefault("auth", options.GetAuth())
viper.SetDefault("max-recv-msg-size", options.MaxRecvMsgSize)
Expand Down
9 changes: 9 additions & 0 deletions cmd/immudb/command/parse_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func parseOptions() (options *server.Options, err error) {
}

pidfile := viper.GetString("pidfile")

logdir := viper.GetString("logdir")
logfile := viper.GetString("logfile")
logRotationSize := viper.GetInt("log-rotation-size")
logRotationAge := viper.GetDuration("log-rotation-age")
logAccess := viper.GetBool("log-access")
logFormat := viper.GetString("logformat")

mtls := viper.GetBool("mtls")
Expand Down Expand Up @@ -131,7 +136,11 @@ func parseOptions() (options *server.Options, err error) {
WithAddress(address).
WithReplicationOptions(replicationOptions).
WithPidfile(pidfile).
WithLogDir(logdir).
WithLogfile(logfile).
WithLogRotationSize(logRotationSize).
WithLogRotationAge(logRotationAge).
WithLogAccess(logAccess).
WithTLS(tlsConfig).
WithAuth(auth).
WithMaxRecvMsgSize(maxRecvMsgSize).
Expand Down
14 changes: 10 additions & 4 deletions cmd/immudb/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package immudb

import (
"path/filepath"

c "github.com/codenotary/immudb/cmd/helper"
"github.com/codenotary/immudb/embedded/logger"
"github.com/codenotary/immudb/pkg/server"
Expand Down Expand Up @@ -68,10 +70,14 @@ func (cl *Commandline) Immudb(immudbServer server.ImmuServerIf) func(*cobra.Comm

// initialize logger for immudb
ilogger, err := logger.NewLogger(&logger.Options{
Name: "immudb",
LogFormat: options.LogFormat,
LogFile: options.Logfile,
Level: logger.LogLevelFromEnvironment(),
Name: "immudb",
LogFormat: options.LogFormat,
LogDir: filepath.Join(options.Dir, options.LogDir),
LogFile: options.Logfile,
LogRotationSize: options.LogRotationSize,
LogRotationAge: options.LogRotationAge,
LogFileTimeFormat: logger.LogFileFormat,
Level: logger.LogLevelFromEnvironment(),
})
if err != nil {
c.QuitToStdErr(err)
Expand Down
8 changes: 4 additions & 4 deletions embedded/logger/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"path/filepath"
)

// FileLogger ...
// Deprecated: FileLogger is deprecated and will be removed in a future release.
type FileLogger struct {
Logger *log.Logger
LogLevel LogLevel
out *os.File
}

// NewFileLogger ...
// Deprecated: use method NewLogger instead.
func NewFileLogger(name string, file string) (logger Logger, out *os.File, err error) {
out, err = setup(file)
if err != nil {
Expand Down Expand Up @@ -60,12 +60,12 @@ func NewFileLoggerWithLevel(name string, file string, level LogLevel) (logger Lo
func setup(file string) (out *os.File, err error) {
if _, err = os.Stat(filepath.Dir(file)); os.IsNotExist(err) {
if err = os.Mkdir(filepath.Dir(file), os.FileMode(0755)); err != nil {
return nil, errors.New("Unable to create log folder")
return nil, errors.New("unable to create log folder")
}
}
out, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return out, errors.New("Unable to create log file")
return out, errors.New("unable to create log file")
}
return out, err
}
Expand Down
53 changes: 6 additions & 47 deletions embedded/logger/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package logger

import (
"bytes"
"encoding"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -61,7 +60,7 @@ type JsonLogger struct {
timeFnc TimeFunc

mutex sync.Mutex
writer *writer
writer io.Writer

level int32
}
Expand All @@ -77,19 +76,11 @@ func NewJSONLogger(opts *Options) (*JsonLogger, error) {
output = defaultOutput
}

if opts.LogFile != "" {
out, err := setup(opts.LogFile)
if err != nil {
return nil, err
}
output = out
}

l := &JsonLogger{
name: opts.Name,
timeFormat: DefaultTimeFormat,
timeFnc: time.Now,
writer: newWriter(output),
writer: output,
}

if opts.TimeFnc != nil {
Expand Down Expand Up @@ -121,14 +112,12 @@ func (l *JsonLogger) log(name string, level LogLevel, msg string, args ...interf
t := l.timeFnc()

l.logJSON(t, name, level, msg, args...)

l.writer.Flush()
}

func (l *JsonLogger) logJSON(t time.Time, name string, level LogLevel, msg string, args ...interface{}) {
vals := l.getVals(t, name, level, msg)

if args != nil && len(args) > 0 {
if len(args) > 0 {
for i := 0; i < len(args); i = i + 2 {
val := args[i+1]
switch sv := val.(type) {
Expand Down Expand Up @@ -243,38 +232,8 @@ func (i *JsonLogger) Name() string {

// Close the logger
func (i *JsonLogger) Close() error {
return i.writer.Close()
}

type writer struct {
b bytes.Buffer
out io.Writer
}

func newWriter(w io.Writer) *writer {
return &writer{out: w}
}

func (w *writer) Flush() (err error) {
var unwritten = w.b.Bytes()
_, err = w.out.Write(unwritten)
w.b.Reset()
return err
}

func (w *writer) Write(p []byte) (int, error) {
return w.b.Write(p)
}

func (w *writer) Close() error {
switch t := w.out.(type) {
case *os.File:
err := w.Flush()
if err != nil {
return err
}
return t.Close()
default:
return w.Flush()
if wc, ok := i.writer.(io.Closer); ok {
return wc.Close()
}
return nil
}
Loading

0 comments on commit 1e78388

Please sign in to comment.