Skip to content

Commit

Permalink
Automated commit by Forgejo Actions - Personal Bot
Browse files Browse the repository at this point in the history
  • Loading branch information
poniatowski-bot committed Jul 20, 2024
1 parent c76895c commit 1ee8e71
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
40 changes: 35 additions & 5 deletions boillog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"time"

"golang.org/x/exp/slog"
)

// ///////////////////////////== Environment Variables ==////////////////////////////
//

// envProfiler Gets the PROFILER environment variable (bool) and returns it
func envMetrics() string {
profiler := os.Getenv("METRICS")
if len(profiler) == 0 {
profiler = "false"
}
return profiler
}

// envProfiler Gets the PROFILER environment variable (bool) and returns it
func envProfiler() string {
profiler := os.Getenv("PROFILER")
Expand Down Expand Up @@ -55,7 +65,17 @@ const (
// LogIt Boilerplate funtion that calls Logger, to write logs, and prints it if it fails to write it
func LogIt(logFunction string, logOutput string, message string) {
logPath := filepath.Join(envLogLocation(), envAppName())
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
_, err := os.Stat(logPath)
if os.IsNotExist(err) {
// File doesn't exist, create it
file, err := os.Create(logPath)
if err != nil {
log.Println("Error creating log file:", err)
return
}
file.Close()
}
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Println("Error opening log file:", err)
return
Expand Down Expand Up @@ -85,20 +105,30 @@ func logger(logFunction string, logOutput string, message string, w io.Writer) e
default:
logger.InfoContext(ctx, message)
}

return nil
}

// TrackTime defer this function right at the beginning, to track time from start to finish
// TrackTime defer this function right at the beginning, to track time from start to finish. And if you set METRICS to true, you'll get memory usage as well
func TrackTime(taskName string, pre time.Time) time.Duration {
elapsed := time.Since(pre)
startCPU := new(runtime.MemStats)
runtime.ReadMemStats(startCPU)
profiler, err := strconv.ParseBool(envProfiler())
if err != nil {
fmt.Println(err)
}
metrics, err := strconv.ParseBool(envMetrics())
if err != nil {
fmt.Println(err)
}
elapsedCPU := new(runtime.MemStats)
runtime.ReadMemStats(elapsedCPU)
elapsed := time.Since(pre)
if profiler {
fmt.Printf("%v ", taskName)
fmt.Println("elapsed:", elapsed)
}
if metrics {
fmt.Println("Memory usage:", elapsedCPU.TotalAlloc-startCPU.TotalAlloc)
}
return elapsed
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module github.com/APoniatowski/boillog

go 1.21.3
go 1.22.5

require golang.org/x/exp v0.0.0-20231108232855-2478ac86f678

require (
github.com/APoniatowski/boillog v1.1.0 // indirect
github.com/APoniatowski/funcmytemplate v0.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/APoniatowski/boillog v1.1.0 h1:jceyfIZCuGSn4e3Rlo0JOXK6C46Gce6MpITowkbeX1E=
github.com/APoniatowski/boillog v1.1.0/go.mod h1:c7PpNRmlFv6tFVu9ogT/i1VwGsOeWRY8gRX14yHYpRQ=
github.com/APoniatowski/funcmytemplate v0.0.1 h1:iuMga0N5U4umc28iucvy0oPFlW7JCyrT8l7Z9xMsvwU=
github.com/APoniatowski/funcmytemplate v0.0.1/go.mod h1:d6IWMKOFbDyEPb46miTukA2c/4yHFahDC9wm5e/6EOE=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w=
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=

0 comments on commit 1ee8e71

Please sign in to comment.