Files
cmp-go-snippet/cmpSnipper/cmpSnipperLog.go
2024-02-16 19:11:37 +03:00

71 lines
1.1 KiB
Go

package cmpSnipper
import (
"fmt"
"log"
"time"
"encoding/json"
)
func (app *App) LogMod(arg interface{}) string {
var add bool = false
var str string
var prf string
switch t := arg.(type) {
case nil:
prf = "N:"
str = "null"
case string:
prf = "S:"
str = t
case error:
prf = "E:"
str = t.Error()
case byte:
prf = "B:"
str = string(t)
case []byte:
prf = "A:"
str = string(t)
case interface{}:
prf = "J:"
obyte, _ := json.MarshalIndent(arg, "", "\t")
str = string(obyte)
default:
prf = "U:"
str = fmt.Sprintf("Unknown type %T", t)
}
if add {
str = prf + str
}
return str
}
func (app *App) Log(farg any, iargs ...interface{}) {
var str = app.LogMod(farg)
log.SetPrefix(time.Now().UTC().Format("2006-01-02 15:04:05") + ": ")
oargs := make([]interface{}, len(iargs))
for k, arg := range iargs {
oargs[k] = app.LogMod(arg)
}
log.Output(2, fmt.Sprintf(str + "\n", oargs...))
}
func (app *App) LogSetup() {
log.SetFlags( log.Lshortfile );
}