Skip to content

Commit

Permalink
[Feature] Separate log debug into a specific file, not mandatory (#14)
Browse files Browse the repository at this point in the history
* Add debug log for separate log debug into a specific file, not mandatory
  • Loading branch information
renaldihusintkpd authored and Binayak Mishra committed Aug 30, 2019
1 parent 6c08e9f commit 4e40c50
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The logging package provides common functionality as log rotation, conditional debug logging etc.
// Package logging provides common functionality as log rotation, conditional debug logging etc.
// To initialize this package, just import it as
// import "gopkg.in/tokopedia/logging.v1
package logging
Expand All @@ -15,11 +15,15 @@ import (

var stdoutLog string
var stderrLog string

// separating debug log into a specific file to ease monitoring process
var debugLog string

var debugFlag bool
var versionFlag bool

// global logger for debug messages
// logging.Debug.Println("debug message")
// Debug global logger for debug messages
// logging.Debug.Println("debug message")
// debug messages are printed only when the program is started with -debug flag
var Debug *log.Logger

Expand All @@ -28,6 +32,7 @@ var Debug *log.Logger
func init() {
flag.StringVar(&stdoutLog, "l", "", "log file for stdout")
flag.StringVar(&stderrLog, "e", "", "log file for stderr")
flag.StringVar(&debugLog, "d", "", "log file for debug") // only if to use debugLog
flag.BoolVar(&versionFlag, "version", false, "binary version")
flag.BoolVar(&debugFlag, "debug", false, "enable debug logging")

Expand All @@ -49,7 +54,7 @@ func sigHandler(c chan os.Signal) {
}
}

// App must call LogInit once to setup log redirection
// LogInit App must call LogInit once to setup log redirection
func LogInit() {

if versionFlag == true {
Expand All @@ -67,32 +72,42 @@ func LogInit() {
SetDebug(debugFlag)
}

// SetDebug set debug
func SetDebug(enabled bool) {
if enabled {
debugFlag = true
Debug = log.New(os.Stdout, "debug:", log.Ldate|log.Ltime|log.Lshortfile)
f, err := reopen(0, debugLog)
if f == nil || err != nil {
f = os.Stdout
}

Debug = log.New(f, "debug:", log.Ldate|log.Ltime|log.Lshortfile)
Debug.Println("---- debug mode ----")
}
}

// Determine if we are running in debug mode or not
// IsDebug Determine if we are running in debug mode or not
func IsDebug() bool {
return debugFlag
}

func reopen(fd int, filename string) {
func reopen(fd int, filename string) (*os.File, error) {
if filename == "" {
return
return nil, fmt.Errorf("Empty log file for fd: %d", fd)
}

logFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)

if err != nil {
if fd != 0 && err != nil {
// do not terminate in case of debug file open error
log.Println("Error in opening ", filename, err)
os.Exit(2)
}

if err = syscall.Dup2(int(logFile.Fd()), fd); err != nil {
log.Println("Failed to dup", filename)
if fd != 0 {
if err = syscall.Dup2(int(logFile.Fd()), fd); err != nil {
log.Println("Failed to dup", filename)
}
}

return logFile, err
}

0 comments on commit 4e40c50

Please sign in to comment.