In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the custom log format and output mode of Golang GinWeb / the method of activating the banned log color". In the daily operation, it is believed that many people have doubts about the custom log format and the output mode / the method of activating the banned log color of Golang GinWeb. The editor consulted all kinds of materials and sorted out a simple and useful operation method. I hope it will be helpful for you to answer the question of "what is the custom log format and output mode of Golang GinWeb / what is the color of the banned log?" Next, please follow the editor to study!
Log to a file
Using the io.MultiWriter multi-writer, you can log to the file and output to the console at the same time.
Package main import ("github.com/gin-gonic/gin"io"os") func main () {/ / Disable Console Color, you don't need console color when writing the logs to file. / / disable console log color. When log is written to a file, there is no need to open console log color gin.DisableConsoleColor () / / Logging to a file. Create a new log file to get the file structure, which implements the writer Writer interface f, _: = os.Create ("gin.log") / / io.MultiWriter (multiple writer method) to create a writer, appends the incoming multiple writers to a writer array, and the resulting writer implements the Writer interface, which writes the data that needs to be written out to each writer, just like the Unix command tee The data will be written to the file and printed to the standard output / / configure the Gin default log writer for the resulting multi-writer gin.DefaultWriter = io.MultiWriter (f) / / Use the following code if you need to write the logs to file and console at the same time. / / use the following code to write the log to the file and output it to the console / / gin.DefaultWriter = io.MultiWriter (f, os.Stdout) router: = gin.Default () router.GET ("/ ping", func (c * gin.Context) {c.String (200, "pong")}) router.Run (": 8080")}
Custom log format
Use Gin's LoggerWithFormatter method to instantiate a logger Logger middleware with a specified log format
Package main import ("fmt"github.com/gin-gonic/gin"time") func main () {router: = gin.New () / / LoggerWithFormatter middleware will write the logs to gin.DefaultWriter / / By default gin.DefaultWriter = os.Stdout / / type LogFormatter func (params LogFormatterParams) string where LogFormatterParams is a structure router.Use that formats log parameters (gin.LoggerWithFormatter (func (param gin.LogFormatterParams) string { / / your custom format / / 127.0.0.1-[Sun 22 Nov 2020 17:09:53 CST] "GET / ping HTTP/1.1 200 56.113 μ s" curl/7.64.1 "return fmt.Sprintf ("% s-[% s]\ "% s% s% d% s\"% s\ "% s\"\ n ", param.ClientIP, / / request the IP address of the client param.TimeStamp.Format (time.RFC1123) / / request time param.Method, / / request method param.Path, / / routing path param.Request.Proto, / / request protocol param.StatusCode, / / http response code param.Latency / / delay from request to response param.Request.UserAgent (), / / client agent param.ErrorMessage, / / print error message if there is an error}) router.Use (gin.Recovery ()) router.GET ("/ ping", func (c * gin.Context) {c.String (200) "pong")}) router.Run (": 8080")} / / simulated request test: curl http://localhost:8080/ping
Turn on / off log colors
Gin.DisableConsoleColor () disables log color
Gin.ForceConsoleColor () forcibly turns on log color, using virtual terminal TTY color scheme
Package main import ("github.com/gin-gonic/gin") func main () {/ / the log color output to the console by default is based on the virtual terminal TTY you use to color / / Disable log's color disable log color gin.DisableConsoleColor () / / Force log's color force enable log color / / gin.ForceConsoleColor () / / Creates a gin router with default middleware: / / logger and recovery (crash-free) middleware router: = gin.Default () router.GET ("/ ping" Func (c * gin.Context) {c.String (200, "pong")}) router.Run (": 8080")} / / Simulation request test: curl http://localhost:8080/ping so far The study on "what is the custom log format and output mode of Golang GinWeb / the method of disabling log color" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.