github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/log.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 ) 24 25 // FormatLogger is a logger interface that output logs with a format. 26 type FormatLogger interface { 27 Tracef(format string, v ...interface{}) 28 Debugf(format string, v ...interface{}) 29 Infof(format string, v ...interface{}) 30 Noticef(format string, v ...interface{}) 31 Warnf(format string, v ...interface{}) 32 Errorf(format string, v ...interface{}) 33 Fatalf(format string, v ...interface{}) 34 } 35 36 // Logger is a logger interface that provides logging function with levels. 37 type Logger interface { 38 Trace(v ...interface{}) 39 Debug(v ...interface{}) 40 Info(v ...interface{}) 41 Notice(v ...interface{}) 42 Warn(v ...interface{}) 43 Error(v ...interface{}) 44 Fatal(v ...interface{}) 45 } 46 47 // CtxLogger is a logger interface that accepts a context argument and output 48 // logs with a format. 49 type CtxLogger interface { 50 CtxTracef(ctx context.Context, format string, v ...interface{}) 51 CtxDebugf(ctx context.Context, format string, v ...interface{}) 52 CtxInfof(ctx context.Context, format string, v ...interface{}) 53 CtxNoticef(ctx context.Context, format string, v ...interface{}) 54 CtxWarnf(ctx context.Context, format string, v ...interface{}) 55 CtxErrorf(ctx context.Context, format string, v ...interface{}) 56 CtxFatalf(ctx context.Context, format string, v ...interface{}) 57 } 58 59 // Control provides methods to config a logger. 60 type Control interface { 61 SetLevel(Level) 62 SetOutput(io.Writer) 63 } 64 65 // FullLogger is the combination of Logger, FormatLogger, CtxLogger and Control. 66 type FullLogger interface { 67 Logger 68 FormatLogger 69 CtxLogger 70 Control 71 } 72 73 // Level defines the priority of a log message. 74 // When a logger is configured with a level, any log message with a lower 75 // log level (smaller by integer comparison) will not be output. 76 type Level int 77 78 // The levels of logs. 79 const ( 80 LevelTrace Level = iota 81 LevelDebug 82 LevelInfo 83 LevelNotice 84 LevelWarn 85 LevelError 86 LevelFatal 87 ) 88 89 var strs = []string{ 90 "[Trace] ", 91 "[Debug] ", 92 "[Info] ", 93 "[Notice] ", 94 "[Warn] ", 95 "[Error] ", 96 "[Fatal] ", 97 } 98 99 func (lv Level) toString() string { 100 if lv >= LevelTrace && lv <= LevelFatal { 101 return strs[lv] 102 } 103 return fmt.Sprintf("[?%d] ", lv) 104 }