Junior — Middle
How can a logging system be integrated into software?
sobes.tech AI
Answer from AI
To integrate a logging system into Go software, you can use the standard log package or third-party libraries such as logrus or zap.
Main steps:
- Choose a logging library.
- Configure the output format (e.g., JSON, text).
- Define log levels (info, warning, error).
- Call logging functions at the appropriate places in the code.
Example using the standard log package:
package main
import (
"log"
"os"
)
func main() {
// Logging to a file
file, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
log.Println("Application started")
log.Println("An error occurred while processing the request")
}
This approach allows logs to be saved to a file for later analysis.