cc-metric-collector/internal/ccLogger/cclogger.go

116 lines
2.5 KiB
Go
Raw Normal View History

2022-01-25 16:40:02 +01:00
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 {
2022-01-25 17:43:10 +01:00
debugLog = log.New(stderr, "DEBUG ", log.LstdFlags)
2022-01-25 16:40:02 +01:00
}
if infoLog == nil {
2022-01-25 17:43:10 +01:00
infoLog = log.New(stdout, "INFO ", log.LstdFlags)
2022-01-25 16:40:02 +01:00
}
if errorLog == nil {
2022-01-25 17:43:10 +01:00
errorLog = log.New(stderr, "ERROR ", log.LstdFlags)
2022-01-25 16:40:02 +01:00
}
if warnLog == nil {
2022-01-25 17:43:10 +01:00
warnLog = log.New(stderr, "WARN ", log.LstdFlags)
2022-01-25 16:40:02 +01:00
}
if defaultLog == nil {
defaultLog = log.New(stdout, "", log.LstdFlags)
}
}
func Print(e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
defaultLog.Print(e)
2022-01-25 16:40:02 +01:00
}
func ComponentPrint(component string, e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
defaultLog.Print(fmt.Sprintf("[%s] ", component), e)
2022-01-25 16:40:02 +01:00
}
func Info(e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
infoLog.Print(e)
2022-01-25 16:40:02 +01:00
}
func ComponentInfo(component string, e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
infoLog.Print(fmt.Sprintf("[%s] ", component), e)
2022-01-25 16:40:02 +01:00
}
func Debug(e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
if globalDebug == true {
debugLog.Print(e)
2022-01-25 16:40:02 +01:00
}
}
func ComponentDebug(component string, e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
if globalDebug == true && debugLog != nil {
//CCComponentPrint(debugLog, component, e)
debugLog.Print(fmt.Sprintf("[%s] ", component), e)
2022-01-25 16:40:02 +01:00
}
}
func Error(e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
2022-01-25 16:40:02 +01:00
_, fn, line, _ := runtime.Caller(1)
2022-01-25 17:43:10 +01:00
errorLog.Print(fmt.Sprintf("[%s:%d] ", fn, line), e)
2022-01-25 16:40:02 +01:00
}
func ComponentError(component string, e ... interface{}) {
2022-01-25 17:43:10 +01:00
initLogger()
2022-01-25 16:40:02 +01:00
_, fn, line, _ := runtime.Caller(1)
2022-01-25 17:43:10 +01:00
errorLog.Print(fmt.Sprintf("[%s|%s:%d] ", component, fn, line), e)
2022-01-25 16:40:02 +01:00
}
func SetDebug() {
globalDebug = true
2022-01-25 17:43:10 +01:00
initLogger()
2022-01-25 16:40:02 +01:00
}
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()
}