diff --git a/BENCHMARKS.md b/BENCHMARKS.md index b1428db..8fbb8b6 100644 --- a/BENCHMARKS.md +++ b/BENCHMARKS.md @@ -17,42 +17,42 @@ The results below are generated on: * **CPU:** Apple M2 * **Go Version:** 1.24 -| Benchmark Case | Iterations | Memory Allocations | Bytes Allocations | Operation Allocations | -|---------------------------|------------|--------------------|-------------------|-----------------------| -| BenchmarkSyniolLogger-8 | 1891555 | 611.1 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1957856 | 613.0 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1957063 | 615.2 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1886391 | 621.1 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1942413 | 614.8 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1951993 | 618.5 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1935925 | 617.1 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1897650 | 624.7 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1945797 | 630.0 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSyniolLogger-8 | 1942968 | 628.3 ns/op | 664 B/op | 9 allocs/op | - | BenchmarkSlogJSON-8 | 2352092 | 510.8 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2353771 | 509.9 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2380887 | 510.9 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2371741 | 507.7 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2364392 | 513.5 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2346122 | 508.3 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2375124 | 504.0 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2372458 | 505.7 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2375211 | 502.5 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogJSON-8 | 2354427 | 504.1 ns/op | 0 B/op | 0 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1053 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1067 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1058 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1052 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1053 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1054 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1053 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1050 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1050 ns/op | 584 B/op | 6 allocs/op | - | BenchmarkSlogWithSource-8 | 1000000 | 1053 ns/op | 584 B/op | 6 allocs/op | +| Benchmark Case | Iterations | Memory Allocations | Bytes Allocations | Operation Allocations | +|---------------------------|------------|----------------------|-------------------|-----------------------| +| BenchmarkSyniolLogger-8 | 2792167 | 407.3 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2944899 | 408.6 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2927808 | 409.0 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2939458 | 409.0 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2938980 | 408.9 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2927814 | 411.8 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2787464 | 410.9 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2920656 | 409.7 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2928950 | 411.4 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger-8 | 2881113 | 410.9 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSlogJSON-8 | 2355129 | 508.6 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2355932 | 513.1 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2373483 | 511.4 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2355658 | 508.8 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2359935 | 510.0 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2353203 | 507.1 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2361937 | 517.4 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2343535 | 518.6 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2300044 | 516.1 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON-8 | 2342082 | 515.3 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1056 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1044 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1044 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1042 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1042 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1042 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1042 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1043 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1051 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource-8 | 1000000 | 1054 ns/op | 584 B/op | 6 allocs/op | ```shell PASS -ok github.com/syniol/go-logger 46.628s +ok github.com/syniol/go-logger 44.622s ``` diff --git a/README.md b/README.md index 9973758..f97f460 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,12 @@ if err != nil { } ``` +#### Zero-Loss Fatal Errors +Our LogFatal implementation uses Type Assertion to detect if the underlying `io.Writer` supports +synchronisation (like `*os.File`). This ensures that during a catastrophic failure, your logs are +flushed to the disk before the process terminates, preventing "ghost crashes" where the cause of +death is lost in a buffered stream. + ## Core Concepts: The Power of "Facility" In systems architecture, clarity is the antidote to complexity. When adapting the Syslog @@ -181,16 +187,22 @@ sylog.LogError("order-processor", "Database connection timed out") When this reaches **CloudWatch** or **Datadog**, you can simply run a query like facility: "order-processor" to see the entire lifecycle of that specific service's behavior. -## Performance Benchmarks + +## Performance and Benchmarks The document that tracks the performance of the logger package is published in details at the root of repository [`BENCHMARKS.md`](https://github.com/syniol/go-logger/blob/main/BENCHMARKS.md). - -| Benchmark Case | Iterations | Memory Allocations | Bytes Allocations | Operation Allocations | -|---------------------------|------------|--------------------|-------------------|-----------------------| -| BenchmarkSyniolLogger-8 | 1891555 | 611.1 ns/op | 664 B/op | 9 allocs/op | -| BenchmarkSlogJSON-8 | 2352092 | 510.8 ns/op | 0 B/op | 0 allocs/op | -| BenchmarkSlogWithSource-8 | 1000000 | 1053 ns/op | 584 B/op | 6 allocs/op | +| Benchmark Case | Iterations | Memory Allocations | Bytes Allocations | Operation Allocations | +|-------------------------|------------|----------------------|-------------------|-----------------------| +| BenchmarkSyniolLogger | 2792167 | 407.3 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger | 2944899 | 408.6 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSyniolLogger | 2927808 | 409.0 ns/op | 336 B/op | 6 allocs/op | +| BenchmarkSlogJSON | 2355129 | 508.6 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON | 2355932 | 513.1 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogJSON | 2373483 | 511.4 ns/op | 0 B/op | 0 allocs/op | +| BenchmarkSlogWithSource | 1000000 | 1056 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource | 1000000 | 1044 ns/op | 584 B/op | 6 allocs/op | +| BenchmarkSlogWithSource | 1000000 | 1044 ns/op | 584 B/op | 6 allocs/op | #### Credits diff --git a/logger.go b/logger.go index c60a1ea..ed61d34 100644 --- a/logger.go +++ b/logger.go @@ -47,13 +47,36 @@ type logger struct { Timestamp string `json:"timestamp"` } +// locCache prevents us from calling the expensive runtime.CallersFrames repeatedly +var locCache sync.Map + func logLocation() string { - _, fileName, fileLine, ok := runtime.Caller(2) - if !ok { - return "location: path could not be found" + // Get the Program Counter (PC) only. + // runtime.Callers method is faster than runtime.Caller because + // according to Go release docs it doesn't resolve symbols + var pcs [1]uintptr + n := runtime.Callers(2, pcs[:]) + if n == 0 { + return "unknown:0" + } + pc := pcs[0] + + // Fast Path Checking: Checks the cache for path + if val, ok := locCache.Load(pc); ok { + return val.(string) } - return `location: '` + fileName + `' on line: ` + strconv.Itoa(fileLine) + `` + // Slow Path: Resolve symbols only once per call-site + frames := runtime.CallersFrames(pcs[:n]) + frame, _ := frames.Next() + + // Format the string once (e.g., "location: 'main.go:42'") + locStr := `location: '` + frame.File + `:` + strconv.Itoa(frame.Line) + `'` + + // Store in cache for future hits at this exact code line + locCache.Store(pc, locStr) + + return locStr } func log(level logLevel, facility string, args []string) *logger { diff --git a/logger_test.go b/logger_test.go index 3516e06..7d79546 100644 --- a/logger_test.go +++ b/logger_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestLogInfo(t *testing.T) { +func TestLoggedMessage(t *testing.T) { var buf bytes.Buffer SetOutput(&buf) @@ -18,7 +18,7 @@ func TestLogInfo(t *testing.T) { } } -func TestLogWithEmptyArgument(t *testing.T) { +func TestLogWithEmptyArgumentExecution(t *testing.T) { LogInfo("PayNow Microservice", "Mocked Message") LogEmergency("PayNow Microservice") LogCritical("PayNow Microservice") @@ -28,7 +28,7 @@ func TestLogWithEmptyArgument(t *testing.T) { LogNotice("PayNow Microservice") } -func TestLog(t *testing.T) { +func TestLogExecution(t *testing.T) { LogInfo("PayNow Microservice", "SomeText", "lorem ipsum", " 3232") LogWarning("PayNow Microservice", "SomeText", "lorem ipsum", "3232")