logger Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/logger/ Sat, 30 Nov 2019 18:05:36 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 logger Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/logger/ 32 32 159787465 Go Logger Rotation https://vikasboss.github.io/go-logger-rotation/ https://vikasboss.github.io/go-logger-rotation/#respond Sat, 30 Nov 2019 17:59:29 +0000 https://vikasboss.github.io/?p=761 This article is about how we rotate log files from within the go app without having to worry about the disk full alerts. We are going to use https://github.com/lestrrat/go-file-rotatelogs for log rotation....

The post Go Logger Rotation appeared first on Welcome To Golang By Example.

]]>
This article is about how we rotate log files from within the go app without having to worry about the disk full alerts.

We are going to use https://github.com/lestrrat/go-file-rotatelogs for log rotation.

Example: In the example below we are setting

  • MaxAge to 10 seconds. It sets the max-age of a log file before it is purged from the file system. Meaning the file will be deleted after 10 seconds. See
rotatelogs.WithMaxAge(time.Second*10)
  • RotationTime is 1 second. It sets the time when the file will be rotated. So the file will be rotated every second. See
rotatelogs.WithRotationTime(time.Second*1)
  • Location is /var/log/service/ . See
path := "/var/log/service/"
  • Pattern of the file is “old.UTC..2019-11-30.22:40:10” . See
 fmt.Sprintf("%s.%s", path, "%Y-%m-%d.%H:%M:%S")
  • Link Path is/var/log/service/current. This file would be a symlink for the actual file. See
rotatelogs.WithLinkName("/var/log/service/current")

Code:

package main

import (
    "fmt"
    "log"
    "time"
    rotatelogs "github.com/lestrrat/go-file-rotatelogs"
    "github.com/sirupsen/logrus"
)

func initiLogger() {
    path := "/var/log/service/old.UTC."
    writer, err := rotatelogs.New(
        fmt.Sprintf("%s.%s", path, "%Y-%m-%d.%H:%M:%S"),
        rotatelogs.WithLinkName("/var/log/service/current"),
        rotatelogs.WithMaxAge(time.Second*10),
        rotatelogs.WithRotationTime(time.Second*1),
    )
    if err != nil {
        log.Fatalf("Failed to Initialize Log File %s", err)
    }
    log.SetOutput(writer)
    return
}

func main() {
    initiLogger()
    for i := 0; i < 100; i++ {
        time.Sleep(time.Second * 1)
        log.Printf("Hello, World!")
    }
    fmt.Scanln()
}

Go to the location /var/log/service/. You will notice

  • Files are rotated every second.
  • Max age of the file is 10 seconds. So every file will be deleted after 10 seconds.

The post Go Logger Rotation appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/go-logger-rotation/feed/ 0 761