Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions BENCHMARKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 27 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestLogInfo(t *testing.T) {
func TestLoggedMessage(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)

Expand All @@ -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")
Expand All @@ -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")
Expand Down