In Go, the standard log package does not provide built-in support for log levels such as DEBUG, INFO, WARN, ERROR, etc. However, you can achieve a similar effect by creating a custom logging utility that wraps the standard log functions and uses a global log level to control what gets logged. Below is an example of how you can implement log levels in Go.
package main
import (
"fmt"
"log"
"os"
"strings"
)
// Define the log levels
const (
DEBUG = iota
INFO
WARN
ERROR
)
var currentLogLevel = INFO // Set the default log level
func SetLogLevel(level int) {
currentLogLevel = level
}
func Log(level int, message string) {
if level >= currentLogLevel {
log.Println(message)
}
}
func main() {
// Set log level to DEBUG
SetLogLevel(DEBUG)
// Log messages of different levels
Log(DEBUG, "This is a DEBUG message.")
Log(INFO, "This is an INFO message.")
Log(WARN, "This is a WARNING message.")
Log(ERROR, "This is an ERROR message.")
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?