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
10 changes: 8 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ linters:
- nestif # cyclop does this
- nlreturn # Similar to wsl, I think best left to judgement
- nonamedreturns # Named returns are often helpful, it's naked returns that are the issue
- noinlineerr # This is more readable in some cases
- paralleltest # I've never had Go tests take longer than a few seconds, it's fine
- unparam # gopls is better and more subtle
- varnamelen # Lots of false positives of things that are fine
Expand All @@ -56,8 +57,8 @@ linters:

- path: examples
linters:
- gosec
- mnd
- gosec

settings:
cyclop:
Expand Down Expand Up @@ -157,7 +158,12 @@ linters:
disabled: true # predeclared does this

- name: unhandled-error
disabled: true # errcheck does this
arguments:
- fmt\.(Fp|P)rint(ln|f)?
- strings.Builder.Write(String|Byte)?
- bytes.Buffer.Write(String|Byte)?
- bytes.Buffer.WriteTo?
- os.File.Close?

- name: flag-parameter
disabled: true # As far as I can work out this just doesn't like bools
Expand Down
5 changes: 3 additions & 2 deletions examples/prefix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"math/rand/v2"
"net/http"
"os"
"time"

Expand All @@ -17,9 +18,9 @@ func main() {

prefixed.Warn("Slow endpoint", "endpoint", "users/slow", "duration", 10*time.Second)
sleep()
prefixed.Info("Response from get repos", "status", 200, "duration", 500*time.Millisecond)
prefixed.Info("Response from get repos", "status", http.StatusOK, "duration", 500*time.Millisecond)
sleep()
prefixed.Error("Response from something else", "status", 400, "duration", 33*time.Millisecond)
prefixed.Error("Response from something else", "status", http.StatusBadRequest, "duration", 33*time.Millisecond)
}

func sleep() {
Expand Down
10 changes: 9 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// intended to be human readable and easy on the eye with a good choice of colours, ideal for command line
// applications that have a --debug or --verbose flag that enables extra logging.
//
// log emphasis simplicity and efficiency so there aren't too many knobs to twiddle, you just get a consistent,
// log emphasises simplicity and efficiency so there aren't too many knobs to twiddle, you just get a consistent,
// easy to use, simple logger with minimal overhead.
package log // import "go.followtheprocess.codes/log"

Expand Down Expand Up @@ -106,6 +106,7 @@ func (l *Logger) With(kv ...any) *Logger {
sub := l.clone()

sub.kv = slices.Concat(sub.kv, kv)

return sub
}

Expand All @@ -116,6 +117,7 @@ func (l *Logger) Prefixed(prefix string) *Logger {
sub := l.clone()

sub.prefix = prefix

return sub
}

Expand Down Expand Up @@ -155,15 +157,18 @@ func (l *Logger) log(level Level, msg string, kv ...any) {
buf.WriteString(timestampStyle.Text(l.timeFunc().Format(l.timeFormat)))
buf.WriteByte(' ')
buf.WriteString(level.styled())

if l.prefix != "" {
buf.WriteString(" " + prefixStyle.Text(l.prefix))
}

buf.WriteByte(':')
buf.WriteByte(' ')
buf.WriteString(msg)

if numKVs := len(l.kv) + len(kv); numKVs != 0 {
kvs := make([]any, 0, numKVs)

kvs = append(kvs, l.kv...)
if len(kvs)%2 != 0 {
kvs = append(kvs, missingValue)
Expand All @@ -176,6 +181,7 @@ func (l *Logger) log(level Level, msg string, kv ...any) {

for i := 0; i < len(kvs); i += 2 {
buf.WriteByte(' ')

key := keyStyle.Sprint(kvs[i])
val := fmt.Sprintf("%+v", kvs[i+1])

Expand All @@ -194,6 +200,7 @@ func (l *Logger) log(level Level, msg string, kv ...any) {
// WriteTo drains the buffer
l.mu.Lock()
defer l.mu.Unlock()

buf.WriteTo(l.w) //nolint: errcheck // Just like printing
}

Expand Down Expand Up @@ -226,6 +233,7 @@ var bufPool = sync.Pool{
func getBuffer() *bytes.Buffer {
buf := bufPool.Get().(*bytes.Buffer) //nolint:revive,errcheck,forcetypeassert // We are in total control of this
buf.Reset()

return buf
}

Expand Down
4 changes: 4 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,21 @@ func TestRace(t *testing.T) {

var wg sync.WaitGroup
wg.Add(n)

for i := range n {
go func(wg *sync.WaitGroup, i int) {
defer wg.Done()

logger.Info(fmt.Sprintf("Something: %d", i))
}(&wg, i)
}

wg.Add(n)

for i := range n {
go func(wg *sync.WaitGroup, i int) {
defer wg.Done()

sub.Info(fmt.Sprintf("Other: %d", i))
}(&wg, i)
}
Expand Down
Loading