Created
April 5, 2022 02:27
-
-
Save kanguki/b4bbb6475c3fcf0c8557ed062c87ba12 to your computer and use it in GitHub Desktop.
Logging enable write log to file, log rotation and debug flag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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