inital commit
This commit is contained in:
71
logger/ConsoleLogger.go
Normal file
71
logger/ConsoleLogger.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (lgr *ConsoleLoggerimpl) init() {
|
||||
lgr.messages = make(chan string, logbuffersize)
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) StartLogger() {
|
||||
fmt.Println("Starting Logger")
|
||||
loggerlogonce.Do(func() {
|
||||
for msg := range logger.messages {
|
||||
fmt.Print(msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) Write(message string) {
|
||||
logger.messages <- time.Now().Format(time.UnixDate) + " : " + message + "\n"
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) Write_Request(message string, uuid string) {
|
||||
logger.Write(uuid + " : " + message)
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) WriteErr(err error) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write("Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) WriteErr_Request(err error, uuid string) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write(uuid + " : Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) Write_DEBUG(message string) {
|
||||
if DEBUG {
|
||||
logger.Write(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) Write_Request_DEBUG(message string, uuid string) {
|
||||
if DEBUG {
|
||||
logger.Write_Request(message, uuid)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) WriteErr_DEBUG(err error) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write_DEBUG("Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *ConsoleLoggerimpl) WriteErr_Request_DEBUG(err error, uuid string) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write_DEBUG(uuid + " : Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
111
logger/FileLogger.go
Normal file
111
logger/FileLogger.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (lgr *FileLoggerimpl) init() {
|
||||
lgr.filepath = "./log"
|
||||
lgr.messages = make(chan string, logbuffersize)
|
||||
envfp, envexist := os.LookupEnv("LOGFILE_GO_LOGGER")
|
||||
if envexist {
|
||||
if len(envfp) > 0 {
|
||||
lgr.filepath = envfp
|
||||
} else {
|
||||
Logger().Write_DEBUG(fmt.Sprintf("LOGFILE_GO_LOGGER env exist but has an empty value using default value: %s !\n", lgr.filepath))
|
||||
}
|
||||
} else {
|
||||
Logger().Write_DEBUG(fmt.Sprintf("LOGFILE_GO_LOGGER env doesn't exist using default value: %s !\n", lgr.filepath))
|
||||
}
|
||||
f, err := os.OpenFile(lgr.filepath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
// We probably really don't want to continue execution without file backed logging
|
||||
panic(fmt.Sprintf("Error opening or creating file: %s", err.Error()))
|
||||
}
|
||||
lgr.logFile = f
|
||||
lgr.mutex = &sync.Mutex{}
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) StartLogger() {
|
||||
fmt.Println("Starting FileLogger")
|
||||
loggerlogonce.Do(func() {
|
||||
for msg := range logger.messages {
|
||||
|
||||
logger.mutex.Lock()
|
||||
_, err := logger.logFile.WriteString(msg)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
logger.logFile.Close()
|
||||
logger.mutex.Unlock()
|
||||
panic("Failed to write to file")
|
||||
}
|
||||
err = logger.logFile.Sync()
|
||||
if err != nil {
|
||||
logger.logFile.Close()
|
||||
logger.mutex.Unlock()
|
||||
|
||||
panic("Failed to write to file")
|
||||
}
|
||||
logger.mutex.Unlock()
|
||||
}
|
||||
})
|
||||
// Technically we should do this but this will never run
|
||||
// logger.mutex.Lock()
|
||||
// logger.logFile.Close()
|
||||
// logger.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) Write(message string) {
|
||||
logger.messages <- time.Now().Format(time.UnixDate) + " : " + message + "\n"
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) Write_Request(message string, request string) {
|
||||
logger.Write(request + " : " + message)
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) WriteErr(err error) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write("Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) WriteErr_Request(err error, uuid string) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write(uuid + " : Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) Write_DEBUG(message string) {
|
||||
if DEBUG {
|
||||
logger.Write(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) Write_Request_DEBUG(message string, uuid string) {
|
||||
if DEBUG {
|
||||
logger.Write_Request(message, uuid)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) WriteErr_DEBUG(err error) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write_DEBUG("Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *FileLoggerimpl) WriteErr_Request_DEBUG(err error, uuid string) (errnum int) {
|
||||
if err != nil {
|
||||
logger.Write_DEBUG(uuid + " : Error: " + err.Error())
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
48
logger/Logger.go
Normal file
48
logger/Logger.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Use this in the init() function to initialize the size of the buffered channel
|
||||
const logbuffersize int32 = 200
|
||||
|
||||
var DEBUG bool = true
|
||||
|
||||
var (
|
||||
loggerInstance LGRImpl
|
||||
loggeronce sync.Once
|
||||
loggerlogonce sync.Once
|
||||
)
|
||||
|
||||
func Create(instance LGRImpl) {
|
||||
loggeronce.Do(func() {
|
||||
loggerInstance = instance
|
||||
loggerInstance.init()
|
||||
})
|
||||
}
|
||||
|
||||
func Logger() LGRImpl {
|
||||
return loggerInstance
|
||||
}
|
||||
|
||||
func PrintJson[T any](entity *T) string {
|
||||
typename := reflect.TypeFor[T]().Name()
|
||||
outputStringJson, err := json.MarshalIndent((*entity), "", " ")
|
||||
if err != nil {
|
||||
return "Error parsing json data"
|
||||
} else {
|
||||
return typename + ":\n" + string(outputStringJson) + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleLogMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
Logger().Write_DEBUG(fmt.Sprintf("\tRequest from: %s to: Host: %s URL: %s \tWith HEADERS: %s \tWith BODY: %s", r.RemoteAddr, r.Host, r.URL, r.Header, r.Body))
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
41
logger/NullLogger.go
Normal file
41
logger/NullLogger.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package logger
|
||||
|
||||
func (lgr *NullLoggerimpl) init() {}
|
||||
|
||||
func (logger *NullLoggerimpl) StartLogger() {}
|
||||
|
||||
func (logger *NullLoggerimpl) Write(message string) {}
|
||||
|
||||
func (logger *NullLoggerimpl) Write_Request(message string, request string) {}
|
||||
|
||||
func (logger *NullLoggerimpl) WriteErr(err error) (errnum int) {
|
||||
if err != nil {
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *NullLoggerimpl) WriteErr_Request(err error, request string) (errnum int) {
|
||||
if err != nil {
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *NullLoggerimpl) Write_DEBUG(message string) {}
|
||||
|
||||
func (logger *NullLoggerimpl) Write_Request_DEBUG(message string, uuid string) {}
|
||||
|
||||
func (logger *NullLoggerimpl) WriteErr_DEBUG(err error) (errnum int) {
|
||||
if err != nil {
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
|
||||
func (logger *NullLoggerimpl) WriteErr_Request_DEBUG(err error, uuid string) (errnum int) {
|
||||
if err != nil {
|
||||
errnum = 1
|
||||
}
|
||||
return errnum
|
||||
}
|
||||
47
logger/interfaces.go
Normal file
47
logger/interfaces.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package logger
|
||||
|
||||
// Message format(s)
|
||||
//
|
||||
// Unixdate : message\n
|
||||
//
|
||||
// Unixdate : Error: error\n
|
||||
//
|
||||
// Unixdate : uuid : message\n
|
||||
//
|
||||
// Unixdate : uuid : Error: error\n
|
||||
type LGRImpl interface {
|
||||
LoggerI
|
||||
DebugLoggerI
|
||||
}
|
||||
|
||||
type (
|
||||
LoggerI interface {
|
||||
// Private, use it for member initialization etc
|
||||
init()
|
||||
// Start an infinite loop to write out messages from the channel
|
||||
StartLogger()
|
||||
Write(message string)
|
||||
Write_Request(message string, uuid string)
|
||||
// If an error that is not nill passed in it logs the error and returns 1, otherwise 0
|
||||
WriteErr(error) int
|
||||
WriteErr_Request(err error, uuid string) int
|
||||
}
|
||||
// Use _DEBUG prints to strip them out of release builds
|
||||
DebugLoggerI interface {
|
||||
// Private, use it for member initialization etc
|
||||
init()
|
||||
// Start an infinite loop to write out messages from the channel
|
||||
StartLogger()
|
||||
Write_DEBUG(message string)
|
||||
Write_Request_DEBUG(message string, uuid string)
|
||||
WriteErr_DEBUG(err error) (errnum int)
|
||||
WriteErr_Request_DEBUG(err error, uuid string) int
|
||||
}
|
||||
)
|
||||
|
||||
// Ensure all methods from LGRImpl are implemented ccompile time
|
||||
var (
|
||||
_ LGRImpl = (*NullLoggerimpl)(nil)
|
||||
_ LGRImpl = (*ConsoleLoggerimpl)(nil)
|
||||
_ LGRImpl = (*FileLoggerimpl)(nil)
|
||||
)
|
||||
21
logger/types.go
Normal file
21
logger/types.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A logger without logging functionality
|
||||
type NullLoggerimpl struct{}
|
||||
|
||||
// A logger that logs to sdtout
|
||||
type ConsoleLoggerimpl struct {
|
||||
messages chan string
|
||||
}
|
||||
|
||||
type FileLoggerimpl struct {
|
||||
messages chan string
|
||||
mutex *sync.Mutex
|
||||
logFile *os.File
|
||||
filepath string
|
||||
}
|
||||
Reference in New Issue
Block a user