Skip to content

Commit

Permalink
Create env var for log level allow the use of log level in the logger…
Browse files Browse the repository at this point in the history
… util (#2250)

* create env var for log level allow the use of log level in the logger util

* pushed

* commit push
  • Loading branch information
MahtabBukhari authored Dec 19, 2024
1 parent efdcbc9 commit 5aa7662
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var MemeUrl string
var RelayAuthKey string
var RelayNodeKey string
var SuperAdmins []string = []string{""}
var LogLevel string

// these are constants for the store
var InvoiceList = "INVOICELIST"
Expand Down Expand Up @@ -67,6 +68,7 @@ func InitConfig() {
V2BotUrl = os.Getenv("V2_BOT_URL")
V2BotToken = os.Getenv("V2_BOT_TOKEN")
FfWebsocket = os.Getenv("FF_WEBSOCKET") == "true"
LogLevel = os.Getenv("LOG_LEVEL")

// Add to super admins
SuperAdmins = StripSuperAdmins(AdminStrings)
Expand Down Expand Up @@ -135,6 +137,10 @@ func InitConfig() {
if JarvisToken == "" {
JarvisToken = ""
}

if LogLevel == "" {
LogLevel = "DEBUG"
}
}

func StripSuperAdmins(adminStrings string) []string {
Expand Down
47 changes: 36 additions & 11 deletions utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@ package utils
import (
"log"
"os"
"strings"
)

type Logger struct {
infoLogger *log.Logger
errorLogger *log.Logger
debugLogger *log.Logger
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
debugLogger *log.Logger
machineLogger *log.Logger
logLevel string
}

var Log = Logger{
infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile),
errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile),
debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile),
infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile),
warningLogger: log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile),
errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile),
debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile),
machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile),
logLevel: strings.ToUpper(os.Getenv("LOG_LEVEL")),
}

func (l *Logger) Machine(format string, v ...interface{}) {
if l.logLevel == "MACHINE" {
l.machineLogger.Printf(format, v...)
}
}

func (l *Logger) Debug(format string, v ...interface{}) {
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" {
l.debugLogger.Printf(format, v...)
}
}

func (l *Logger) Info(format string, v ...interface{}) {
l.infoLogger.Printf(format, v...)
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" {
l.infoLogger.Printf(format, v...)
}
}

func (l *Logger) Error(format string, v ...interface{}) {
l.errorLogger.Printf(format, v...)
func (l *Logger) Warning(format string, v ...interface{}) {
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" {
l.warningLogger.Printf(format, v...)
}
}

func (l *Logger) Debug(format string, v ...interface{}) {
l.debugLogger.Printf(format, v...)
func (l *Logger) Error(format string, v ...interface{}) {
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" || l.logLevel == "ERROR" {
l.errorLogger.Printf(format, v...)
}
}

0 comments on commit 5aa7662

Please sign in to comment.