Skip to content

Commit

Permalink
feat: implementing logger (zurvan-lab#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Dec 16, 2023
1 parent cde8be5 commit ca32799
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 113 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Thumbs.db
# dev files
cmd/config.yaml
/client
log.ttrace
2 changes: 2 additions & 0 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/zurvan-lab/TimeTrace/core/database"
"github.com/zurvan-lab/TimeTrace/core/server"
tte "github.com/zurvan-lab/TimeTrace/utils/errors"
ttlog "github.com/zurvan-lab/TimeTrace/utils/log"
)

func RunCommand(parentCmd *cobra.Command) {
Expand All @@ -28,6 +29,7 @@ func RunCommand(parentCmd *cobra.Command) {
}

db := database.Init(cfg)
ttlog.InitGlobalLogger(&cfg.Log)

server := server.NewServer(cfg, db)
if err := server.Start(); err != nil {
Expand Down
16 changes: 12 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ type Server struct {
}

type Log struct {
WriteToFile bool `yaml:"write_to_file"`
Path string `yaml:"path"`
Path string `yaml:"path"`
Colorful bool `yaml:"colorful"`
Compress bool `yaml:"compress"`
MaxAge int `yaml:"max_age"`
MaxBackups int `yaml:"max_backups"`
MaxLogSize int `yaml:"max_log_size"`
}

type User struct {
Expand Down Expand Up @@ -64,8 +68,12 @@ func DefaultConfig() *Config {
Port: "7070",
},
Log: Log{
WriteToFile: true,
Path: "log.ttrace",
Colorful: true,
Compress: true,
MaxAge: 1,
MaxBackups: 10,
MaxLogSize: 10,
Path: "log.ttrace",
},
Name: "time_trace",
}
Expand Down
6 changes: 5 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ server:
port: "7070"

log:
write_to_file: true
path: log.ttrace
colorful: true
compress: true
max_age: 1
max_backups: 10
max_log_size: 10

users:
- name: root
Expand Down
23 changes: 13 additions & 10 deletions core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/zurvan-lab/TimeTrace/core/TQL/execute"
parser "github.com/zurvan-lab/TimeTrace/core/TQL/parser"
"github.com/zurvan-lab/TimeTrace/core/database"
ttlogger "github.com/zurvan-lab/TimeTrace/log"
"github.com/zurvan-lab/TimeTrace/utils/errors"
ttlogger "github.com/zurvan-lab/TimeTrace/utils/log"
)

type Server struct {
Expand All @@ -25,18 +25,21 @@ type Server struct {
ActiveConnsMux sync.Mutex
Config *config.Config

db *database.Database
logger *ttlogger.SubLogger
db *database.Database
}

func NewServer(cfg *config.Config, db *database.Database) *Server {
lna := fmt.Sprintf("%v:%v", cfg.Server.IP, cfg.Server.Port)
sLogger := ttlogger.NewSubLogger("server")

return &Server{
ListenAddress: lna,
QuitChan: make(chan struct{}),
ActiveConnections: make(map[net.Conn]*config.User),
db: db,
Config: cfg,
logger: sLogger,
}
}

Expand All @@ -48,14 +51,14 @@ func (s *Server) Start() error {

s.Listener = listener

ttlogger.Info("server started", "address", s.ListenAddress, "db-name", s.Config.Name)
s.logger.Info("server started", "address", s.ListenAddress, "db-name", s.Config.Name)

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

go func() {
sig := <-signalChan
ttlogger.Info("Received signal, shutting down...", "signal", sig, "db-name", s.Config.Name)
s.logger.Info("Received signal, shutting down...", "signal", sig, "db-name", s.Config.Name)

close(s.QuitChan)
s.Stop()
Expand All @@ -81,20 +84,20 @@ func (s *Server) AcceptConnections() {

conn, err := s.Listener.Accept()
if err != nil {
ttlogger.Error("error accepting connection", "error", err, "db-name", s.Config.Name)
s.logger.Error("error accepting connection", "error", err, "db-name", s.Config.Name)

continue
}

user, err := s.Authenticate(conn)
if err != nil {
ttlogger.Warn("invalid user try to connect", "db-name", s.Config.Name)
s.logger.Warn("invalid user try to connect", "db-name", s.Config.Name)
} else {
s.ActiveConnsMux.Lock()
s.ActiveConnections[conn] = user
s.ActiveConnsMux.Unlock()

ttlogger.Info("new connection", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name)
s.logger.Info("new connection", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name)

s.Wg.Add(1)
go s.HandleConnection(conn)
Expand All @@ -113,7 +116,7 @@ func (s *Server) HandleConnection(conn net.Conn) {

n, err := conn.Read(buffer)
if err != nil {
ttlogger.Error("Connection closed", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name)
s.logger.Error("Connection closed", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name)

s.ActiveConnsMux.Lock()
delete(s.ActiveConnections, conn)
Expand All @@ -130,7 +133,7 @@ func (s *Server) HandleConnection(conn net.Conn) {

_, err = conn.Write([]byte(result))
if err != nil {
ttlogger.Error("Can't write on TCP connection", "error", err, "db-name", s.Config.Name)
s.logger.Error("Can't write on TCP connection", "error", err, "db-name", s.Config.Name)
}
} else {
_, _ = conn.Write([]byte(database.INVALID))
Expand All @@ -143,7 +146,7 @@ func (s *Server) Authenticate(conn net.Conn) (*config.User, error) {

n, err := conn.Read(buffer)
if err != nil {
ttlogger.Error("error reading connection", "error", err, "db-name", s.Config.Name)
s.logger.Error("error reading connection", "error", err, "db-name", s.Config.Name)

_ = conn.Close()
}
Expand Down
10 changes: 7 additions & 3 deletions doc/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ server:

#### log

This part will help you to config log stuff (levels, saving path and...).
This part will help you to config log stuff (saving path, max size and...).

How it looks in `config.yaml`:

```yml
log:
write_to_file: true
path: log.ttrace
path: log.ttrace
colorful: true # should logs be colorful in terminal?
compress: true # should log file backups be compressed?
max_age: 1 # in day. (max age of log backup and files)
max_backups: 10
max_log_size: 10
```

#### users
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.13.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
63 changes: 0 additions & 63 deletions log/logger.go

This file was deleted.

32 changes: 0 additions & 32 deletions log/logger_test.go

This file was deleted.

Loading

0 comments on commit ca32799

Please sign in to comment.