log

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 5 Imported by: 2

README

modlib/log

A fast, leveled, configurable logging (based on zap) in Go.

Usage

package main

import (
	"fmt"
	"io/ioutil"
	"path/filepath"

	"github.com/chinmobi/modlib/log"
)

func setUp() {
	config := log.DefaultConfig()

	tempDir, err := ioutil.TempDir("", "*-logs")
	if err == nil {
		config.File.Enabled = true
		config.File.Filename = filepath.Join(tempDir, "info.log")
		config.File.Level = "<WARN"

		warnFilename := filepath.Join(tempDir, "warn.log")
		warnFileCfg := log.NewFileConfig("=WARN", warnFilename)
		config.AppendFileConfig(warnFileCfg)

		errFilename := filepath.Join(tempDir, "error.log")
		errFileCfg := log.NewFileConfig("ERROR", errFilename)
		config.AppendFileConfig(errFileCfg)
	} else {
		fmt.Errorf("%+v", err)
	}

	log.SetUpLogger(config)
}

func main() {
	defer func() {
		if p := recover(); p != nil {
			// discard
		}
	}()

	setUp()

	log.Debug("A debug msg")
	log.Debugf("A %s msg", "debug")

	log.Info("A info msg")
	log.Infof("A %s msg", "info")

	log.Warn("A warn msg")
	log.Warnf("A %s msg", "warn")

	log.Error("A error msg")
	log.Errorf("A %s msg", "error")

	log.Panic("A panic msg")
}

Documentation

Index

Constants

View Source
const (
	DEFAULT_LEVEL = "INFO"
	DEBUG_LEVEL   = "DEBUG"
	INFO_LEVEL    = "INFO"
	WARN_LEVEL    = "WARN"
	ERROR_LEVEL   = "ERROR"
	PANIC_LEVEL   = "PANIC"
	FATAL_LEVEL   = "FATAL"
)

Variables

View Source
var (
	Logger  *zap.Logger        = zap.L()
	SLogger *zap.SugaredLogger = zap.S()
)

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(template string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(template string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(template string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(template string, args ...interface{})

func L

func L() *zap.Logger

func Panic

func Panic(args ...interface{})

func Panicf

func Panicf(template string, args ...interface{})

func ParseLevels

func ParseLevels(levels string) zapcore.LevelEnabler

func S

func S() *zap.SugaredLogger

func SetUpLogger

func SetUpLogger(config *LoggerConfig)

Set up logger with config

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(template string, args ...interface{})

Types

type ConsoleConfig

type ConsoleConfig struct {
	Enabled bool   `json:"enabled"`
	Level   string `json:"level"`
}

Config for console logger

type FileConfig

type FileConfig struct {
	Enabled    bool   `json:"enabled"`
	Level      string `json:"level"`
	Filename   string `json:"filename"`
	MaxSize    int    `json:"max_size"`
	MaxBackups int    `json:"max_backups"`
	MaxAge     int    `json:"max_age"`
	Compress   bool   `json:"compress"`
}

Config for file logger

func NewFileConfig

func NewFileConfig(level, filename string) *FileConfig

New a FileConfig

type LoggerConfig

type LoggerConfig struct {
	File    FileConfig    `json:"file"`
	Console ConsoleConfig `json:"console"`
	Level   string        `json:"level"`
	// contains filtered or unexported fields
}

Logger configuration

func DefaultConfig

func DefaultConfig() *LoggerConfig

Create a LoggerConfig with default value.

func (*LoggerConfig) AppendFileConfig

func (config *LoggerConfig) AppendFileConfig(cfg ...*FileConfig)

Append extra FileConfig(s)

func (*LoggerConfig) Init

func (config *LoggerConfig) Init()

Initial the LoggerConfig with default value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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