logger

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 7 Imported by: 0

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

View Source
const (
	// DefaultLogFileSizeInMB Default log file size with Megabyte unit.
	DefaultLogFileSizeInMB = 512
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...zap.Field)

Debug logs a message at Debug level.

func Error

func Error(msg string, fields ...zap.Field)

Error logs a message at Error level.

func Fatal

func Fatal(msg string, fields ...zap.Field)

Fatal logs a message at Fatal level.

func Info

func Info(msg string, fields ...zap.Field)

Info logs a message at Info level.

func Init

func Init(cfg Config) error

Init creates the global logger that is used everywhere in project with your config.

func InitProduction

func InitProduction(filePath string) error

InitProduction creates the global logger that is used everywhere in project with production config logger.

func Sync

func Sync() error

Sync flushs any buffered log entries. It should be call before program exit.

func Warn

func Warn(msg string, fields ...zap.Field)

Warn logs a message at Warn level.

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

func NewProductionConfig(isUseFile bool, filePath string) Config

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,....

type Level

type Level string

Level map between string level with zapcore level.

const (
	// DEBUG logs.
	DEBUG Level = "DEBUG"
	// INFO level is the default logging.
	INFO Level = "INFO"
	// WARN level logs are more important than Info.
	WARN Level = "WARN"
	// ERROR logs are high-priority.
	ERROR Level = "ERROR"
	// FATAL log message and then calls os.Exit(1).
	FATAL Level = "FATAL"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL