log

package module
v1.0.0 Latest Latest
Warning

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

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

README

Introduction

The log package refers to lexkong/log, and some convenience changes The function is exactly the same, because the original author is no longer maintained and does not support go mod

The log packages commonly used when develop by Go:

  • log
  • glog
  • logrus

log and glog are relatively simple and cannot meet production-level program development. logrus is powerful, but not support rotate. You need to rotate the log file by an external program yourself. This log package summarizes the requirements commonly used in enterprise development, and integrates these functions in a log package. After testing, the performance of the log package can fully meet the needs of enterprise-level production.

Instructions

Before using the log package, you need to initialize the log package. The initialization functions are:InitWithConfig(), InitWithFile()

A simple example:

package main

import (
	"fmt"

	"github.com/sinlovgo/log"
	"github.com/sinlovgo/log/lager"
)

func main() {
	_ := log.InitWithFile("log.yaml", "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 file content:

log:
  writers: file,stdout
  logger_level: DEBUG
  logger_file: logs/log.log # if not set use FRAME_HOME env to replace or use default log/frame.log
  log_format_text: false
  rollingPolicy: size # size, daily
  log_rotate_date: 1
  log_rotate_size: 1
  log_backup_count: 7

Log parameters

  • writers: file,stdout。file will let logger_file to file,stdout will show at std, most of time use bose
  • logger_level: log level: DEBUG, INFO, WARN, ERROR, FATAL
  • logger_file: log file setting
  • log_format_text: format true will format json, false will show abs
  • rollingPolicy: rotate policy, can choose as: daily, size. daily store as daily,size will save as max
  • log_rotate_date: rotate date, coordinate rollingPolicy: daily
  • log_rotate_size: rotate size,coordinate rollingPolicy: size
  • log_backup_count: backup max count, log system will compress the log file when log reaches rotate set, this set is max file count

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 Init

func Init() error

func InitWithConfig

func InitWithConfig(passLagerDef *PassLagerCfg) error

func InitWithFile

func InitWithFile(lagerFile, fileType string) error

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