aboutsummaryrefslogtreecommitdiffhomepage
path: root/common/loggers/handlersmisc.go
blob: 5c9d6c0910a4c771c0a85c3fc1325035c856cd44 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2023 The Hugo Authors. All rights reserved.
// Some functions in this file (see comments) is based on the Go source code,
// copyright The Go Authors and  governed by a BSD-style license.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

import (
	"fmt"
	"strings"
	"sync"

	"github.com/bep/logg"
	"github.com/gohugoio/hugo/identity"
)

// PanicOnWarningHook panics on warnings.
var PanicOnWarningHook = func(e *logg.Entry) error {
	if e.Level != logg.LevelWarn {
		return nil
	}
	panic(e.Message)
}

func newLogLevelCounter() *logLevelCounter {
	return &logLevelCounter{
		counters: make(map[logg.Level]int),
	}
}

func newLogOnceHandler(threshold logg.Level) *logOnceHandler {
	return &logOnceHandler{
		threshold: threshold,
		seen:      make(map[uint64]bool),
	}
}

func newStopHandler(h ...logg.Handler) *stopHandler {
	return &stopHandler{
		handlers: h,
	}
}

func newSuppressStatementsHandler(statements map[string]bool) *suppressStatementsHandler {
	return &suppressStatementsHandler{
		statements: statements,
	}
}

type logLevelCounter struct {
	mu       sync.RWMutex
	counters map[logg.Level]int
}

func (h *logLevelCounter) HandleLog(e *logg.Entry) error {
	h.mu.Lock()
	defer h.mu.Unlock()
	h.counters[e.Level]++
	return nil
}

var stopError = fmt.Errorf("stop")

type logOnceHandler struct {
	threshold logg.Level
	mu        sync.Mutex
	seen      map[uint64]bool
}

func (h *logOnceHandler) HandleLog(e *logg.Entry) error {
	if e.Level < h.threshold {
		// We typically only want to enable this for warnings and above.
		// The common use case is that many go routines may log the same error.
		return nil
	}
	h.mu.Lock()
	defer h.mu.Unlock()
	hash := identity.HashUint64(e.Level, e.Message, e.Fields)
	if h.seen[hash] {
		return stopError
	}
	h.seen[hash] = true
	return nil
}

func (h *logOnceHandler) reset() {
	h.mu.Lock()
	defer h.mu.Unlock()
	h.seen = make(map[uint64]bool)
}

type stopHandler struct {
	handlers []logg.Handler
}

// HandleLog implements logg.Handler.
func (h *stopHandler) HandleLog(e *logg.Entry) error {
	for _, handler := range h.handlers {
		if err := handler.HandleLog(e); err != nil {
			if err == stopError {
				return nil
			}
			return err
		}
	}
	return nil
}

type suppressStatementsHandler struct {
	statements map[string]bool
}

func (h *suppressStatementsHandler) HandleLog(e *logg.Entry) error {
	for _, field := range e.Fields {
		if field.Name == FieldNameStatementID {
			if h.statements[field.Value.(string)] {
				return stopError
			}
		}
	}
	return nil
}

// replacer creates a new log handler that does string replacement in log messages.
func replacer(repl *strings.Replacer) logg.Handler {
	return logg.HandlerFunc(func(e *logg.Entry) error {
		e.Message = repl.Replace(e.Message)
		for i, field := range e.Fields {
			if s, ok := field.Value.(string); ok {
				e.Fields[i].Value = repl.Replace(s)
			}
		}
		return nil
	})
}

// whiteSpaceTrimmer creates a new log handler that trims whitespace from log messages and string fields.
func whiteSpaceTrimmer() logg.Handler {
	return logg.HandlerFunc(func(e *logg.Entry) error {
		e.Message = strings.TrimSpace(e.Message)
		for i, field := range e.Fields {
			if s, ok := field.Value.(string); ok {
				e.Fields[i].Value = strings.TrimSpace(s)
			}
		}
		return nil
	})
}