From df1fb9fef3323937199ca7bb1df5691935413bd1 Mon Sep 17 00:00:00 2001 From: dungnt Date: Fri, 10 Nov 2023 15:17:54 +0700 Subject: [PATCH] update --- logger/destination.go | 1 + logger/destination_file.go | 3 +++ logger/destination_stdout.go | 3 +++ logger/destination_syslog.go | 4 ++++ logger/destination_udplog.go | 5 ++++ logger/logger.go | 26 +++++++++++++++++++++ main.go | 44 ++++++++++++++++++++++-------------- 7 files changed, 69 insertions(+), 17 deletions(-) diff --git a/logger/destination.go b/logger/destination.go index 8e728aa..36792d4 100644 --- a/logger/destination.go +++ b/logger/destination.go @@ -23,4 +23,5 @@ const ( type destination interface { log(time.Time, Level, string, ...interface{}) close() + Type() int } diff --git a/logger/destination_file.go b/logger/destination_file.go index d40c91d..8561dee 100644 --- a/logger/destination_file.go +++ b/logger/destination_file.go @@ -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 +} diff --git a/logger/destination_stdout.go b/logger/destination_stdout.go index 95f76c4..d17ee66 100644 --- a/logger/destination_stdout.go +++ b/logger/destination_stdout.go @@ -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 +} diff --git a/logger/destination_syslog.go b/logger/destination_syslog.go index 1798b1a..d5b27e9 100644 --- a/logger/destination_syslog.go +++ b/logger/destination_syslog.go @@ -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 +} diff --git a/logger/destination_udplog.go b/logger/destination_udplog.go index 8563c95..c03b917 100644 --- a/logger/destination_udplog.go +++ b/logger/destination_udplog.go @@ -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: @@ -93,4 +96,6 @@ loop: } } } + + fmt.Println("UDP Log Done") } diff --git a/logger/logger.go b/logger/logger.go index 2626659..f08951c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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 { diff --git a/main.go b/main.go index e4306b2..cd9512d 100644 --- a/main.go +++ b/main.go @@ -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"}, @@ -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() + } + } }