Skip to content

Commit

Permalink
feat: improved logging, can change level via LOG_LEVEL env var
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardKnop committed Oct 26, 2024
1 parent b9dabf5 commit 1832551
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cmd/minisql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.uber.org/zap"

"github.com/RichardKnop/minisql/internal/pkg/database"
"github.com/RichardKnop/minisql/internal/pkg/logging"
"github.com/RichardKnop/minisql/internal/pkg/minisql"
"github.com/RichardKnop/minisql/internal/pkg/pager"
"github.com/RichardKnop/minisql/internal/pkg/parser"
Expand Down Expand Up @@ -61,11 +62,26 @@ func doMetaCommand(inputBuffer string) metaCommand {
func main() {
ctx, cancel := context.WithCancel(context.Background())

logger, err := zap.NewDevelopment()
logConf := logging.DefaultConfig()

level := os.Getenv("LOG_LEVEL")
if level == "" {
level = "info"
}

l, err := logging.ParseLevel(level)
if err != nil {
panic(err)
}
logConf.Level = zap.NewAtomicLevelAt(l)

logger, err := logConf.Build()
if err != nil {
panic(err)
}

defer logger.Sync() // flushes buffer, if any

// TODO - hardcoded database for now
dbFile, err := os.OpenFile("db", os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package logging

import (
"strconv"
"strings"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func DefaultConfig() zap.Config {
logConf := zap.NewProductionConfig()
logConf.Sampling = nil
logConf.EncoderConfig.TimeKey = "time"
logConf.EncoderConfig.LevelKey = "severity"
logConf.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logConf.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder

return logConf
}

func ParseLevel(l string) (zapcore.Level, error) {
l = strings.ToLower(strings.TrimSpace(l))
switch l {
case "debug":
return zapcore.DebugLevel, nil
case "info":
return zapcore.InfoLevel, nil
case "warn":
return zapcore.WarnLevel, nil
case "error":
return zapcore.ErrorLevel, nil
case "dpanic":
return zapcore.DPanicLevel, nil
case "panic":
return zapcore.PanicLevel, nil
case "fatal":
return zapcore.FatalLevel, nil
default:
level, err := strconv.ParseInt(l, 10, 8)
if err != nil {
return 0, err
}
return zapcore.Level(level), nil
}
}
19 changes: 17 additions & 2 deletions internal/pkg/minisql/minisql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package minisql

import (
"bytes"
"os"
"time"

"github.com/brianvoe/gofakeit/v7"
"go.uber.org/zap"

"github.com/RichardKnop/minisql/internal/pkg/logging"
)

//go:generate mockery --name=Pager --structname=MockPager --inpackage --case=snake --testonly
Expand Down Expand Up @@ -58,8 +61,20 @@ var (
)

func init() {
var err error
testLogger, err = zap.NewDevelopment()
logConf := logging.DefaultConfig()

level := os.Getenv("LOG_LEVEL")
if level == "" {
level = "debug"
}

l, err := logging.ParseLevel(level)
if err != nil {
panic(err)
}
logConf.Level = zap.NewAtomicLevelAt(l)

testLogger, err = logConf.Build()
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 1832551

Please sign in to comment.