v1.2.0
This commit is contained in:
parent
e6efed682b
commit
2455ca35b2
44
log.go
44
log.go
@ -29,6 +29,7 @@ const (
|
|||||||
var (
|
var (
|
||||||
golog *log.Logger = nil
|
golog *log.Logger = nil
|
||||||
mylog *Logger = nil
|
mylog *Logger = nil
|
||||||
|
logWriter *cacheWriter = nil
|
||||||
logidCreator LogidCreator = nil
|
logidCreator LogidCreator = nil
|
||||||
modulelogs map[string]*Logger = map[string]*Logger{}
|
modulelogs map[string]*Logger = map[string]*Logger{}
|
||||||
)
|
)
|
||||||
@ -62,7 +63,7 @@ func New(module string) *Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////
|
// ////////////////////////////////////////////////////////////////
|
||||||
func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) {
|
func (log *Logger) SetLogLevel(newlv logLevel) (oldlv logLevel) {
|
||||||
oldlv = log.loglevel
|
oldlv = log.loglevel
|
||||||
log.loglevel = newlv
|
log.loglevel = newlv
|
||||||
@ -129,25 +130,29 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var idstr = log.GetLogidStr("[%s] ")
|
var (
|
||||||
|
idstr = log.GetLogidStr("[%s] ")
|
||||||
|
prestr = idstr + "[" + log.module + "] "
|
||||||
|
)
|
||||||
|
|
||||||
format = strings.Trim(format, "\n")
|
format = strings.Trim(format, "\n")
|
||||||
switch typ {
|
switch typ {
|
||||||
case PANIC:
|
case PANIC:
|
||||||
log.golog.SetPrefix("\x1b[31m" + "PANIC " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("\x1b[31m" + "PANI ")
|
||||||
case FATAL:
|
case FATAL:
|
||||||
log.golog.SetPrefix("\x1b[31m" + "FATAL " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("\x1b[31m" + "FATA ")
|
||||||
case WARNING:
|
case WARNING:
|
||||||
log.golog.SetPrefix("\x1b[32m" + "WARNING " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("\x1b[32m" + "WARN ")
|
||||||
case ERROR:
|
case ERROR:
|
||||||
log.golog.SetPrefix("\x1b[32m" + "ERROR " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("\x1b[32m" + "ERRO ")
|
||||||
case INFO:
|
case INFO:
|
||||||
log.golog.SetPrefix("INFO " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("INFO ")
|
||||||
case NOTICE:
|
case NOTICE:
|
||||||
log.golog.SetPrefix("NOTICE " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("NOTI ")
|
||||||
case DEBUG:
|
case DEBUG:
|
||||||
log.golog.SetPrefix("DEBUG " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("DBUG ")
|
||||||
default:
|
default:
|
||||||
log.golog.SetPrefix("UNKNOWN " + idstr + "[" + log.module + "] ")
|
log.golog.SetPrefix("UNKN ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if mylog == log {
|
if mylog == log {
|
||||||
@ -155,25 +160,25 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
|
|||||||
}
|
}
|
||||||
|
|
||||||
if typ == FATAL || typ == WARNING || typ == ERROR {
|
if typ == FATAL || typ == WARNING || typ == ERROR {
|
||||||
log.golog.Output(calldepth, fmt.Sprintf(format+"\x1b[0m\n", v...))
|
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\x1b[0m\n", v...))
|
||||||
} else if typ == NOTICE {
|
} else if typ == NOTICE {
|
||||||
calldepth = calldepth + 2
|
calldepth = calldepth + 2
|
||||||
log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
|
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...))
|
||||||
} else if typ == PANIC {
|
} else if typ == PANIC {
|
||||||
stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1)
|
stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1)
|
||||||
stack = str.SkipLine(stack, calldepth*2+1)
|
stack = str.SkipLine(stack, calldepth*2+1)
|
||||||
v = append(v, stack)
|
v = append(v, stack)
|
||||||
panicstr := fmt.Sprintf(format+"\x1b[0m. Panic stack:\n%s\n", v...)
|
panicstr := fmt.Sprintf(prestr+format+"\x1b[0m. Panic stack:\n%s\n", v...)
|
||||||
log.golog.Output(calldepth, panicstr)
|
log.golog.Output(calldepth, panicstr)
|
||||||
panic(ErrLogPanic)
|
panic(ErrLogPanic)
|
||||||
} else {
|
} else {
|
||||||
log.golog.Output(calldepth, fmt.Sprintf(format+"\n", v...))
|
log.golog.Output(calldepth, prestr+fmt.Sprintf(format+"\n", v...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////Initialize/////////////////////////////////////////////////
|
// ///////////////////////Initialize/////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////////////////////
|
||||||
var once sync.Once = sync.Once{}
|
var once sync.Once = sync.Once{}
|
||||||
|
|
||||||
// init
|
// init
|
||||||
@ -190,13 +195,12 @@ func initlog() {
|
|||||||
logDirsOptional = []string{"./log", "/var/log"}
|
logDirsOptional = []string{"./log", "/var/log"}
|
||||||
logSplitTag = "20060102" //20060102150405
|
logSplitTag = "20060102" //20060102150405
|
||||||
baseWriter = NewBaseWriter(nil, os.Stdout, nil)
|
baseWriter = NewBaseWriter(nil, os.Stdout, nil)
|
||||||
cacheWriter = NewCacheWriter(baseWriter)
|
|
||||||
gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
|
gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
|
||||||
goSystemLog = log.New(cacheWriter, "", gologFlags)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Step 2. init golog(golang std logger) & mylog(myself defined logger)
|
// Step 2. init golog(golang std logger) & mylog(myself defined logger)
|
||||||
golog = goSystemLog
|
logWriter = NewCacheWriter(baseWriter)
|
||||||
|
golog = log.New(logWriter, "", gologFlags)
|
||||||
mylog = New("system")
|
mylog = New("system")
|
||||||
baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
|
baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user