Use log.SetOutput and the io.MultiWriter function to replace the output writer of the log package to write to stdout and a file at the same time.
log.SetOutput(io.MultiWriter(os.Stdout, f)) There is a limited capability to influence the timestamp with the SetFlags function. You could also replace the Writer with a Writer that modifies the time stamp at the beginning of every line. But if you do that, it is probably better to copy the log package and create the timestamp you want. You would need to change the formatHeader method of the Logger type. You could also check the log packages that are out there, whether they fit you. You may want to use logrus, which provides some compatibility to the stdlib log package and it allows time formst strings in its TextFormatter. On Monday, January 17, 2022 at 6:56:44 PM UTC+1 Rich wrote: > What I want to do is extend the go standard log package. I am using log to > write logfiles, but there are two things with it I need to extend. > > The first is a 'Format' string, which allows me to change the time format > string log uses. > > The second is to be able to print to both logfile and stdout., This is > useful when you're starting your server, and you want some of the output to > go directly to stdout. I use this so that with one line I can print the > same line to stdout as I print to my log file. > > lgOut.Tprintf("Server Version: %s Started and listens on: %s:%s, ", > server.Version, server.Address, server.Port) > > That would print to both stdout and to the logfile lgOut is configured for. > Ex: > 20220117-13:45 | Server Version 1.0.1 Started and listens on > 127.0.0.1:8080" > > What I am worried about is that my version of log will diverge from the Go > standard log package. Is there a way to extend an existing package so > that my adds to the log package pulls from whatever happens to be the most > current version of log? > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a5e3e140-bc5f-41dc-8971-7adf28d5c0cfn%40googlegroups.com.