log

package module
v0.0.0-...-7e68a9f Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

README

简介

该日志包参考 paas-lager,做了一些便捷性上的改动,功能完全一样,只不过该日志包更便捷些。

Go开发中常用的log包有:

  • log
  • glog
  • logrus
  • ...

logglog比较简单,无法满足生产级的程序开发。logrus功能很强大,但缺少rotate功能,需要自己通过外部的程序来rotate日志文件。该日志包总结了企业开发中常用的需求,将这些功能整合在一个日志包中。经过测试该日志包性能完全可以满足企业级的生产需求。

使用方法

在使用log包前,需要先初始化log包,初始化函数有:InitWithConfig(), InitWithFile()。一个简单的example:

package main

import (
	"fmt"

	"gitee.com/gaozhenhai/log"
	"gitee.com/gaozhenhai/log/lager"
)

func main() {
	log.InitWithFile("log.yaml")

	for i := 0; i < 1; i++ {
		log.Infof("Hi %s, system is starting up ...", "paas-bot")
		log.Info("check-info", lager.Data{
			"info": "something",
		})

		log.Debug("check-info", lager.Data{
			"info": "something",
		})

		log.Warn("failed-to-do-somthing", lager.Data{
			"info": "something",
		})

		err := fmt.Errorf("This is an error")
		log.Error("failed-to-do-somthing", err)

		log.Info("shutting-down")
	}
}

log.yaml文件为:

writers: file,stdout
logger_level: DEBUG
logger_file: logs/log.log
log_format_text: false
rollingPolicy: size # size, daily
log_rotate_date: 1
log_rotate_size: 1
log_backup_count: 7

日志参数

  • writers: 输出位置,有2个可选项:file,stdout。选择file会将日志记录到logger_file指定的日志文件中,选择stdout会将日志输出到标准输出,当然也可以两者同时选择
  • logger_level: 日志级别,DEBUG, INFO, WARN, ERROR, FATAL
  • logger_file: 日志文件
  • log_format_text: 日志的输出格式,json或者plaintext,true会输出成json格式,false会输出成非json格式
  • rollingPolicy: rotate依据,可选的有:daily, size。如果选daily则根据天进行转存,如果是size则根据大小进行转存
  • log_rotate_date: rotate转存时间,配合rollingPolicy: daily使用
  • log_rotate_size: rotate转存大小,配合rollingPolicy: size使用
  • log_backup_count:当日志文件达到转存标准时,log系统会将该日志文件进行压缩备份,这里指定了备份文件的最大个数。

Documentation

Overview

Package lager is the package for lager

Index

Constants

View Source
const (
	RollingPolicySize = "size"
	LogRotateDate     = 1
	LogRotateSize     = 10
	LogBackupCount    = 7
)

constant values for logrotate parameters

View Source
const (
	//DEBUG is a constant of string type
	DEBUG = "DEBUG"
	INFO  = "INFO"
	WARN  = "WARN"
	ERROR = "ERROR"
	FATAL = "FATAL"
)

Variables

View Source
var Logger lager.Logger

Logger is the global variable for the object of lager.Logger

View Source
var Writers = make(map[string]io.Writer)

Writers is a map

Functions

func CopyFile

func CopyFile(srcFile, destFile string) error

CopyFile copy file

func Debug

func Debug(action string, data ...lager.Data)

func Debugf

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

func Error

func Error(action string, err error, data ...lager.Data)

func Errorf

func Errorf(err error, format string, args ...interface{})

func EscapPath

func EscapPath(msg string) string

EscapPath escape path

func Fatal

func Fatal(action string, err error, data ...lager.Data)

func Fatalf

func Fatalf(err error, format string, args ...interface{})

func FilterFileList

func FilterFileList(path, pat string) ([]string, error)

FilterFileList function for filter file list path : where the file will be filtered pat : regexp pattern to filter the matched file

func Info

func Info(action string, data ...lager.Data)

func Infof

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

func InitWithConfig

func InitWithConfig(passLagerDef *PassLagerCfg)

func InitWithFile

func InitWithFile(lagerFile string)

readPassLagerConfigFile is unmarshal the paas lager configuration file(lager.yaml)

func Initialize

func Initialize(writers, loggerLevel, loggerFile, rollingPolicy string, logFormatText bool,
	LogRotateDate, LogRotateSize, LogBackupCount int)

Initialize Build constructs a *Lager.Logger with the configured parameters.

func LagerInit

func LagerInit(c Config)

Init is a function which initializes all config struct variables

func LogRotate

func LogRotate(path string, MaxFileSize int, MaxBackupCount int)

LogRotate function for log rotate path: where log files need rollover MaxFileSize: MaxSize of a file before rotate. By M Bytes. MaxBackupCount: Max counts to keep of a log's backup files.

func NewLogger

func NewLogger(component string) lager.Logger

NewLogger is a function

func NewLoggerExt

func NewLoggerExt(component string, appGUID string) lager.Logger

NewLoggerExt is a function which is used to write new logs

func RegisterWriter

func RegisterWriter(name string, writer io.Writer)

RegisterWriter is used to register a io writer

func Warn

func Warn(action string, data ...lager.Data)

func Warnf

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

Types

type Config

type Config struct {
	LoggerLevel    string
	LoggerFile     string
	Writers        []string
	EnableRsyslog  bool
	RsyslogNetwork string
	RsyslogAddr    string

	LogFormatText bool
}

Config is a struct which stores details for maintaining logs

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig is a function which retuns config object with default configuration

type Lager

type Lager struct {
	Writers        string `yaml:"writers"`
	LoggerLevel    string `yaml:"logger_level"`
	LoggerFile     string `yaml:"logger_file"`
	LogFormatText  bool   `yaml:"log_format_text"`
	RollingPolicy  string `yaml:"rollingPolicy"`
	LogRotateDate  int    `yaml:"log_rotate_date"`
	LogRotateSize  int    `yaml:"log_rotate_size"`
	LogBackupCount int    `yaml:"log_backup_count"`
}

Lager struct for logger parameters

type PassLagerCfg

type PassLagerCfg struct {
	Writers        string `yaml:"writers"`
	LoggerLevel    string `yaml:"logger_level"`
	LoggerFile     string `yaml:"logger_file"`
	LogFormatText  bool   `yaml:"log_format_text"`
	RollingPolicy  string `yaml:"rollingPolicy"`
	LogRotateDate  int    `yaml:"log_rotate_date"`
	LogRotateSize  int    `yaml:"log_rotate_size"`
	LogBackupCount int    `yaml:"log_backup_count"`
}

PassLagerCfg is the struct for lager information(passlager.yaml)

var PassLagerDefinition *PassLagerCfg = DefaultLagerDefinition()

PassLagerDefinition is having the information about loging

func DefaultLagerDefinition

func DefaultLagerDefinition() *PassLagerCfg

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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