Sobes.tech
Senior

Where are logs stored?

sobes.tech AI

Answer from AI

In Go projects, logs are usually stored in several places depending on requirements and infrastructure:

  • In files on the server, using log rotation through libraries like lumberjack or logrotate to manage size and retention.
  • In centralized logging systems, such as sending logs to ELK Stack (Elasticsearch, Logstash, Kibana) or Grafana Loki for easy search and visualization.
  • In cloud logging services, such as AWS CloudWatch or Google Cloud Logging, if the project is deployed in the cloud.

Example of using lumberjack for log rotation:

import (
    "log"
    "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    log.SetOutput(&lumberjack.Logger{
        Filename:   "/var/log/myapp/app.log",
        MaxSize:    10, // MB
        MaxBackups: 3,
        MaxAge:     28, // days
        Compress:   true,
    })

    log.Println("Log entry example")
}