Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Add two features in gplog (#43)
Browse files Browse the repository at this point in the history
* Add two features in gplog

- A new function FatalWithoutPanic, same as Fatal but will not
  panic and will exit(1)
- Enable user to customize the log file name

* Add comment and change FatalWithoutPanic errorCode to 2.

* Add test cases & FatalWithoutPanic can mock exit function
  • Loading branch information
Jialun authored May 24, 2019
1 parent 0a52505 commit c535ba3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
46 changes: 44 additions & 2 deletions gplog/gplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var (
* arise while running tests in parallel.
*/
logMutex sync.Mutex
/*
* A function which can customize log file name
*/
logFileNameFunc LogFileNameFunc
exitFunc ExitFunc
)

const (
Expand Down Expand Up @@ -75,8 +80,11 @@ const (
* - Fatal: Messages indicating that the program cannot proceed, e.g. the database
* cannot be reached. This function will exit the program after printing
* the error message.
* - FatalWithoutPanic: Same as Fatal, but will not trigger panic. Just exit(1).
*/
type LogPrefixFunc func(string) string
type LogFileNameFunc func(string, string) string
type ExitFunc func()

type GpLogger struct {
logStdout *log.Logger
Expand Down Expand Up @@ -108,11 +116,23 @@ func InitializeLogging(program string, logdir string) {
}

createLogDirectory(logdir)
timestamp := operating.System.Now().Format("20060102")
logfile := fmt.Sprintf("%s/%s_%s.log", logdir, program, timestamp)

logfile := GenerateLogFileName(program, logdir)
logFileHandle := openLogFile(logfile)

logger = NewLogger(os.Stdout, os.Stderr, logFileHandle, logfile, LOGINFO, program)
SetExitFunc(defaultExit)
}

func GenerateLogFileName(program, logdir string) string {
var logfile string
if logFileNameFunc != nil {
logfile = logFileNameFunc(program, logdir)
} else {
timestamp := operating.System.Now().Format("20060102")
logfile = fmt.Sprintf("%s/%s_%s.log", logdir, program, timestamp)
}
return logfile
}

func SetLogger(log *GpLogger) {
Expand Down Expand Up @@ -157,6 +177,14 @@ func SetLogPrefixFunc(logPrefixFunc func(string) string) {
logger.logPrefixFunc = logPrefixFunc
}

func SetLogFileNameFunc(fileNameFunc func(string, string) string) {
logFileNameFunc = fileNameFunc
}

func SetExitFunc(pExitFunc func()) {
exitFunc = pExitFunc
}

func defaultLogPrefixFunc(level string) string {
logTimestamp := operating.System.Now().Format("20060102:15:04:05")
return fmt.Sprintf("%s %s", logTimestamp, fmt.Sprintf(logger.header, level))
Expand Down Expand Up @@ -286,6 +314,16 @@ func FatalOnError(err error, output ...string) {
}
}

func FatalWithoutPanic(s string, v ...interface{}) {
logMutex.Lock()
defer logMutex.Unlock()
message := GetLogPrefix("CRITICAL") + fmt.Sprintf(s, v...)
errorCode = 2
_ = logger.logFile.Output(1, message)
_ = logger.logStderr.Output(1, message)
exitFunc()
}

type stackTracer interface {
StackTrace() errors.StackTrace
}
Expand Down Expand Up @@ -338,3 +376,7 @@ func createLogDirectory(dirname string) {
abort(errors.Errorf("%s is a file, not a directory", dirname))
}
}

func defaultExit() {
os.Exit(1)
}
10 changes: 10 additions & 0 deletions gplog/gplog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ var _ = Describe("logger/log tests", func() {
gplog.Fatal(errors.New(expectedMessage), "")
})
})
Context("FatalWithoutPanic", func() {
It("prints to the log file, then exit(1)", func() {
gplog.SetExitFunc(func() {})
expectedMessage := "logfile error fatalwithoutpanic"
gplog.FatalWithoutPanic(expectedMessage)
testhelper.NotExpectRegexp(stdout, fatalExpected+expectedMessage)
testhelper.ExpectRegexp(stderr, fatalExpected+expectedMessage)
testhelper.ExpectRegexp(logfile, fatalExpected+expectedMessage)
})
})
})
})
})

0 comments on commit c535ba3

Please sign in to comment.