energy/logger/cef-logger.go

121 lines
2.3 KiB
Go
Raw Normal View History

2022-10-04 16:38:43 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under GNU General Public License v3.0
//
//----------------------------------------
2022-10-05 21:06:29 +08:00
//简单的日志输出
2022-10-04 22:34:57 +08:00
package logger
2022-10-04 13:21:05 +08:00
import (
"io"
"log"
"os"
)
type CefLoggerLevel int8
const (
CefLog_Error CefLoggerLevel = iota
CefLog_Info
CefLog_Debug
)
2022-10-05 21:06:29 +08:00
const log_file_name = "energy.log"
2022-10-04 13:21:05 +08:00
type CefLogger struct {
logFile *os.File
logger *log.Logger
enable bool
2022-10-05 21:06:29 +08:00
isInit bool
2022-10-04 13:21:05 +08:00
level CefLoggerLevel
}
2022-10-05 20:43:25 +08:00
var logger = &CefLogger{}
2022-10-04 13:21:05 +08:00
2022-10-05 21:06:29 +08:00
func loggerInit() {
if logger.isInit {
return
}
logger.isInit = true
2022-10-04 13:21:05 +08:00
logFile, err := os.OpenFile(log_file_name, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
2022-10-19 16:35:47 +08:00
logger.enable = false
2022-10-04 13:21:05 +08:00
return
}
2022-10-05 20:43:25 +08:00
logger.enable = true
logger.logFile = logFile
logger.level = CefLog_Error
logger.logger = log.New(io.MultiWriter(os.Stdout, logFile), "", log.Ldate|log.Ltime)
2022-10-04 13:21:05 +08:00
}
2022-10-05 20:43:25 +08:00
func SetLevel(l CefLoggerLevel) {
logger.level = l
2022-10-04 13:21:05 +08:00
}
2022-10-05 20:43:25 +08:00
func SetEnable(enable bool) {
logger.enable = enable
2022-10-05 21:06:29 +08:00
if enable {
loggerInit()
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Error(v ...interface{}) {
if logger.enable && logger.level >= CefLog_Error {
logger.logger.SetPrefix("[CEF-LCL-Error] ")
logger.logger.Println(v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Errorf(format string, v ...interface{}) {
if logger.enable && logger.level >= CefLog_Error {
logger.logger.SetPrefix("[CEF-LCL-Error] ")
logger.logger.Printf(format, v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Info(v ...interface{}) {
if logger.enable && logger.level >= CefLog_Info {
logger.logger.SetPrefix("[CEF-LCL-Info] ")
logger.logger.Println(v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Infof(format string, v ...interface{}) {
if logger.enable && logger.level >= CefLog_Info {
logger.logger.SetPrefix("[CEF-LCL-Info] ")
logger.logger.Printf(format, v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Debug(v ...interface{}) {
if logger.enable && logger.level >= CefLog_Debug {
logger.logger.SetPrefix("[CEF-LCL-Debug] ")
logger.logger.Println(v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-05 20:43:25 +08:00
func Debugf(format string, v ...interface{}) {
if logger.enable && logger.level >= CefLog_Debug {
logger.logger.SetPrefix("[CEF-LCL-Debug] ")
logger.logger.Printf(format, v...)
2022-10-04 13:21:05 +08:00
}
}
2022-10-20 14:16:38 +08:00
func Fatal(v ...interface{}) {
if logger.enable {
logger.logger.SetPrefix("[CEF-LCL-Fatal] ")
logger.logger.Fatal(v...)
}
}
func Fatalf(format string, v ...interface{}) {
if logger.enable {
logger.logger.SetPrefix("[CEF-LCL-Fatal] ")
logger.logger.Fatalf(format, v...)
}
}