logs only show loc if level is above info, much neater for server log output

This commit is contained in:
loki
2023-06-23 19:53:35 +01:00
parent a6b81442bc
commit bb0b80e5c9
5 changed files with 61 additions and 15 deletions

View File

@@ -2,7 +2,7 @@
<configuration default="false" name="go test local" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="indra" />
<working_directory value="$PROJECT_DIR$/pkg" />
<go_parameters value="-v ./... -tags local -gcflags &quot;all=-trimpath=$ContentRoot$&quot;" />
<go_parameters value="-v ./... -tags local -gcflags &quot;all=-trimpath=$PROJECT_DIR$&quot;" />
<root_directory value="$PROJECT_DIR$" />
<kind value="DIRECTORY" />
<package value="git-indra.lan/indra-labs/indra/pkg/engine" />

View File

@@ -60,3 +60,8 @@ Later, rendezvous access protocols will be added and enable the creation of
arbitrary hidden service addresses such as web applications.
# fin
notes:
`([a-zA-z0-9\_\-\.][a-zA-z0-9\/\_\-\.]+)\:([0-9]+) ([a-zA-z0-9\_\-\.][a-zA-z0-9\/\_\-\.]+)\:([0-9]+)` is a regex that
matches the relative file paths in the log output. $1 and $2 from this are the relative path and the line number.

4
pkg/proc/log/length.go Normal file
View File

@@ -0,0 +1,4 @@
package log
const maxLen = 44

View File

@@ -44,11 +44,11 @@ var (
// LvlStr is a map that provides the uniform width strings that are printed
// to identify the LogLevel of a log entry.
LvlStr = LevelMap{
Off: "off ",
Off: "off",
Fatal: "fatal",
Error: "error",
Warn: "warn ",
Info: "info ",
Warn: "warn",
Info: "info",
Check: "check",
Debug: "debug",
Trace: "trace",
@@ -68,7 +68,7 @@ var (
"trace": Trace,
}
startTime = time.Now()
timeStampFormat = "15:04:05.000"
timeStampFormat = "15:04:05.000000"
tty io.Writer = os.Stderr
file *os.File
path string
@@ -84,6 +84,10 @@ var (
allSubsystems []string
)
func init() {
Longest.Store(maxLen)
}
type (
LevelMap map[LogLevel]string
// LogLevel is a code representing a scale of importance and context for log
@@ -326,7 +330,7 @@ func _s(level LogLevel, subsystem string) Prints {
}
logPrint(
level, subsystem, func() string {
return color.Gray.Sprint(text + spew.Sdump(a...))
return text + spew.Sdump(a...)
},
)()
}
@@ -384,26 +388,29 @@ func logPrint(
}
tsf := timeStampFormat
timeText := getTimeText(tsf)
loc := GetLoc(3, subsystem)
var loc string
if int(Longest.Load()) < len(loc) {
Longest.Store(uint32(len(loc)))
}
loc = color.OpItalic.Sprint(color.OpUnderscore.Sprint(loc)) + strings.Repeat(" ", int(Longest.Load())-len(loc)+1)
formatString := "%s%-6v %s %s %s"
timeText = time.Now().Format("2006-01-02 15:04:05.999999999 UTC+0700")
formatString := "%s%s %s %s"
//timeText = time.Now().Format("2006-01-02 15:04:05.999999999 UTC+0700")
if logLevel > Info {
loc = GetLoc(3, subsystem)
loc = loc + strings.Repeat(" ", int(Longest.Load())-len(loc)+1)
}
var app string
if len(App.Load()) > 0 {
app = fmt.Sprint(App.Load(), " ")
// app = fmt.Sprint(App.Load(), "")
app = App.Load()
}
s := fmt.Sprintf(
formatString,
app,
loc,
timeText,
LevelSpecs[level].Colorizer(
LevelSpecs[level].Name,
strings.ToUpper(app),
),
LevelSpecs[level].Colorizer(loc),
printFunc(),
color.Black.Sprint(timeText),
)
s = strings.TrimSuffix(s, "\n")
fmt.Fprintln(writer, s)

View File

@@ -0,0 +1,30 @@
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
func main() {
var max int
var longest string
filepath.Walk(os.Args[1], func(path string, info fs.FileInfo, err error) error {
if strings.HasPrefix(path, ".") ||
!strings.HasSuffix(path, ".go") ||
strings.HasSuffix(path, "_test.go") { // doesn't matter as much if test logs rel path grow initially
return nil
}
if len(path) > max {
max = len(path)
longest = path
}
return nil
})
if e := os.WriteFile("pkg/proc/log/length.go", []byte(fmt.Sprintf("package log\n\nconst maxLen = %d\n\n", max)), 0600); e != nil {
fmt.Println(e)
}
fmt.Println(longest)
}