Junior — Middle
How can a logging mechanism be integrated into your application?
sobes.tech AI
Answer from AI
In Go, to implement logging, you can use the standard log package or third-party libraries such as logrus or zap. For basic logging, it is sufficient to import the log package and use functions like log.Println(), log.Fatalf().
Example using the standard package:
import (
"log"
)
func main() {
log.Println("Application started")
// ...
}
For more advanced logging (log levels, formatting, output to file), third-party libraries are used. For example, with logrus, you can configure log levels and output format:
import (
log "github.com/sirupsen/logrus"
)
func main() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.SetLevel(log.InfoLevel)
log.Info("Informational message")
log.Warn("Warning")
}
Thus, the logging mechanism is connected by importing the necessary library and configuring logging parameters at the start of the application.