Giter Club home page Giter Club logo

log-manager's Introduction

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.

log-manager's People

Contributors

colecrouter avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.