104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package umlog
|
|
|
|
import (
|
|
"AngkorWalletScanning/ummessage"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Logger struct {
|
|
Debug *log.Logger
|
|
Info *log.Logger
|
|
Warn *log.Logger
|
|
Error *log.Logger
|
|
}
|
|
|
|
var (
|
|
logFile *os.File
|
|
umLogger Logger
|
|
log_level string = ""
|
|
)
|
|
|
|
// 패키지 외부에서 사용하기 위해서는 첫문자는 대문자로...
|
|
func Init(logfile string, messageToken string, messageId string) error {
|
|
var err error
|
|
logFile, err = os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
umLogger.Debug = log.New(logFile, "[DEBUG] ", log.Ldate|log.Ltime)
|
|
umLogger.Info = log.New(logFile, "[INFO] ", log.Ldate|log.Ltime)
|
|
umLogger.Warn = log.New(logFile, "[WARN] ", log.Ldate|log.Ltime)
|
|
umLogger.Error = log.New(logFile, "[ERROR] ", log.Ldate|log.Ltime)
|
|
|
|
ummessage.InitTelegramBot(messageToken, messageId)
|
|
|
|
return nil
|
|
}
|
|
|
|
func Close() {
|
|
logFile.Close()
|
|
}
|
|
|
|
func SetLevel(level string) {
|
|
switch level {
|
|
case "I":
|
|
log_level = "info"
|
|
case "W":
|
|
log_level = "warn"
|
|
case "E":
|
|
log_level = "error"
|
|
default:
|
|
log_level = "debug"
|
|
}
|
|
}
|
|
|
|
func debugInfo() (string, string, string) {
|
|
funcname, filename, line, _ := runtime.Caller(2)
|
|
functionname := runtime.FuncForPC(funcname).Name()
|
|
filenames := strings.Split(filename, "/")
|
|
return filenames[len(filenames)-1], functionname, strconv.Itoa(line)
|
|
}
|
|
|
|
func Message(title string, body string) {
|
|
chatId := int64(-5029236432)
|
|
token := "7108014537:AAHU7299mCzHsBJp1KmPMp7tZEGfGsQ-ffM"
|
|
ummessage.SendMessageDirect(chatId, token, title, body)
|
|
}
|
|
func Error(format string, args ...interface{}) {
|
|
if log_level == "error" || log_level == "warn" || log_level == "info" || log_level == "debug" {
|
|
filename, function, line := debugInfo()
|
|
message := fmt.Sprintf(format, args...)
|
|
umLogger.Error.Println("[" + filename + "][" + function + "][" + line + "] " + message)
|
|
ummessage.SendMessage("["+filename+"]["+function+"]["+line+"]", message)
|
|
}
|
|
}
|
|
|
|
func Warn(format string, args ...interface{}) {
|
|
if log_level == "warn" || log_level == "info" || log_level == "debug" {
|
|
filename, _, line := debugInfo()
|
|
message := fmt.Sprintf(format, args...)
|
|
umLogger.Warn.Println("[" + filename + "][" + line + "] " + message)
|
|
}
|
|
}
|
|
|
|
func Info(format string, args ...interface{}) {
|
|
if log_level == "info" || log_level == "debug" {
|
|
filename, _, line := debugInfo()
|
|
message := fmt.Sprintf(format, args...)
|
|
umLogger.Info.Println("[" + filename + "][" + line + "] " + message)
|
|
}
|
|
}
|
|
|
|
func Debug(format string, args ...interface{}) {
|
|
if log_level == "debug" {
|
|
filename, function, line := debugInfo()
|
|
message := fmt.Sprintf(format, args...)
|
|
umLogger.Debug.Println("[" + filename + "][" + function + "][" + line + "] " + message)
|
|
}
|
|
}
|