github.com/neohugo/neohugo@v0.123.8/common/loggers/loggerglobal.go (about)

     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  func InitGlobalLogger(level logg.Level, panicOnWarnings bool) {
    25  	logMu.Lock()
    26  	defer logMu.Unlock()
    27  	var logHookLast func(e *logg.Entry) error
    28  	if panicOnWarnings {
    29  		logHookLast = PanicOnWarningHook
    30  	}
    31  
    32  	log = New(
    33  		Options{
    34  			Level:         level,
    35  			DistinctLevel: logg.LevelInfo,
    36  			HandlerPost:   logHookLast,
    37  		},
    38  	)
    39  }
    40  
    41  var logMu sync.Mutex
    42  
    43  func Log() Logger {
    44  	logMu.Lock()
    45  	defer logMu.Unlock()
    46  	return log
    47  }
    48  
    49  // The global logger.
    50  var log Logger
    51  
    52  func init() {
    53  	InitGlobalLogger(logg.LevelWarn, false)
    54  }