// 定義6個常量,每個常量代表一個布林值為true的位置
const (
LEVEL_DEBUG = 1 << iota
LEVEL_INFO
LEVEL_WARN
LEVEL_ERROR
LEVEL_FATAL
PRINT_SRC_FILE
)
type consoleFunc func(format string, a ...any)
var printSrcFileEmptyFunc = func() {}
type Console struct {
Debug consoleFunc
Info consoleFunc
Warn consoleFunc
Error consoleFunc
Fatal consoleFunc
}
func NewConsole(params int) *Console {
levelDebug := params&LEVEL_DEBUG != 0
levelInfo := params&LEVEL_INFO != 0
levelWarn := params&LEVEL_WARN != 0
levelError := params&LEVEL_ERROR != 0
levelFatal := params&LEVEL_FATAL != 0
srcFile := params&PRINT_SRC_FILE != 0
result := &Console{
Debug: func(format string, a ...any) {},
Info: func(format string, a ...any) {},
Warn: func(format string, a ...any) {},
Error: func(format string, a ...any) {},
Fatal: func(format string, a ...any) {},
}
if levelDebug {
result.Debug = result.debugFunc
}
if levelInfo {
result.Info = result.infoFunc
}
if levelWarn {
result.Warn = result.warnFunc
}
if levelError {
result.Error = result.errorFunc
}
if levelFatal {
result.Fatal = result.fatalFunc
}
if srcFile {
printSrcFileEmptyFunc = printSrcFileFunc
}
return result
}
// Log 列印日誌的函式
func output(level int, msg string) {
// 格式化日誌等級字串,新增顏色
levelStr := generateLevel(level)
now := generateNowTime()
printSrcFileEmptyFunc()
fmt.Printf("%s |%s| %s\n", now, levelStr, msg)
}
// debugFunc 列印DEBUG等級的日誌
func (c *Console) debugFunc(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
output(LEVEL_DEBUG, msg)
}
// infoFunc 列印INFO等級的日誌
func (c *Console) infoFunc(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
output(LEVEL_INFO, msg)
}
// warningFunc 列印WARNING等級的日誌
func (c *Console) warnFunc(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
output(LEVEL_WARN, msg)
}
// errorFunc 列印ERROR等級的日誌
func (c *Console) errorFunc(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
output(LEVEL_ERROR, msg)
}
// fatalFunc 列印FATAL等級的日誌
func (c *Console) fatalFunc(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
output(LEVEL_FATAL, msg)
}
func printSrcFileFunc() {
_, file, line, ok := runtime.Caller(3) // 注意這裡要改為3,因為呼叫堆疊深入了一層(此處根據實際情況修改數值)
if !ok {
log.Println("無法獲取呼叫者資訊")
return
}
fileStr := fmt.Sprintf("\033[1;95m%s:%d\033[0m", file, line)
// 列印格式化的日誌
fmt.Printf("%s\n", fileStr)
}
func generateNowTime() string {
return time.Now().Format("2006-01-02 - 15:04:05.000")
}
func generateLevel(level int) string {
// logColor 用於根據日誌等級設定背景色和文字色
logColor := map[int]string{
LEVEL_DEBUG: fmt.Sprintf("\033[44;97m%-5s\033[0m", "DEBUG"), //
LEVEL_INFO: fmt.Sprintf("\033[102;97m%-5s\033[0m", "INFO"), //
LEVEL_WARN: fmt.Sprintf("\033[103;97m%-5s\033[0m", "WARN"), //
LEVEL_ERROR: fmt.Sprintf("\033[101;97m%-5s\033[0m", "ERROR"), //
LEVEL_FATAL: fmt.Sprintf("\033[41;97m%-5s\033[0m", "FATAL"), //
}
return fmt.Sprintf(logColor[level])
}
呼叫方法:
func main() {
c := NewConsole(31)
c.Error("test error")
c.Info("test info")
c.Debug("test debug")
}
當傳入31時效果如下:
當傳入63時效果如下:
下面是列印日誌等級組合的數值: