logmanager

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

README

Log-Manager

go get github.com/Mexican-Man/log-manager

There are lots of good log rotators out there, but I couldn't find one that gave me the granularity/number of features that I needed. So, I've compiled features from other log rotating packages into this one. If you think it's missing a feature, please open an issue or PR.

How to Use

import (
    lm "github.com/Mexican-Man/log-manager"
)

manager := lm.NewLogManager(lm.LogManagerOptions{
    Dir:              "/path/to/logs",
    RotationInterval: time.Hour * 24,
})

log.SetOutput(manager)

Options

  • *Dir — Directory to store logs in
  • *RotationInterval — How often to rotate logs (0 disables it)
  • FilenameFormat — Template string using text/template (more info below)
  • MaxFileSize — How large a file can get before its rotated (0 for no limit)
  • GZIP — GZIP old logs
  • LatestDotLog — Keeps a symlink called latest that points to the latest log

More Details

Filenameformat

Here's the templated struct format:

type LogTemplate struct {
	Time      time.Time
	Iteration uint
}

When rotating, Interation will increase if another log with the same name already exists. If increasing the iteration does not solve the issue, it will throw an error, and continue writing to the old log.

Here's the default, if not defined in LogManagerOptions{}:

{{ .Time.Format "2006-01-02" }}_{{ .Iteration }}.log

This will print create logs like this:

  • 2022-05-17_0.log
  • 2022-05-17_1.log
  • 2022-05-18_0.log

Here's another example:

{{ .Time.Format "2006-01-02" }}{{ if .Iteration }}_{{ .Iteration }}{{ end }}.log

This will print create logs like this:

  • 2022-05-17.log
  • 2022-05-17_1.log
  • 2022-05-18.log

Note that the date format is the Go's standard date formatting.

Scheduled Rotation

You can set RotationInterval to indicate when your logs should rotate. For example, a RotationInterval of

time.Hour * 24

would ensure that logs are rotated everyday, at midnight. A RotationInterval of

time.Hour * 12

would ensure that logs are rotated everyday, at midnight and noon.

Documentation

Overview

LogManager implements io.Writer from os, and is meant to be used directly with the log package. Use NewLogManager() to create a new LogManager with your desired settings. Upon Write(), it will manage rotation, compression, etc. rather than scheduling rotation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogManager

type LogManager struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LogManager is the main struct of the package. It implements io.Writer, and is safe for concurrent use.

func NewLogManager

func NewLogManager(options LogManagerOptions) *LogManager

Create a new LogManager. `timeFormat` is the format used in `filenameFormat`. `filenameFormat` is a template string for type LogNameTemplate.

func (*LogManager) Rotate

func (lm *LogManager) Rotate() (err error)

Rotate manually triggers a log rotation

func (*LogManager) Write

func (lm *LogManager) Write(p []byte) (n int, err error)

Write checks all of the log manager's conditions, potentially triggers a rotation, then writes to a corresponding log file

type LogManagerOptions

type LogManagerOptions struct {
	Dir              string
	FilenameFormat   string
	RotationInterval time.Duration
	MaxFileSize      int64
	GZIP             bool
	LatestDotLog     bool
}

type LogTemplate

type LogTemplate struct {
	Time      time.Time
	Iteration uint
}

Jump to

Keyboard shortcuts

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