In my program there is a lot places where I should create logs like following
log.Println("[Build]: bla bla“)
log.Println(“[INFO]: bla bla“)
log.Printf(“[ERROR]: bla bla“)
log.Println(“[WARNING]: bla bla“)
I use it for all my logging in different packages inside the application and it's bit frustrating to copy this, is there a way to create some generic log which I can simple re-use it inside my program.
I read about logrus etc but they provide some way to do it but not provide some custom way or maybe I miss something.
My program is command line tool
It's best to use a more proper logger like zap or logrus . Also prefer passing the logger explicitly as an argument and avoid global loggers.
Yet if the exact logging workflow with the built-in one is needed, with the provided sample output, it's possible to define them with proper prefixes, instead of repeating the level in each string:
var (
warnlog = log.New(os.Stderr, "[ warn ]", log.Ltime|log.Lshortfile)
infolog = log.New(os.Stderr, "[ info ]", log.Ltime|log.Lshortfile)
buildlog = log.New(os.Stderr, "[ build ]", log.Ltime|log.Lshortfile)
errlog = log.New(os.Stderr, "[ error ]", log.Ltime|log.Lshortfile)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.