Change to own Logger

This commit is contained in:
Thomas Roehl 2022-01-25 16:40:02 +01:00
parent 99aaece6c2
commit bafc6322e6
4 changed files with 140 additions and 32 deletions

View File

@ -2,13 +2,13 @@ package collectors
import ( import (
"encoding/json" "encoding/json"
"log"
"os" "os"
"sync" "sync"
"time" "time"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker" mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
) )
var AvailableCollectors = map[string]MetricCollector{ var AvailableCollectors = map[string]MetricCollector{
@ -58,29 +58,29 @@ func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Durat
cm.duration = duration cm.duration = duration
configFile, err := os.Open(collectConfigFile) configFile, err := os.Open(collectConfigFile)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return err return err
} }
defer configFile.Close() defer configFile.Close()
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&cm.config) err = jsonParser.Decode(&cm.config)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return err return err
} }
for k, cfg := range cm.config { for k, cfg := range cm.config {
log.Print(k, " ", cfg)
if _, found := AvailableCollectors[k]; !found { if _, found := AvailableCollectors[k]; !found {
log.Print("[CollectorManager] SKIP unknown collector ", k) cclog.ComponentPrint("CollectorManager", "SKIP unknown collector ", k)
continue continue
} }
c := AvailableCollectors[k] c := AvailableCollectors[k]
err = c.Init(cfg) err = c.Init(cfg)
if err != nil { if err != nil {
log.Print("[CollectorManager] Collector ", k, "initialization failed: ", err.Error()) cclog.ComponentPrint("CollectorManager", "Collector ", k, "initialization failed: ", err.Error())
continue continue
} }
cclog.ComponentDebug("CollectorManager", "Collector ", k, "initialized")
cm.collectors = append(cm.collectors, c) cm.collectors = append(cm.collectors, c)
} }
return nil return nil
@ -99,7 +99,7 @@ func (cm *collectorManager) Start() {
c.Close() c.Close()
} }
cm.wg.Done() cm.wg.Done()
log.Print("[CollectorManager] DONE\n") cclog.ComponentPrint("CollectorManager", "DONE")
break CollectorManagerLoop break CollectorManagerLoop
case t := <-tick: case t := <-tick:
for _, c := range cm.collectors { for _, c := range cm.collectors {
@ -110,18 +110,17 @@ func (cm *collectorManager) Start() {
c.Close() c.Close()
} }
cm.wg.Done() cm.wg.Done()
log.Print("[CollectorManager] DONE\n") cclog.ComponentPrint("CollectorManager", "DONE")
break CollectorManagerInputLoop break CollectorManagerInputLoop
default: default:
log.Print("[CollectorManager] ", c.Name(), " ", t) cclog.ComponentPrint("CollectorManager", c.Name(), " ", t)
c.Read(cm.duration, cm.output) c.Read(cm.duration, cm.output)
} }
} }
} }
} }
log.Print("[CollectorManager] EXIT\n")
}() }()
log.Print("[CollectorManager] STARTED\n") cclog.ComponentPrint("CollectorManager", "STARTED")
} }
func (cm *collectorManager) AddOutput(output chan lp.CCMetric) { func (cm *collectorManager) AddOutput(output chan lp.CCMetric) {
@ -130,7 +129,7 @@ func (cm *collectorManager) AddOutput(output chan lp.CCMetric) {
func (cm *collectorManager) Close() { func (cm *collectorManager) Close() {
cm.done <- true cm.done <- true
log.Print("[CollectorManager] CLOSE") cclog.ComponentPrint("CollectorManager", "CLOSE")
} }
func New(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) { func New(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) {

1
go.mod
View File

@ -9,5 +9,6 @@ require (
github.com/nats-io/nats.go v1.10.0 github.com/nats-io/nats.go v1.10.0
github.com/nats-io/nkeys v0.1.4 // indirect github.com/nats-io/nkeys v0.1.4 // indirect
github.com/prometheus/client_golang v1.10.0 // indirect github.com/prometheus/client_golang v1.10.0 // indirect
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2
gopkg.in/Knetic/govaluate.v2 v2.3.0 gopkg.in/Knetic/govaluate.v2 v2.3.0
) )

View File

@ -0,0 +1,111 @@
package cclogger
import (
"fmt"
"runtime"
"os"
"log"
)
var (
globalDebug = false
stdout = os.Stdout
stderr = os.Stderr
debugLog *log.Logger = nil
infoLog *log.Logger = nil
errorLog *log.Logger = nil
warnLog *log.Logger = nil
defaultLog *log.Logger = nil
)
func initLogger() {
if debugLog == nil {
debugLog = log.New(stderr, "DEBUG", log.LstdFlags)
}
if infoLog == nil {
infoLog = log.New(stdout, "INFO", log.LstdFlags)
}
if errorLog == nil {
errorLog = log.New(stderr, "ERROR", log.LstdFlags)
}
if warnLog == nil {
warnLog = log.New(stderr, "WARN", log.LstdFlags)
}
if defaultLog == nil {
defaultLog = log.New(stdout, "", log.LstdFlags)
}
}
func CCPrint(logger *log.Logger, e ... interface {}) {
if logger != nil {
logger.Print(e)
}
}
func Print(e ... interface{}) {
CCPrint(defaultLog, e)
}
func ComponentPrint(component string, e ... interface{}) {
CCPrint(defaultLog, fmt.Sprintf("[%s]", component), e)
}
func Info(e ... interface{}) {
CCPrint(infoLog, e)
}
func ComponentInfo(component string, e ... interface{}) {
CCPrint(infoLog, fmt.Sprintf("[%s]", component), e)
}
func Debug(e ... interface{}) {
if globalDebug {
CCPrint(debugLog, e)
}
}
func ComponentDebug(component string, e ... interface{}) {
if globalDebug {
CCPrint(debugLog, fmt.Sprintf("[%s]", component), e)
}
}
func Error(e ... interface{}) {
_, fn, line, _ := runtime.Caller(1)
CCPrint(errorLog, fn, line, e)
}
func ComponentError(component string, e ... interface{}) {
_, fn, line, _ := runtime.Caller(1)
CCPrint(errorLog, fmt.Sprintf("[%s]", component), fn, line, e)
}
func SetDebug() {
globalDebug = true
}
func SetOutput(filename string) {
if filename == "stderr" {
if stderr != os.Stderr && stderr != os.Stdout {
stderr.Close()
}
stderr = os.Stderr
} else if filename == "stdout" {
if stderr != os.Stderr && stderr != os.Stdout {
stderr.Close()
}
stderr = os.Stdout
} else {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
defer file.Close()
stderr = file
}
}
debugLog = nil
errorLog = nil
warnLog = nil
initLogger()
}

View File

@ -3,8 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" // "log"
"log"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -17,7 +16,7 @@ import (
"sync" "sync"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
mr "github.com/ClusterCockpit/cc-metric-collector/internal/metricRouter" mr "github.com/ClusterCockpit/cc-metric-collector/internal/metricRouter"
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker" mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
@ -37,7 +36,7 @@ func LoadCentralConfiguration(file string, config *CentralConfigFile) error {
configFile, err := os.Open(file) configFile, err := os.Open(file)
defer configFile.Close() defer configFile.Close()
if err != nil { if err != nil {
fmt.Println(err.Error()) cclog.Error(err.Error())
return err return err
} }
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
@ -151,21 +150,21 @@ func ReadCli() map[string]string {
// General shutdown function that gets executed in case of interrupt or graceful shutdown // General shutdown function that gets executed in case of interrupt or graceful shutdown
func shutdown(config *RuntimeConfig) { func shutdown(config *RuntimeConfig) {
log.Print("Shutdown...") cclog.Info("Shutdown...")
if config.CollectManager != nil { if config.CollectManager != nil {
log.Print("Shutdown CollectManager...") cclog.Debug("Shutdown CollectManager...")
config.CollectManager.Close() config.CollectManager.Close()
} }
if config.ReceiveManager != nil { if config.ReceiveManager != nil {
log.Print("Shutdown ReceiveManager...") cclog.Debug("Shutdown ReceiveManager...")
config.ReceiveManager.Close() config.ReceiveManager.Close()
} }
if config.Router != nil { if config.Router != nil {
log.Print("Shutdown Router...") cclog.Debug("Shutdown Router...")
config.Router.Close() config.Router.Close()
} }
if config.SinkManager != nil { if config.SinkManager != nil {
log.Print("Shutdown SinkManager...") cclog.Debug("Shutdown SinkManager...")
config.SinkManager.Close() config.SinkManager.Close()
} }
@ -184,7 +183,6 @@ func prepare_shutdown(config *RuntimeConfig) {
go func(config *RuntimeConfig) { go func(config *RuntimeConfig) {
<-sigs <-sigs
log.Print("Shutdown...")
shutdown(config) shutdown(config)
}(config) }(config)
} }
@ -199,24 +197,23 @@ func mainFunc() int {
// Load and check configuration // Load and check configuration
err = LoadCentralConfiguration(rcfg.CliArgs["configfile"], &rcfg.ConfigFile) err = LoadCentralConfiguration(rcfg.CliArgs["configfile"], &rcfg.ConfigFile)
if err != nil { if err != nil {
log.Print("Error reading configuration file ", rcfg.CliArgs["configfile"]) cclog.Error("Error reading configuration file ", rcfg.CliArgs["configfile"], ": ", err.Error())
log.Print(err.Error())
return 1 return 1
} }
if rcfg.ConfigFile.Interval <= 0 || time.Duration(rcfg.ConfigFile.Interval)*time.Second <= 0 { if rcfg.ConfigFile.Interval <= 0 || time.Duration(rcfg.ConfigFile.Interval)*time.Second <= 0 {
log.Print("Configuration value 'interval' must be greater than zero") cclog.Error("Configuration value 'interval' must be greater than zero")
return 1 return 1
} }
rcfg.Interval = time.Duration(rcfg.ConfigFile.Interval) * time.Second rcfg.Interval = time.Duration(rcfg.ConfigFile.Interval) * time.Second
if rcfg.ConfigFile.Duration <= 0 || time.Duration(rcfg.ConfigFile.Duration)*time.Second <= 0 { if rcfg.ConfigFile.Duration <= 0 || time.Duration(rcfg.ConfigFile.Duration)*time.Second <= 0 {
log.Print("Configuration value 'duration' must be greater than zero") cclog.Error("Configuration value 'duration' must be greater than zero")
return 1 return 1
} }
rcfg.Duration = time.Duration(rcfg.ConfigFile.Duration) * time.Second rcfg.Duration = time.Duration(rcfg.ConfigFile.Duration) * time.Second
rcfg.Hostname, err = os.Hostname() rcfg.Hostname, err = os.Hostname()
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return 1 return 1
} }
// Drop domain part of host name // Drop domain part of host name
@ -231,14 +228,14 @@ func mainFunc() int {
if len(rcfg.ConfigFile.RouterConfigFile) > 0 { if len(rcfg.ConfigFile.RouterConfigFile) > 0 {
rcfg.Router, err = mr.New(rcfg.Ticker, &rcfg.Sync, rcfg.ConfigFile.RouterConfigFile) rcfg.Router, err = mr.New(rcfg.Ticker, &rcfg.Sync, rcfg.ConfigFile.RouterConfigFile)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return 1 return 1
} }
} }
if len(rcfg.ConfigFile.SinkConfigFile) > 0 { if len(rcfg.ConfigFile.SinkConfigFile) > 0 {
rcfg.SinkManager, err = sinks.New(&rcfg.Sync, rcfg.ConfigFile.SinkConfigFile) rcfg.SinkManager, err = sinks.New(&rcfg.Sync, rcfg.ConfigFile.SinkConfigFile)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return 1 return 1
} }
RouterToSinksChannel := make(chan lp.CCMetric) RouterToSinksChannel := make(chan lp.CCMetric)
@ -248,7 +245,7 @@ func mainFunc() int {
if len(rcfg.ConfigFile.CollectorConfigFile) > 0 { if len(rcfg.ConfigFile.CollectorConfigFile) > 0 {
rcfg.CollectManager, err = collectors.New(rcfg.Ticker, rcfg.Duration, &rcfg.Sync, rcfg.ConfigFile.CollectorConfigFile) rcfg.CollectManager, err = collectors.New(rcfg.Ticker, rcfg.Duration, &rcfg.Sync, rcfg.ConfigFile.CollectorConfigFile)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return 1 return 1
} }
CollectToRouterChannel := make(chan lp.CCMetric) CollectToRouterChannel := make(chan lp.CCMetric)
@ -258,7 +255,7 @@ func mainFunc() int {
if len(rcfg.ConfigFile.ReceiverConfigFile) > 0 { if len(rcfg.ConfigFile.ReceiverConfigFile) > 0 {
rcfg.ReceiveManager, err = receivers.New(&rcfg.Sync, rcfg.ConfigFile.ReceiverConfigFile) rcfg.ReceiveManager, err = receivers.New(&rcfg.Sync, rcfg.ConfigFile.ReceiverConfigFile)
if err != nil { if err != nil {
log.Print(err.Error()) cclog.Error(err.Error())
return 1 return 1
} }
ReceiveToRouterChannel := make(chan lp.CCMetric) ReceiveToRouterChannel := make(chan lp.CCMetric)
@ -278,7 +275,7 @@ func mainFunc() int {
// Wait until one tick has passed. This is a workaround // Wait until one tick has passed. This is a workaround
if rcfg.CliArgs["once"] == "true" { if rcfg.CliArgs["once"] == "true" {
var x int = (1.8 * float64(rcfg.ConfigFile.Interval)) var x float64 = (1.8 * float64(rcfg.ConfigFile.Interval))
time.Sleep(time.Duration(int(x)) * time.Second) time.Sleep(time.Duration(int(x)) * time.Second)
shutdown(&rcfg) shutdown(&rcfg)
} }