<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Burkay Altuntaş on Medium]]></title>
        <description><![CDATA[Stories by Burkay Altuntaş on Medium]]></description>
        <link>https://medium.com/@burkayaltuntas?source=rss-56d7574dcfe4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IEwcMfSiMPiKu3opv4VgRA@2x.jpeg</url>
            <title>Stories by Burkay Altuntaş on Medium</title>
            <link>https://medium.com/@burkayaltuntas?source=rss-56d7574dcfe4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:07:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@burkayaltuntas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Daily log rotation for Golang Gin]]></title>
            <link>https://medium.com/@burkayaltuntas/daily-log-rotation-for-golang-gin-6eb6ec278479?source=rss-56d7574dcfe4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6eb6ec278479</guid>
            <category><![CDATA[logrotate]]></category>
            <category><![CDATA[gin]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[middleware]]></category>
            <dc:creator><![CDATA[Burkay Altuntaş]]></dc:creator>
            <pubDate>Thu, 09 Mar 2023 20:22:30 GMT</pubDate>
            <atom:updated>2023-03-09T20:22:30.700Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Log rotation</strong> is a way to keep log file sizes under control.</p><p>I was surprised when I realized gin web framework doesn’t have log rotation out of the box. Even its <a href="https://github.com/gin-gonic/gin/issues/350">github repository</a> has issues about it. I implemented a straight forward solution for it and staging it here for the ones who will end up here after a short googling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*8uPzHWP-LLXN5DRh3acjcQ.png" /><figcaption>golang+gin+logging</figcaption></figure><p>The most common approach is to use logger function at the middleware like:</p><pre>import &quot;where/ever/your/logger/middlewares&quot;<br>func main() {<br>  .<br>  app := gin.New()<br>  app.Use(middlewares.Logger())<br>  .<br>  .<br>  app.Run()<br>}</pre><p>Below is your middleware handler function for gin. This is for http request pipeline but injecting it anywhere is up to you for custom written logs.</p><pre>package middlewares<br><br>import (<br> &quot;bytes&quot;<br> &quot;encoding/json&quot;<br> &quot;io&quot;<br> &quot;log&quot;<br> &quot;os&quot;<br> &quot;time&quot;<br><br> &quot;.../utils&quot;<br> &quot;github.com/gin-gonic/gin&quot;<br>)<br><br>var LOGFILEBASE = &quot;./logs/info/&quot;<br>var _log *log.Logger<br>var _f *os.File<br>var _today time.Time = time.Now()<br><br>func init() {<br>  var err error<br>  var infoLogFile = LOGFILEBASE + time.Now().Format(&quot;2006-01-02&quot;) + &quot;.log&quot;<br>  <br>  _f, err = os.OpenFile(infoLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)<br>  if err != nil {<br>    log.Panicf(&quot;error opening file: %v&quot;, err)<br>  }<br>  wr := io.MultiWriter(_f, os.Stdout)<br>  _log = log.New(wr, &quot;INFO &quot;, log.LstdFlags|log.Lmicroseconds)<br>  _log.SetFlags(log.LstdFlags | log.Lmicroseconds)<br>}<br><br>func Logger() gin.HandlerFunc {<br> return func(c *gin.Context) {<br>  // this if block is where important things happen for rotation<br>  // changing output file for logger<br>  if !dateEqual(_today, time.Now()) {<br>   _today = time.Now()<br><br>   dailyLogFile := LOGFILEBASE + time.Now().Format(&quot;2006-01-02&quot;) + &quot;.log&quot;<br>   newF, err := os.OpenFile(dailyLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)<br>   if err != nil {<br>    log.Panicf(&quot;error opening file: %v&quot;, err)<br>   }<br>   wr := io.MultiWriter(newF, os.Stdout)<br>   _log.SetOutput(wr)<br>  }<br><br>  var bs string<br>  if c.Request.Method == &quot;POST&quot; || c.Request.Method == &quot;PUT&quot; {<br>   body, _ := io.ReadAll(c.Request.Body)<br>   bs = string(body)<br>   c.Request.Body = io.NopCloser(bytes.NewReader(body))<br>  }<br><br>  // better if you have a user in the context<br><br>  go _log.Println(c.ClientIP(), c.Request.Method, c.Request.RequestURI, &quot;REQUEST&quot;, bs)<br><br>  blw := &amp;bodyLogWriter{body: bytes.NewBufferString(&quot;&quot;), ResponseWriter: c.Writer}<br>  c.Writer = blw<br><br>  c.Next()<br>  // after this &#39;c.Next()&#39; part is for request response logs<br>  // i standartize responses with a struct called ResponseModel<br>  // and unmarshalled to get the response message<br>  <br>  mdl := utils.ResponseModel{}<br><br>  if blw.Status() &gt; 201 {<br>   resp, err := io.ReadAll(blw.body)<br>   if err == nil {<br>    json.Unmarshal(resp, &amp;mdl)<br>   }<br>  }<br><br>  go _log.Println(c.Request.RemoteAddr, c.Request.Method, c.Request.RequestURI, &quot;RESPONSE&quot;, blw.Status(), mdl.Message)<br> }<br>}<br><br>func dateEqual(date1, date2 time.Time) bool {<br> y1, m1, d1 := date1.Date()<br> y2, m2, d2 := date2.Date()<br> return y1 == y2 &amp;&amp; m1 == m2 &amp;&amp; d1 == d2<br>}<br><br>type bodyLogWriter struct {<br> gin.ResponseWriter<br> body *bytes.Buffer<br>}<br><br>func (w bodyLogWriter) Write(b []byte) (int, error) {<br> w.body.Write(b)<br> return w.ResponseWriter.Write(b)<br>}</pre><p>For errors/panics you can use this approach. It has a default writer <em>os.Stdout </em>but you can add file writer as well.</p><p>Don’t forget to discard sensitive data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6eb6ec278479" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>