logger

package
v0.0.34 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 6 Imported by: 2

README

Logger Module

A logging module built on Zap, integrated with Uber Fx for dependency injection.

Features

  • Uber Fx dependency injection integration
  • Zap high-performance logging
  • Debug mode with configurable log levels
  • Colorful console output
  • Custom timestamp format

Installation

go get github.com/weedbox/common-modules/logger

Quick Start

Basic Usage
package main

import (
    "github.com/weedbox/common-modules/logger"
    "go.uber.org/fx"
)

func main() {
    fx.New(
        logger.Module(),
        // ... other modules
    ).Run()
}
Using Logger in Your Module
package myservice

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

type Params struct {
    fx.In

    Logger *zap.Logger
}

type Service struct {
    logger *zap.Logger
}

func NewService(p Params) *Service {
    return &Service{
        logger: p.Logger.Named("myservice"),
    }
}

func (s *Service) DoSomething() {
    s.logger.Info("Doing something",
        zap.String("key", "value"),
        zap.Int("count", 42),
    )
}

Configuration

Configuration is done via environment variables:

Environment Variable Values Default Description
DEBUG_MODE debug, true - Enable debug mode
DEBUG_LEVEL debug, info, warn, error, dpanic, panic, fatal debug Log level when debug mode is enabled
Enable Debug Mode
export DEBUG_MODE=true
export DEBUG_LEVEL=debug

Log Output Format

The logger outputs in console format with colorful level encoding:

2024-01-15 10:30:45  INFO    myservice   Doing something    {"key": "value", "count": 42}

API Reference

Module
Module() fx.Option

Creates a Logger module and returns an Fx Option. This module provides a *zap.Logger instance.

SetupLogger() *zap.Logger

Manually setup and return a logger instance. Called automatically by the module.

GetLogger() *zap.Logger

Returns the global logger instance after initialization.

Debug Mode Behavior

Mode Caller Info Log Level
Normal No Info
Debug Yes Configurable via DEBUG_LEVEL

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLogger

func GetLogger() *zap.Logger

func Module

func Module() fx.Option

func NewCustomEncoderConfig

func NewCustomEncoderConfig() zapcore.EncoderConfig

func SetupLogger

func SetupLogger() *zap.Logger

Types

type Params

type Params struct {
	fx.In
}

Jump to

Keyboard shortcuts

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