Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dungnt committed Nov 10, 2023
1 parent 78c4978 commit df1fb9f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions logger/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ const (
type destination interface {
log(time.Time, Level, string, ...interface{})
close()
Type() int
}
3 changes: 3 additions & 0 deletions logger/destination_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ func (d *destinationFile) log(t time.Time, level Level, format string, args ...i
func (d *destinationFile) close() {
d.file.Close()
}
func (d *destinationFile) Type() int {
return 0
}
3 changes: 3 additions & 0 deletions logger/destination_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ func (d *destinationStdout) log(t time.Time, level Level, format string, args ..

func (d *destinationStdout) close() {
}
func (d *destinationStdout) Type() int {
return 1
}
4 changes: 4 additions & 0 deletions logger/destination_syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func (d *destinationSysLog) log(t time.Time, level Level, format string, args ..
func (d *destinationSysLog) close() {
d.syslog.Close()
}

func (d *destinationSysLog) Type() int {
return 2
}
5 changes: 5 additions & 0 deletions logger/destination_udplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (d *destinationUdpLog) log(t time.Time, level Level, format string, args ..
func (d *destinationUdpLog) close() {
close(d.done)
}
func (d *destinationUdpLog) Type() int {
return 3
}

func (p *destinationUdpLog) run() {
loop:
Expand Down Expand Up @@ -93,4 +96,6 @@ loop:
}
}
}

fmt.Println("UDP Log Done")
}
26 changes: 26 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ func New2(level Level, destinations []Destination, filePath string, udp_server s
return lh, nil
}

func (p *Logger) EnableUDPLogServer(sv string, server_name string) bool {
dest, err := newDestinationUdpLog(sv, server_name)
if err != nil {
return false
}
p.destinations = append(p.destinations, dest)
return true
}

func removea[T any](slice []T, s int) []T {
return append(slice[:s], slice[s+1:]...)
}

func (p *Logger) DisableUDPLogServer() {
p.mutex.Lock()
defer p.mutex.Unlock()

for i, dest := range p.destinations {
if dest.Type() == 3 {
dest.close()
p.destinations = removea(p.destinations, i)
break
}
}
}

// Close closes a log handler.
func (lh *Logger) Close() {
for _, dest := range lh.destinations {
Expand Down
44 changes: 27 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"github.com/DungntVccorp/libinternal/logger"
redisDB "github.com/DungntVccorp/libinternal/redis_db"
"github.com/redis/go-redis/v9"
"time"

"github.com/DungntVccorp/libinternal/logger"
)

func fnlog() {
_logger, _ := logger.New2(logger.Info, []logger.Destination{logger.DestinationFile, logger.DestinationUdplog, logger.DestinationStdout}, "test.log", "127.0.0.1:44953", "ServerA")
//_logger, _ := logger.New2(logger.Info, []logger.Destination{logger.DestinationFile, logger.DestinationUdplog, logger.DestinationStdout}, "test.log", "10.3.3.115:44953", "ServerA")
_logger, _ := logger.New(logger.Info, []logger.Destination{logger.DestinationFile, logger.DestinationUdplog, logger.DestinationStdout}, "test.log")

//_db := redisDB.New(_logger,&redis.ClusterOptions{
// Addrs: []string{"10.3.3.33:6379"},
Expand All @@ -19,26 +19,36 @@ func fnlog() {
// },
//
//})
_db := redisDB.NewRedisClient(_logger, &redis.UniversalOptions{
Addrs: []string{"10.3.3.33:6379"},
Username: "default",
Password: "123456a@",
DB: 10,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
DialTimeout: time.Second * 5,
PoolSize: 10,
})
_logger.Log(logger.Info, "%v", _db.Run())

// _db := redisDB.NewRedisClient(_logger, &redis.UniversalOptions{
// Addrs: []string{"10.3.3.33:6379"},
// Username: "default",
// Password: "123456a@",
// DB: 10,
// ReadTimeout: time.Second * 10,
// WriteTimeout: time.Second * 10,
// DialTimeout: time.Second * 5,
// PoolSize: 10,
// })
// _logger.Log(logger.Info, "%v", _db.Run())
var _ok bool = false
var c int = 0
for {
var i int = 0
for i < 10 {
_logger.Log(logger.Info, "%v", i)
i++
c += 1
}
_logger.Log(logger.Info, "ABC ")
_logger.Log(logger.Info, "ABC %v", c)
time.Sleep(time.Second * 5)
if !_ok {
_ok = _logger.EnableUDPLogServer("ipcam.vivas.vn:44953", "ServerA")
}

if _ok && c == 30 {
_logger.DisableUDPLogServer()
}

}

}
Expand Down

0 comments on commit df1fb9f

Please sign in to comment.