logfile

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 13 Imported by: 0

README

logfile

Travis CI Software License GoDoc Coverage Status goreport

write log from other systems to different files

  1. 当前写入:日志(包含时间)=> 应用名/日期(YYYYMMDD)/应用名_YYYYMMDD_源IP_分区ID.log
  2. 超期归档:归档n(如7天)之前的日志:应用名/日期(YYYYMMDD)/应用名_YYYYMMDD_源IP_分区ID.tar.gz (保留.tar.gz,在不限制大小时只包含1一个文件,为后续可能的日志文件最大大小做预留)
  3. 过期删除:删除n(如90天)之前的归档日志

usage:

package main

import (
	"github.com/bingoohuang/logfile"
	"time"
)

func main() {
	l := logfile.File{
		Pattern:     "{APP}/YYYYMMDD/{APP}_YYYYMMDD_{IP}_{ZONE}.log",
		ArchiveDays: 1, // 归档1天前的日志
		DeleteDays:  2, // 删除2天之前的日志(包括归档日志)
	}
    // 开始
    l.Start()

	day1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-21 18:00:54")

	l.Write(map[string]string{
		"APP":  "ids",
		"IP":   "192.168.0.1",
		"ZONE": "zone01",
	}, day1, "我是第1天的一行日志,啦啦啦啦啦")

	day2 := day1.Add(24 * time.Hour)

	l.Write(map[string]string{
		"APP":  "ids",
		"IP":   "192.168.0.1",
		"ZONE": "zone01",
	}, day2, "我是第2天的一行日志,啦啦啦啦啦")
}

Documentation

Index

Constants

View Source
const Day = time.Hour * 24

Day means 24 hours.

Variables

View Source
var ErrNotStarted = errors.New("not started")

ErrNotStarted 表示没有调用Start方法开始,或者在Close之后继续Write.

View Source
var ErrOverArchiveDays = errors.New("over max archive days")

ErrOverArchiveDays 定义了写入日志的时间超过了ArchiveDays的错误.

Functions

func CreateTarGz

func CreateTarGz(archiveName string, files []string) error

CreateTarGz creates an archive file with archiveName like a/b/c.tar.gz.

func ExtractTarGz

func ExtractTarGz(gzipStream io.Reader) error

ExtractTarGz extracts files from a tar archive.

func ReplaceAll

func ReplaceAll(subject string, search string, replace string) string

ReplaceAll replaces all the search string to replace in subject with case-insensitive.

func ReplaceIgnoreCase

func ReplaceIgnoreCase(subject string, search string, replace string) string

ReplaceIgnoreCase replaces all the search string to replace in subject with case-insensitive.

Types

type File

type File struct {
	// Pattern 定义了日志文件的布局,例如:/var/logs/{APP}/YYYYMMDD/{APP}_YYYYMMDD_{IP}_ZONE.log.
	Pattern string
	// ArchiveDays 定义了多少天之前的日志文件,进行归档。0时不归档.
	ArchiveDays int
	// DeleteDays 定义了多少天之前的日志删除(包括归档日志)。0时不删除.
	DeleteDays int
	// 是否写入后刷盘.
	Flush bool

	Clock clock.Clock
	// contains filtered or unexported fields
}

File 定义了写入日志的结构.

func (*File) Close

func (f *File) Close() error

Close shutdowns the scheduler.

func (*File) Start

func (f *File) Start() error

Start starts the logfile archiving and deleting works after necessary initialization. .

func (*File) Write

func (f *File) Write(properties map[string]string, logTime time.Time, s string) error

Write 写入一条日志.

Jump to

Keyboard shortcuts

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