logging

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

logging

Logging package based on zap and lumberjack

Quick Start

  • Create file ./etc/conf/logging.json and config it.
{
    "default": {
        "filename": "root.log",
        "maxsize": 20,
        "maxage": 7,
        "maxbackups": 50,
        "localtime": true,
        "compress": true,
        "level": "warn",
        "env": "prod"
    },
    "root": {
        "filename": "root.log",
        "maxsize": 20,
        "maxage": 7,
        "maxbackups": 50,
        "localtime": true,
        "compress": true,
        "level": "warn",
        "env": "prod"
    },
    "proxy": {
        "filename": "proxy.log",
        "maxsize": 20,
        "maxage": 7,
        "maxbackups": 50,
        "localtime": true,
        "compress": true,
        "level": "warn"
    }
}

The root and proxy are the names of the loggers from which you can get the logger instance.

The range of level is debug, info, warn, error, dpanic, panic, fatal, and the value is from low to high.

  • Usage
package main

import (
    "github.com/mallbook/logging"
    "go.uber.org/zap"
)

func main() {
    logger := logging.Logger("mylog") // or logger := logging.Logger("root")

    for i := 0; i < 100; i++ {
        logger.Info("hello world", zap.String("key", "value"), zap.Int("age", 20))
        logger.Debug("hello china", zap.String("key", "value"), zap.Int("age", 20))
        logger.Error("hello error", zap.String("key", "value"), zap.Int("age", 30))
    }

    proxyLogger := logging.Logger("proxy") 
    proxyLogger.Info("hello world", zap.String("key", "value"), zap.Int("age", 20))
}

Logo Level

// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1

// InfoLevel is the default logging priority.
InfoLevel

// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel

// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel

// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel

// PanicLevel logs a message, then panics.
PanicLevel

// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger(names ...string) *zap.Logger

Logger return the named zap.Logger instance, if no names return defaultLogger

Types

type Env

type Env int8
const (
	UnknowEnv Env = iota - 1
	ProdEnv
	DevEnv
)

func (*Env) Get

func (e *Env) Get() interface{}

Get gets the level for the flag.Getter interface.

func (*Env) Set

func (e *Env) Set(s string) error

Set sets the level for the flag.Value interface.

func (Env) String

func (e Env) String() string

String returns a lower-case ASCII representation of the env.

func (*Env) UnmarshalText

func (e *Env) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

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