github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/hlog.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     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  
    17  package hlog
    18  
    19  import (
    20  	"io"
    21  	"log"
    22  	"os"
    23  )
    24  
    25  var (
    26  	// Provide default logger for users to use
    27  	logger FullLogger = &defaultLogger{
    28  		stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
    29  		depth:  4,
    30  	}
    31  
    32  	// Provide system logger for print system log
    33  	sysLogger FullLogger = &systemLogger{
    34  		&defaultLogger{
    35  			stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
    36  			depth:  4,
    37  		},
    38  		systemLogPrefix,
    39  	}
    40  )
    41  
    42  // SetOutput sets the output of default logger and system logger. By default, it is stderr.
    43  func SetOutput(w io.Writer) {
    44  	logger.SetOutput(w)
    45  	sysLogger.SetOutput(w)
    46  }
    47  
    48  // SetLevel sets the level of logs below which logs will not be output.
    49  // The default logger and system logger level is LevelTrace.
    50  // Note that this method is not concurrent-safe.
    51  func SetLevel(lv Level) {
    52  	logger.SetLevel(lv)
    53  	sysLogger.SetLevel(lv)
    54  }
    55  
    56  // DefaultLogger return the default logger for hertz.
    57  func DefaultLogger() FullLogger {
    58  	return logger
    59  }
    60  
    61  // SystemLogger return the system logger for hertz to print system log.
    62  // This function is not recommended for users to use.
    63  func SystemLogger() FullLogger {
    64  	return sysLogger
    65  }
    66  
    67  // SetSystemLogger sets the system logger.
    68  // Note that this method is not concurrent-safe and must not be called
    69  // This function is not recommended for users to use.
    70  func SetSystemLogger(v FullLogger) {
    71  	sysLogger = &systemLogger{v, systemLogPrefix}
    72  }
    73  
    74  // SetLogger sets the default logger and the system logger.
    75  // Note that this method is not concurrent-safe and must not be called
    76  // after the use of DefaultLogger and global functions in this package.
    77  func SetLogger(v FullLogger) {
    78  	logger = v
    79  	SetSystemLogger(v)
    80  }