Hello everyone, I have an issue to use the different logs from my custom Logger package in other files of the project. So here is my Logger package with InitLogs() method.
I have only one module in the application ‘ConsulGitOpsOperator/packages’ and all other custom packages are located here. I can use Methods from other packages in main function, but for some reason the Logger package doesn’t work for other files. Could you please help me to find a solution?
Any help is appreciated!!
package Logger
import (
"ConsulGitOpsOperator/packages/Config"
"flag"
"fmt"
"io"
"log"
"os"
)
var (
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
func InitLogs() {
logPath := fmt.Sprintf("%s%s", Config.WorkingDir(), "\\appLogs.txt")
flag.Parse()
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("Error creating log file: %v\n", err)
os.Exit(1)
}
defer logFile.Close()
writer := io.Writer(logFile)
flags := log.Ldate | log.Ltime | log.Lshortfile
Info = log.New(writer, "INFO: ", flags)
Warning = log.New(writer, "WARNING: ", flags)
Error = log.New(writer, "ERROR: ", flags)
writeLog := io.MultiWriter(os.Stdout, writer)
log.SetOutput(writeLog)
Warning.Println("LogFile : " + logPath) // the output is saved in the logFile
}
And this is the way how I import it and use in the main function
package main
import (
"ConsulGitOpsOperator/packages/GenerateACLTokens"
Logger "ConsulGitOpsOperator/packages/Logger"
)
func main() {
Logger.InitLogs()
Logger.Info.Println("This is an Info") // the output is NOT saved in the logFile
err := GenerateACLTokens.StartGeneratingToken()
if err != nil {
Logger.Error.Println("Error", err) // the output is NOT saved in the logFile
}
}