Skip to content

Instantly share code, notes, and snippets.

@kanguki
Created April 5, 2022 02:27
Show Gist options
  • Save kanguki/b4bbb6475c3fcf0c8557ed062c87ba12 to your computer and use it in GitHub Desktop.
Save kanguki/b4bbb6475c3fcf0c8557ed062c87ba12 to your computer and use it in GitHub Desktop.
Logging enable write log to file, log rotation and debug flag
package log
import (
"flag"
"fmt"
"log"
"os"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
debug *bool
LOG_PATH = "LOG_PATH"
)
// call flag.Parse() in main
func init() {
//enable log file
if path := os.Getenv(LOG_PATH); path != "" {
l := &lumberjack.Logger{
Filename: path,
MaxSize: 1, // megabytes
MaxAge: 7, //days
Compress: true, // disabled by default
}
l.Rotate()
log.SetOutput(l)
}
//enable debug
debug = flag.Bool("d", false, "Run in debug mode")
}
func Debug(format string, args ...interface{}) {
if *debug {
Log(format, args...)
}
}
func Log(format string, args ...interface{}) {
log.Printf("%s\n", fmt.Sprintf(format, args...))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment