Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils/logger): adding log system. #106

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package commands

import (
"errors"
"log"
"os"
"os/signal"
"syscall"

"github.com/dezh-tech/immortal/cmd/relay"
"github.com/dezh-tech/immortal/config"
"github.com/dezh-tech/immortal/pkg/logger"
)

func HandleRun(args []string) {
if len(args) < 3 {
ExitOnError(errors.New("at least 1 arguments expected\nuse help command for more information"))
}

log.Println("loading config...")

cfg, err := config.Load(args[2])
if err != nil {
ExitOnError(err)
}

logger.InitGlobalLogger(&cfg.Logger)

r, err := relay.New(cfg)
if err != nil {
ExitOnError(err)
Expand All @@ -35,13 +35,13 @@ func HandleRun(args []string) {

select {
case sig := <-sigChan:
log.Printf("Received signal: %s\nInitiating graceful shutdown...\n", sig.String()) //nolint
logger.Info("Received signal: Initiating graceful shutdown", "signal", sig.String())
if err := r.Stop(); err != nil {
ExitOnError(err)
}

case err := <-errCh:
log.Printf("Unexpected error: %v\nInitiating shutdown due to the error...\n", err) //nolint
logger.Info("Unexpected error: Initiating shutdown due to the error", "err", err)
if err := r.Stop(); err != nil {
ExitOnError(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/commands/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package commands

import (
"log"
"os"

"github.com/dezh-tech/immortal/pkg/logger"
)

func ExitOnError(err error) {
log.Printf("immortal error: %s\n", err.Error()) //nolint
logger.Error("immortal error", "err", err.Error())
os.Exit(1)
}
25 changes: 16 additions & 9 deletions cmd/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package relay
import (
"context"
"fmt"
"log"
"time"

"github.com/dezh-tech/immortal/config"
Expand All @@ -13,6 +12,7 @@ import (
grpcclient "github.com/dezh-tech/immortal/infrastructure/grpc_client"
"github.com/dezh-tech/immortal/infrastructure/metrics"
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/repository"
)

Expand All @@ -39,13 +39,13 @@ func New(cfg *config.Config) (*Relay, error) {
return nil, err
}

c, err := grpcclient.New(cfg.GRPCClient.Endpoint)
c, err := grpcclient.New(cfg.GRPCClient.Endpoint, cfg.GRPCClient)
if err != nil {
return nil, err
}

resp, err := c.RegisterService(context.Background(), fmt.Sprint(cfg.GRPCServer.Port),
cfg.GRPCClient.Region, cfg.GRPCClient.Heartbeat)
cfg.GRPCClient.Region)
if err != nil {
return nil, err
}
Expand All @@ -54,7 +54,9 @@ func New(cfg *config.Config) (*Relay, error) {
return nil, fmt.Errorf("cant register to master: %s", *resp.Message)
}

params, err := c.GetParameters(context.Background(), resp.Token)
c.SetID(resp.Token)

params, err := c.GetParameters(context.Background())
if err != nil {
return nil, err
}
Expand All @@ -64,9 +66,9 @@ func New(cfg *config.Config) (*Relay, error) {
return nil, err
}

h := repository.New(db, cfg.Handler)
h := repository.New(cfg.Handler, db, c)

ws, err := websocket.New(cfg.WebsocketServer, h, m, r)
ws, err := websocket.New(cfg.WebsocketServer, h, m, r, c)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +86,8 @@ func New(cfg *config.Config) (*Relay, error) {

// Start runs the relay and its children.
func (r *Relay) Start() chan error {
log.Println("relay started successfully...")
logger.Info("starting the relay")

errCh := make(chan error, 2)

go func() {
Expand All @@ -104,7 +107,7 @@ func (r *Relay) Start() chan error {

// Stop shutdowns the relay and its children gracefully.
func (r *Relay) Stop() error {
log.Println("stopping relay...")
logger.Info("stopping the relay")

if err := r.websocketServer.Stop(); err != nil {
return err
Expand All @@ -114,5 +117,9 @@ func (r *Relay) Stop() error {
return err
}

return r.database.Stop()
if err := r.database.Stop(); err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dezh-tech/immortal/infrastructure/database"
grpcclient "github.com/dezh-tech/immortal/infrastructure/grpc_client"
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/repository"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
Expand All @@ -21,6 +22,7 @@ type Config struct {
Database database.Config `yaml:"database"`
RedisConf redis.Config `yaml:"redis"`
GRPCServer grpc.Config `yaml:"grpc_server"`
Logger logger.Config `yaml:"logger"`
Handler repository.Config
}

Expand Down
26 changes: 26 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ manager:
# default is global.
region: global

# stack is the stack name which the immortal instance is located in.
# default is immortal.
stack: immortal

# database contains details of database connections and limitations.
database:
# db_name is the name of mongodb related to immortal
Expand Down Expand Up @@ -77,3 +81,25 @@ redis:
# white_list_filter_name specifies the name of whitelist cuckoo filter key
# default is IMMO_WHITE_LIST.
white_list_filter_name: IMMO_WHITE_LIST


# log contains configs for logs output in console and file.
logger:
# level determine the log level.
level: "info"

# filename is the output file containing logs.
filename: "immortal.log"

# max_size_in_mb is the maximum size of log file.
max_size_in_mb: 10

# max_backups determines the maximum number of backup of log file size.
max_backups: 10

# compress determines to compress the log files or not.
compress: true

# targets is targets for logs to be written to.
targets: [file, console]

2 changes: 1 addition & 1 deletion config/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package config
import (
"github.com/dezh-tech/immortal/delivery/websocket"
mpb "github.com/dezh-tech/immortal/infrastructure/grpc_client/gen"
"github.com/dezh-tech/immortal/pkg/utils"
"github.com/dezh-tech/immortal/repository"
"github.com/dezh-tech/immortal/utils"
)

type Parameters struct {
Expand Down
20 changes: 10 additions & 10 deletions delivery/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package grpc

import (
"context"
"log"
"net"
"strconv"
"time"

rpb "github.com/dezh-tech/immortal/delivery/grpc/gen"
"github.com/dezh-tech/immortal/infrastructure/database"
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -44,8 +44,6 @@ func (s *Server) Start() error {
return err
}

log.Println("grpc server started...")

grpcServer := grpc.NewServer(grpc.ChainUnaryInterceptor())

healthServer := newHealthServer(s)
Expand All @@ -55,18 +53,20 @@ func (s *Server) Start() error {
s.listener = listener
s.grpc = grpcServer

return s.grpc.Serve(listener)
logger.Info("gRPC server started successfully", "listen", listener.Addr().String())

if err := s.grpc.Serve(listener); err != nil {
return err
}

return nil
}

func (s *Server) Stop() error {
s.cancel()

log.Println("grpc server stopped...")
logger.Info("stopping gRPC server")

s.cancel()
s.grpc.Stop()
if err := s.listener.Close(); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion delivery/websocket/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package websocket
import (
"fmt"

"github.com/dezh-tech/immortal/pkg/utils"
"github.com/dezh-tech/immortal/types"
"github.com/dezh-tech/immortal/types/message"
"github.com/dezh-tech/immortal/utils"
"github.com/gorilla/websocket"
)

Expand Down
2 changes: 1 addition & 1 deletion delivery/websocket/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"time"

"github.com/dezh-tech/immortal/pkg/utils"
"github.com/dezh-tech/immortal/types/message"
"github.com/dezh-tech/immortal/utils"
"github.com/gorilla/websocket"
)

Expand Down
2 changes: 1 addition & 1 deletion delivery/websocket/req_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package websocket
import (
"fmt"

"github.com/dezh-tech/immortal/pkg/utils"
"github.com/dezh-tech/immortal/types/message"
"github.com/dezh-tech/immortal/utils"
"github.com/gorilla/websocket"
)

Expand Down
30 changes: 22 additions & 8 deletions delivery/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package websocket

import (
"fmt"
"log"
"net"
"net/http"
"strconv"
"sync"

grpcclient "github.com/dezh-tech/immortal/infrastructure/grpc_client"
"github.com/dezh-tech/immortal/infrastructure/metrics"
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/repository"
"github.com/dezh-tech/immortal/types/filter"
"github.com/dezh-tech/immortal/types/message"
Expand All @@ -29,9 +30,11 @@ type Server struct {
handler *repository.Handler
metrics *metrics.Metrics
redis *redis.Redis
grpc grpcclient.IClient
}

func New(cfg Config, h *repository.Handler, m *metrics.Metrics, r *redis.Redis,
func New(cfg Config, h *repository.Handler, m *metrics.Metrics,
r *redis.Redis, grpc grpcclient.IClient,
) (*Server, error) {
return &Server{
config: cfg,
Expand All @@ -40,18 +43,21 @@ func New(cfg Config, h *repository.Handler, m *metrics.Metrics, r *redis.Redis,
handler: h,
metrics: m,
redis: r,
grpc: grpc,
}, nil
}

// Start starts a new server instance.
func (s *Server) Start() error {
log.Println("websocket server started successfully...")

go s.checkExpiration()

addr := net.JoinHostPort(s.config.Bind,
strconv.Itoa(int(s.config.Port)))

logger.Info("websocket server started", "listen", addr)

http.Handle("/", s)
err := http.ListenAndServe(net.JoinHostPort(s.config.Bind, //nolint
strconv.Itoa(int(s.config.Port))), nil)
err := http.ListenAndServe(addr, nil) //nolint

return err
}
Expand All @@ -65,7 +71,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

s.mu.Lock()

log.Println("new websocket connection: ", conn.RemoteAddr().String())
logger.Debug("incoming websocket connection",
"addr", conn.RemoteAddr().String())

s.metrics.Connections.Inc()

known := false
Expand All @@ -88,6 +96,9 @@ func (s *Server) readLoop(conn *websocket.Conn) {
for {
_, buf, err := conn.ReadMessage()
if err != nil {
logger.Debug("failed to read form connection", "conn",
conn.RemoteAddr().String(), "err", err.Error())

// clean up closed connection.
s.mu.Lock()

Expand Down Expand Up @@ -116,6 +127,9 @@ func (s *Server) readLoop(conn *websocket.Conn) {

s.metrics.MessagesTotal.Inc()

logger.Debug("incoming message", "conn",
conn.RemoteAddr().String(), "msg", msg.String())

switch msg.Type() {
case "REQ":
go s.handleReq(conn, msg)
Expand All @@ -137,7 +151,7 @@ func (s *Server) Stop() error {
s.mu.Lock()
defer s.mu.Unlock()

log.Println("stopping websocket server...")
logger.Info("stopping websocket server")

for wsConn, client := range s.conns {
client.Lock()
Expand Down
Loading
Loading