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

1
go.mod
View File

@ -9,5 +9,6 @@ require (
github.com/nats-io/nats.go v1.10.0
github.com/nats-io/nkeys v0.1.4 // 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
)

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 (
"encoding/json"
"flag"
"fmt"
"log"
// "log"
"os"
"os/signal"
"strings"
@ -17,7 +16,7 @@ import (
"sync"
"time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
mr "github.com/ClusterCockpit/cc-metric-collector/internal/metricRouter"
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)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
cclog.Error(err.Error())
return err
}
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
func shutdown(config *RuntimeConfig) {
log.Print("Shutdown...")
cclog.Info("Shutdown...")
if config.CollectManager != nil {
log.Print("Shutdown CollectManager...")
cclog.Debug("Shutdown CollectManager...")
config.CollectManager.Close()
}
if config.ReceiveManager != nil {
log.Print("Shutdown ReceiveManager...")
cclog.Debug("Shutdown ReceiveManager...")
config.ReceiveManager.Close()
}
if config.Router != nil {
log.Print("Shutdown Router...")
cclog.Debug("Shutdown Router...")
config.Router.Close()
}
if config.SinkManager != nil {
log.Print("Shutdown SinkManager...")
cclog.Debug("Shutdown SinkManager...")
config.SinkManager.Close()
}
@ -184,7 +183,6 @@ func prepare_shutdown(config *RuntimeConfig) {
go func(config *RuntimeConfig) {
<-sigs
log.Print("Shutdown...")
shutdown(config)
}(config)
}
@ -199,24 +197,23 @@ func mainFunc() int {
// Load and check configuration
err = LoadCentralConfiguration(rcfg.CliArgs["configfile"], &rcfg.ConfigFile)
if err != nil {
log.Print("Error reading configuration file ", rcfg.CliArgs["configfile"])
log.Print(err.Error())
cclog.Error("Error reading configuration file ", rcfg.CliArgs["configfile"], ": ", err.Error())
return 1
}
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
}
rcfg.Interval = time.Duration(rcfg.ConfigFile.Interval) * time.Second
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
}
rcfg.Duration = time.Duration(rcfg.ConfigFile.Duration) * time.Second
rcfg.Hostname, err = os.Hostname()
if err != nil {
log.Print(err.Error())
cclog.Error(err.Error())
return 1
}
// Drop domain part of host name
@ -231,14 +228,14 @@ func mainFunc() int {
if len(rcfg.ConfigFile.RouterConfigFile) > 0 {
rcfg.Router, err = mr.New(rcfg.Ticker, &rcfg.Sync, rcfg.ConfigFile.RouterConfigFile)
if err != nil {
log.Print(err.Error())
cclog.Error(err.Error())
return 1
}
}
if len(rcfg.ConfigFile.SinkConfigFile) > 0 {
rcfg.SinkManager, err = sinks.New(&rcfg.Sync, rcfg.ConfigFile.SinkConfigFile)
if err != nil {
log.Print(err.Error())
cclog.Error(err.Error())
return 1
}
RouterToSinksChannel := make(chan lp.CCMetric)
@ -248,7 +245,7 @@ func mainFunc() int {
if len(rcfg.ConfigFile.CollectorConfigFile) > 0 {
rcfg.CollectManager, err = collectors.New(rcfg.Ticker, rcfg.Duration, &rcfg.Sync, rcfg.ConfigFile.CollectorConfigFile)
if err != nil {
log.Print(err.Error())
cclog.Error(err.Error())
return 1
}
CollectToRouterChannel := make(chan lp.CCMetric)
@ -258,7 +255,7 @@ func mainFunc() int {
if len(rcfg.ConfigFile.ReceiverConfigFile) > 0 {
rcfg.ReceiveManager, err = receivers.New(&rcfg.Sync, rcfg.ConfigFile.ReceiverConfigFile)
if err != nil {
log.Print(err.Error())
cclog.Error(err.Error())
return 1
}
ReceiveToRouterChannel := make(chan lp.CCMetric)
@ -278,7 +275,7 @@ func mainFunc() int {
// Wait until one tick has passed. This is a workaround
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)
shutdown(&rcfg)
}