zlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

zlog

zlog is a logging package for Go built on top of zap, designed for simple and structured logging. It provides multiple logging levels like Debug, Info, Warn, Error, and Fatal, with flexibility in configuring console output and log file directory.

Installation

To use zlog, add it to your Go project by running:

go get github.com/thisismz/zlog@latest

Configuration

You can configure zlog to control console output and specify a directory for log files:

// Disable console output
log.LogInConsole = false

// Set directory path for log files
log.Director = "./log"

Usage

Each logging function provides an easy way to log messages with various severity levels.

Log Levels
  • Debug: Logs debug information

    log.Debug("Debug message", zap.String("key", "value"))
    
  • Info: Logs informational messages

    log.Info("Info message", zap.String("key", "value"))
    
  • Warn: Logs warnings

    log.Warn("Warning message", zap.String("key", "value"))
    
  • Error: Logs errors

    log.Error("Error occurred", err)
    
  • Fatal: Logs fatal errors and terminates the application

    log.Fatal("Fatal error", zap.String("key", "value"))
    

Each function takes a message string and optional fields (key-value pairs) for structured logging.

Example

package main

import (
    "log"
    "go.uber.org/zap"
)

func main() {
    // Configure logging
    log.LogInConsole = false
    log.Director = "./log"

    // Log messages
    log.Debug("Starting the application", zap.String("status", "debugging"))
    log.Info("Application running", zap.String("status", "info"))
    log.Warn("Potential issue detected", zap.String("status", "warning"))
    log.Error("An error occurred", err)
    log.Fatal("Critical error, shutting down", zap.String("status", "fatal"))
}

Benchmarking

The package includes a benchmark test (log_benchmark_test.go) to evaluate logging performance. Run it with:

go test -bench .

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Level         = "debug"
	Prefix        = "mobinio"
	Format        = "console"
	Director      = ""
	EncodeLevel   = "uppercase"
	StacktraceKey = "trace"
	ShowLine      = false
	LogInConsole  = true
	RetentionDay  = 30
	SentryDns     = ""
)

Functions

func Debug

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

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func Error

func Error(msg string, err error)

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func Fatal

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

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

func Info

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

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func LevelEncoder

func LevelEncoder() zapcore.LevelEncoder

func Log

func Log() *zap.Logger

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

func Panic

func Panic(msg string, fields ...zapcore.Field)

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func Warn

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

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Types

This section is empty.

Jump to

Keyboard shortcuts

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