github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/default.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  	"context"
    21  	"fmt"
    22  	"io"
    23  	"log"
    24  	"os"
    25  )
    26  
    27  // Fatal calls the default logger's Fatal method and then os.Exit(1).
    28  func Fatal(v ...interface{}) {
    29  	logger.Fatal(v...)
    30  }
    31  
    32  // Error calls the default logger's Error method.
    33  func Error(v ...interface{}) {
    34  	logger.Error(v...)
    35  }
    36  
    37  // Warn calls the default logger's Warn method.
    38  func Warn(v ...interface{}) {
    39  	logger.Warn(v...)
    40  }
    41  
    42  // Notice calls the default logger's Notice method.
    43  func Notice(v ...interface{}) {
    44  	logger.Notice(v...)
    45  }
    46  
    47  // Info calls the default logger's Info method.
    48  func Info(v ...interface{}) {
    49  	logger.Info(v...)
    50  }
    51  
    52  // Debug calls the default logger's Debug method.
    53  func Debug(v ...interface{}) {
    54  	logger.Debug(v...)
    55  }
    56  
    57  // Trace calls the default logger's Trace method.
    58  func Trace(v ...interface{}) {
    59  	logger.Trace(v...)
    60  }
    61  
    62  // Fatalf calls the default logger's Fatalf method and then os.Exit(1).
    63  func Fatalf(format string, v ...interface{}) {
    64  	logger.Fatalf(format, v...)
    65  }
    66  
    67  // Errorf calls the default logger's Errorf method.
    68  func Errorf(format string, v ...interface{}) {
    69  	logger.Errorf(format, v...)
    70  }
    71  
    72  // Warnf calls the default logger's Warnf method.
    73  func Warnf(format string, v ...interface{}) {
    74  	logger.Warnf(format, v...)
    75  }
    76  
    77  // Noticef calls the default logger's Noticef method.
    78  func Noticef(format string, v ...interface{}) {
    79  	logger.Noticef(format, v...)
    80  }
    81  
    82  // Infof calls the default logger's Infof method.
    83  func Infof(format string, v ...interface{}) {
    84  	logger.Infof(format, v...)
    85  }
    86  
    87  // Debugf calls the default logger's Debugf method.
    88  func Debugf(format string, v ...interface{}) {
    89  	logger.Debugf(format, v...)
    90  }
    91  
    92  // Tracef calls the default logger's Tracef method.
    93  func Tracef(format string, v ...interface{}) {
    94  	logger.Tracef(format, v...)
    95  }
    96  
    97  // CtxFatalf calls the default logger's CtxFatalf method and then os.Exit(1).
    98  func CtxFatalf(ctx context.Context, format string, v ...interface{}) {
    99  	logger.CtxFatalf(ctx, format, v...)
   100  }
   101  
   102  // CtxErrorf calls the default logger's CtxErrorf method.
   103  func CtxErrorf(ctx context.Context, format string, v ...interface{}) {
   104  	logger.CtxErrorf(ctx, format, v...)
   105  }
   106  
   107  // CtxWarnf calls the default logger's CtxWarnf method.
   108  func CtxWarnf(ctx context.Context, format string, v ...interface{}) {
   109  	logger.CtxWarnf(ctx, format, v...)
   110  }
   111  
   112  // CtxNoticef calls the default logger's CtxNoticef method.
   113  func CtxNoticef(ctx context.Context, format string, v ...interface{}) {
   114  	logger.CtxNoticef(ctx, format, v...)
   115  }
   116  
   117  // CtxInfof calls the default logger's CtxInfof method.
   118  func CtxInfof(ctx context.Context, format string, v ...interface{}) {
   119  	logger.CtxInfof(ctx, format, v...)
   120  }
   121  
   122  // CtxDebugf calls the default logger's CtxDebugf method.
   123  func CtxDebugf(ctx context.Context, format string, v ...interface{}) {
   124  	logger.CtxDebugf(ctx, format, v...)
   125  }
   126  
   127  // CtxTracef calls the default logger's CtxTracef method.
   128  func CtxTracef(ctx context.Context, format string, v ...interface{}) {
   129  	logger.CtxTracef(ctx, format, v...)
   130  }
   131  
   132  type defaultLogger struct {
   133  	stdlog *log.Logger
   134  	level  Level
   135  	depth  int
   136  }
   137  
   138  func (ll *defaultLogger) SetOutput(w io.Writer) {
   139  	ll.stdlog.SetOutput(w)
   140  }
   141  
   142  func (ll *defaultLogger) SetLevel(lv Level) {
   143  	ll.level = lv
   144  }
   145  
   146  func (ll *defaultLogger) logf(lv Level, format *string, v ...interface{}) {
   147  	if ll.level > lv {
   148  		return
   149  	}
   150  	msg := lv.toString()
   151  	if format != nil {
   152  		if len(v) > 0 {
   153  			msg += fmt.Sprintf(*format, v...)
   154  		} else {
   155  			msg += *format
   156  		}
   157  	} else {
   158  		msg += fmt.Sprint(v...)
   159  	}
   160  
   161  	ll.stdlog.Output(ll.depth, msg)
   162  	if lv == LevelFatal {
   163  		os.Exit(1)
   164  	}
   165  }
   166  
   167  func (ll *defaultLogger) Fatal(v ...interface{}) {
   168  	ll.logf(LevelFatal, nil, v...)
   169  }
   170  
   171  func (ll *defaultLogger) Error(v ...interface{}) {
   172  	ll.logf(LevelError, nil, v...)
   173  }
   174  
   175  func (ll *defaultLogger) Warn(v ...interface{}) {
   176  	ll.logf(LevelWarn, nil, v...)
   177  }
   178  
   179  func (ll *defaultLogger) Notice(v ...interface{}) {
   180  	ll.logf(LevelNotice, nil, v...)
   181  }
   182  
   183  func (ll *defaultLogger) Info(v ...interface{}) {
   184  	ll.logf(LevelInfo, nil, v...)
   185  }
   186  
   187  func (ll *defaultLogger) Debug(v ...interface{}) {
   188  	ll.logf(LevelDebug, nil, v...)
   189  }
   190  
   191  func (ll *defaultLogger) Trace(v ...interface{}) {
   192  	ll.logf(LevelTrace, nil, v...)
   193  }
   194  
   195  func (ll *defaultLogger) Fatalf(format string, v ...interface{}) {
   196  	ll.logf(LevelFatal, &format, v...)
   197  }
   198  
   199  func (ll *defaultLogger) Errorf(format string, v ...interface{}) {
   200  	ll.logf(LevelError, &format, v...)
   201  }
   202  
   203  func (ll *defaultLogger) Warnf(format string, v ...interface{}) {
   204  	ll.logf(LevelWarn, &format, v...)
   205  }
   206  
   207  func (ll *defaultLogger) Noticef(format string, v ...interface{}) {
   208  	ll.logf(LevelNotice, &format, v...)
   209  }
   210  
   211  func (ll *defaultLogger) Infof(format string, v ...interface{}) {
   212  	ll.logf(LevelInfo, &format, v...)
   213  }
   214  
   215  func (ll *defaultLogger) Debugf(format string, v ...interface{}) {
   216  	ll.logf(LevelDebug, &format, v...)
   217  }
   218  
   219  func (ll *defaultLogger) Tracef(format string, v ...interface{}) {
   220  	ll.logf(LevelTrace, &format, v...)
   221  }
   222  
   223  func (ll *defaultLogger) CtxFatalf(ctx context.Context, format string, v ...interface{}) {
   224  	ll.logf(LevelFatal, &format, v...)
   225  }
   226  
   227  func (ll *defaultLogger) CtxErrorf(ctx context.Context, format string, v ...interface{}) {
   228  	ll.logf(LevelError, &format, v...)
   229  }
   230  
   231  func (ll *defaultLogger) CtxWarnf(ctx context.Context, format string, v ...interface{}) {
   232  	ll.logf(LevelWarn, &format, v...)
   233  }
   234  
   235  func (ll *defaultLogger) CtxNoticef(ctx context.Context, format string, v ...interface{}) {
   236  	ll.logf(LevelNotice, &format, v...)
   237  }
   238  
   239  func (ll *defaultLogger) CtxInfof(ctx context.Context, format string, v ...interface{}) {
   240  	ll.logf(LevelInfo, &format, v...)
   241  }
   242  
   243  func (ll *defaultLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) {
   244  	ll.logf(LevelDebug, &format, v...)
   245  }
   246  
   247  func (ll *defaultLogger) CtxTracef(ctx context.Context, format string, v ...interface{}) {
   248  	ll.logf(LevelTrace, &format, v...)
   249  }