[mirror] Scalable static site server for Git forges (like GitHub Pages)
1package git_pages
2
3import (
4 "context"
5 "fmt"
6 "log/slog"
7 "os"
8 "runtime"
9 "strings"
10 "time"
11)
12
13var logc slogWithCtx
14
15type slogWithCtx struct{}
16
17func (l slogWithCtx) log(ctx context.Context, level slog.Level, msg string) {
18 if ctx == nil {
19 ctx = context.Background()
20 }
21 logger := slog.Default()
22 if !logger.Enabled(ctx, level) {
23 return
24 }
25
26 var pcs [1]uintptr
27 // skip [runtime.Callers, this method, method calling this method]
28 runtime.Callers(3, pcs[:])
29
30 record := slog.NewRecord(time.Now(), level, strings.TrimRight(msg, "\n"), pcs[0])
31 logger.Handler().Handle(ctx, record)
32}
33
34func (l slogWithCtx) Print(ctx context.Context, v ...any) {
35 l.log(ctx, slog.LevelInfo, fmt.Sprint(v...))
36}
37
38func (l slogWithCtx) Printf(ctx context.Context, format string, v ...any) {
39 l.log(ctx, slog.LevelInfo, fmt.Sprintf(format, v...))
40}
41
42func (l slogWithCtx) Println(ctx context.Context, v ...any) {
43 l.log(ctx, slog.LevelInfo, fmt.Sprintln(v...))
44}
45
46func (l slogWithCtx) Fatalf(ctx context.Context, format string, v ...any) {
47 l.log(ctx, slog.LevelError, fmt.Sprintf(format, v...))
48 os.Exit(1)
49}
50
51func (l slogWithCtx) Fatalln(ctx context.Context, v ...any) {
52 l.log(ctx, slog.LevelError, fmt.Sprintln(v...))
53 os.Exit(1)
54}