simplelog
simplelog provides the logger
package, a simple logger with level and time information.
Usage
package main
import "git.brokenmouse.studio/bms/go-simplelog"
func main() {
logger.Info("0x%02X", 10)
logger.Debug("Test")
logger.Warn("This is weird...")
logger.Error("Something went wrong!")
logger.Fatal("Can't continue, everything is broken!")
}
Result:
[12:00:00/INFO] 0x0A
[12:00:00/DEBUG] Test
[12:00:00/WARN] This is weird...
[12:00:00/ERROR] Something went wrong!
[12:00:00/FATAL] Can't continue, everything is broken!
exit status 1
Debug Messages
Debug messages are only logged when LogDebug
is true
(the default).
package main
import (
"flag"
"git.brokenmouse.studio/bms/go-simplelog"
)
func main() {
verboseFlag := flag.Bool("v", false, "Increase verbosity.")
flag.Parse()
logger.LogDebug = *verboseFlag
logger.Debug("Building index ...")
}
Result:
The message is only logged when the -v
flag is specified.
[12:00:00/DEBUG] Building index ...
Fatal Errors
When logging a fatal error, the program will exit with status code 1.
logger.Fatal("Failed to initialize!")
Result:
[12:00:00/FATAL] Failed to initialize!
exit status 1
Writing to files
Messages can additionally be logged to a file or another io.StringWriter
by setting LogWriter
.
package main
import (
"os"
"git.brokenmouse.studio/bms/go-simplelog"
)
func main() {
logger.LogWriter, _ = os.OpenFile("simplelog.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0660)
logger.Info("Read me later...")
}
Result:
The message will appear in standard output and a new file named simplelog.log
.
[12:00:00/INFO] Read me later...