energy/logger/logger.go

122 lines
2.4 KiB
Go
Raw Normal View History

2022-10-04 16:38:43 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 16:38:43 +08:00
//
//----------------------------------------
// Package logger Simple log output
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Error] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Error] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Info] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Info] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Debug] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Debug] ")
2022-10-05 20:43:25 +08:00
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 {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Fatal] ")
2022-10-20 14:16:38 +08:00
logger.logger.Fatal(v...)
}
}
func Fatalf(format string, v ...interface{}) {
if logger.enable {
2023-01-29 13:26:02 +08:00
logger.logger.SetPrefix("[ENERGY-Fatal] ")
2022-10-20 14:16:38 +08:00
logger.logger.Fatalf(format, v...)
}
}