Golang : Not able to grep log.Println() output
Problem:
You have a Golang program that uses log.Println()
to capture text output generated by your program and you want to pipe the output to grep
command. Mysteriously, it is not working as expected. What's going on?
Solution and notes:
The underlying code behind log.Println()
uses fmt.Sprintln()
(see https://golang.org/src/log/log.go?s=8792:8822#L284) while fmt.Println()
uses Fprintln()
function. (see https://golang.org/src/fmt/print.go?s=7388:7437#L246)
The difference is that Fprintln()
writes to io.Writer, while Sprintln()
does not.
io.Writer will pipe text output to grep
command via the Unix/Linux's |
.
It is ok to use log.Println()
if you expect the log messages to be short or one line. Do not use log.Println()
to capture text output if it is going to be long - such as HTML.
Run the code example below and try to grep
the lines with page
generated by log.Println()
versus fmt.Println()
. You will see the differences immediately.
package main
import (
"fmt"
"io/ioutil"
// "log"
"net/http"
"os"
)
func main() {
// http.Get() can handle gzipped data response
// automagically
resp, err := http.Get("https://golang.org")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//log.Println(string(htmlData)) //<-- grep will not work
fmt.Println(string(htmlData)) //<-- grep will work
}
Output with fmt.Println()
:
./grepalog | grep "page"
<div id="page">
the content of this page is licensed under the
</div><!-- #page -->
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.9k Golang : Reverse IP address for reverse DNS lookup example
+11k Golang : Fix - does not implement sort.Interface (missing Len method)
+7.4k Golang : Handling Yes No Quit query input
+25.5k Golang : missing Mercurial command
+38.9k Golang : How to iterate over a []string(array)
+12.1k Golang : Simple client-server HMAC authentication without SSL example
+4.2k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+6.4k Golang : How to determine if request or crawl is from Google robots
+14.1k Golang : Get uploaded file name or access uploaded files
+7.7k Golang : Scan files for certain pattern and rename part of the files
+18.3k Golang : Example for RSA package functions