Documentation ¶
Overview ¶
Package logger provides a way to create global logger. We only need to create it once and use it anywhere in the project.
Example Usage ¶
The following is a complete example using logger package
import ( "errors" "fmt" "github.com/phamtai97/go-utils/utils/logger" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type user struct { Username string Age int } func (u *user) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("name", u.Username) enc.AddInt("age", u.Age) return nil } func main() { // Can customize the logger config // cfg := logger.Config{ // Level: logger.INFO, // FileLogConfig: logger.FileLogConfig{ // IsUseFile: true, // FilePath: "./logs.log", // }, // } // New default config // cfg := logger.NewDefaultConfig() // New production config, write logs to file // cfg := logger.NewProductionConfig(true, "./logs.log") // New production config, write logs to console // cfg := logger.NewProductionConfig("") // Can init logger with simple line // logger.InitProduction("./logs.log") cfg := logger.Config{ Level: logger.DEBUG, } if err := logger.Init(cfg); err != nil { fmt.Printf("Failed to init logger: %v\n", err) } defer logger.Sync() logger.Debug("Test debug logger", zap.String("Hey, ", "I am a software engineer"), zap.Object("My information: ", &user{ Username: "AJPham", Age: 1997, })) logger.Info("Test info logger", zap.String("Hey, ", "I am a software engineer"), zap.Object("My information: ", &user{ Username: "AJPham", Age: 1997, })) logger.Error("Test error logger", zap.String("Hey, ", "I am a software engineer"), zap.Object("My information: ", &user{ Username: "AJPham", Age: 1997, }), zap.Error(errors.New("Failed to write log"))) logger.Warn("Test warn logger", zap.String("Hey, ", "I am a software engineer"), zap.Object("My information: ", &user{ Username: "AJPham", Age: 1997, })) logger.Fatal("Test fatal logger", zap.String("Hey, ", "I am a software engineer"), zap.Object("My information: ", &user{ Username: "AJPham", Age: 1997, })) }
Index ¶
- Constants
- func Debug(msg string, fields ...zap.Field)
- func Error(msg string, fields ...zap.Field)
- func Fatal(msg string, fields ...zap.Field)
- func Info(msg string, fields ...zap.Field)
- func Init(cfg Config) error
- func InitProduction(filePath string) error
- func Sync() error
- func Warn(msg string, fields ...zap.Field)
- type Config
- type FileLogConfig
- type Level
Constants ¶
View Source
const (
// DefaultLogFileSizeInMB Default log file size with Megabyte unit.
DefaultLogFileSizeInMB = 512
)
Variables ¶
This section is empty.
Functions ¶
func InitProduction ¶
InitProduction creates the global logger that is used everywhere in project with production config logger.
Types ¶
type Config ¶
type Config struct { Level Level FileLogConfig FileLogConfig }
Config allows users to configure log level and log file.
func NewDefaultConfig ¶
func NewDefaultConfig() Config
NewDefaultConfig returns the default config with INFO level and log to console.
func NewProductionConfig ¶
NewProductionConfig returns the production config with INFO level.
type FileLogConfig ¶
type FileLogConfig struct { IsUseFile bool FilePath string MaxSize int MaxBackups int MaxAge int Compress bool }
FileLogConfig allows users to configure detail log file such as file path, max size of file, max file to backup,....
Click to show internal directories.
Click to hide internal directories.