feature: QOOLOG_CLOSE_STDLOG & Println & Fatal

This commit is contained in:
bryan 2024-07-25 00:38:06 +08:00
parent edecd4a8d7
commit a73906b74f
7 changed files with 218 additions and 174 deletions

View File

@ -62,7 +62,7 @@ func Warningf(format string, v ...interface{}) {
}
func Errorf(format string, v ...interface{}) {
mylog.Warningf(format, v...)
mylog.Errorf(format, v...)
}
func Panicf(format string, v ...interface{}) {
@ -73,6 +73,14 @@ func Fatalf(format string, v ...interface{}) {
mylog.Fatalf(format, v...)
}
func Fatal(v ...interface{}){
mylog.Fatal(v...)
}
func Println(v ...interface{}){
mylog.Println(v...)
}
func TraceInto(format string, v ...interface{}) string {
_, fn, line, _ := runtime.Caller(1)
strfn := fmt.Sprintf("%s:%d", fn, line)

6
go.mod
View File

@ -2,4 +2,8 @@ module qoobing.com/gomod/log
go 1.16
require qoobing.com/gomod/str v1.0.1
require (
github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d
github.com/tylerb/is v2.1.4+incompatible // indirect
qoobing.com/gomod/str v1.0.1
)

35
log.go
View File

@ -101,6 +101,7 @@ func (log *Logger) SetLogLevelByName(newlv string) (oldlv string) {
return ErrLogLevel
}
func (log *Logger) Print(prefix string, v interface{}) {
var str = ""
if pkg, err := json.Marshal(v); err != nil {
@ -159,6 +160,15 @@ func (log *Logger) Panicf(format string, v ...interface{}) {
log.logwrite(PANIC, 3, format, v...)
}
func (log *Logger) Fatal(v ...interface{}){
log.logwrite(FATAL, 3, "%s", fmt.Sprintln(v...))
}
func (log *Logger) Println(v ...interface{}){
log.logwrite(DEBUG, 3, "%s", fmt.Sprintln(v...))
}
func (log *Logger) GetLogidStr(format string) string {
if logidCreator != nil {
return fmt.Sprintf(format, logidCreator.GetLogid())
@ -185,7 +195,7 @@ func (log *Logger) logwrite(typ LogLevel, calldepth int, format string, v ...int
case WARNING:
log.golog.SetPrefix("\x1b[32m" + "WARN ")
case ERROR:
log.golog.SetPrefix("\x1b[32m" + "ERRO ")
log.golog.SetPrefix("\x1b[33m" + "ERRO ")
case INFO:
log.golog.SetPrefix("INFO ")
case NOTICE:
@ -226,6 +236,13 @@ func needWriteLogToFile() bool {
return true
}
func needWriteLogToStdout() bool {
if cls, ok := os.LookupEnv("QOOLOG_CLOSE_STDLOG"); ok && (cls == "Y" || cls == "y") {
return false
}
return true
}
// //////////////////////////////////////////////////////////////////////////////////
// ///////////////////////Initialize/////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////
@ -248,14 +265,14 @@ func initlog() {
gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
)
// Step 2. init golog(golang std logger) & mylog(myself defined logger)
golog = log.New(NewCacheWriter(baseWriter), "", gologFlags)
mylog = New("system")
// Step 3. backgroud split log (log rotate) if need
// Step 2.1 add stdout to base writer if need
if needWriteLogToStdout() {
baseWriter.stdw = os.Stdout
}
// Step 2.2 rotate log(split log ) if need
if needWriteLogToFile() {
go func() {
baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
go func() {
for {
tag := time.Now().Local().Format(logSplitTag)
baseWriter.TryBackupLogFile(tag)
@ -263,4 +280,8 @@ func initlog() {
}
}()
}
// Step 3. init golog(golang std logger) & mylog(myself defined logger)
golog = log.New(NewCacheWriter(baseWriter), "", gologFlags)
mylog = New("system")
}

9
log_test.go Normal file
View File

@ -0,0 +1,9 @@
package log
import "testing"
func TestLog(t *testing.T) {
Println("aaa", 1,3)
Fatal("aaa", 1,3)
Fatalf("aaa=%d", 1)
}

View File

@ -1,4 +1,5 @@
// +build glslogid
package log
import (

View File

@ -1,4 +1,5 @@
// +build runtimelogid
package log
//// Format writes the source file path and line number of the record to the buf.

View File

@ -130,5 +130,5 @@ func (w *cacheWriter) StartCacheLog() {
func (w *cacheWriter) StopCacheLog() {
w.cacheLock.Unlock()
w.cacheing = true
w.cacheing = false
}