-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
45 lines (32 loc) · 1.2 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2024, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package youless
import (
"context"
"log"
"net/http"
)
type Logger interface {
LogClientRequest(ctx context.Context, clientName, url string, shared bool)
LogFetchAuthCookie(clientName string, cookie http.Cookie)
}
const panicNilLog = "youless.NewLogger: log.Logger should not be nil"
func NewLogger(l *log.Logger) Logger {
if l == nil {
panic(panicNilLog)
}
return &defaultLogger{l}
}
func DefaultLogger() Logger { return &defaultLogger{log.Default()} }
type defaultLogger struct{ *log.Logger }
func (l *defaultLogger) LogClientRequest(_ context.Context, name, url string, shared bool) {
l.Logger.Printf("client %s requesting %s (shared: %t)\n", name, url, shared)
}
func (l *defaultLogger) LogFetchAuthCookie(name string, cookie http.Cookie) {
l.Logger.Printf("client %s fetched auth cookie: %s\n", name, cookie.String())
}
func NopLogger() Logger { return new(nopLogger) }
type nopLogger struct{}
func (nopLogger) LogClientRequest(_ context.Context, _, _ string, _ bool) {}
func (nopLogger) LogFetchAuthCookie(_ string, _ http.Cookie) {}