Debian Codegraph @

common/loggers/loggerglobal.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 // Some functions in this file (see comments) is based on the Go source code,
3 // copyright The Go Authors and governed by a BSD-style license.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package loggers
17
18 import (
19 "sync"
20
21 "github.com/bep/logg"
22 )
23
24 // SetGlobalLogger sets the global logger.
25 // This is used in a few places in Hugo, e.g. deprecated functions.
26 func SetGlobalLogger(logger Logger) {
27 logMu.Lock()
28 defer logMu.Unlock()
29 log = logger
30 }
31
32 func initGlobalLogger(level logg.Level, panicOnWarnings bool) {
33 logMu.Lock()
34 defer logMu.Unlock()
35 var logHookLast func(e *logg.Entry) error
36 if panicOnWarnings {
37 logHookLast = PanicOnWarningHook
38 }
39
40 log = New(
41 Options{
42 Level: level,
43 DistinctLevel: logg.LevelInfo,
44 HandlerPost: logHookLast,
45 },
46 )
47 }
48
49 var logMu sync.Mutex
50
51 func Log() Logger {
52 logMu.Lock()
53 defer logMu.Unlock()
54 return log
55 }
56
57 // The global logger.
58 var log Logger
59
60 func init() {
61 initGlobalLogger(logg.LevelWarn, false)
62 }