Skip to content
/ logman Public
generated from moul/golang-repo-template

✍️ log files management library optimized for "Share with Developers"

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

moul/logman

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

logman

😄 golang library to organize log files (create by date, GC, etc)

go.dev reference License GitHub release Docker Metrics Made by Manfred Touron

Go Release PR GolangCI codecov Go Report Card CodeFactor

Gitpod ready-to-code

Example

import (
	"fmt"

	"moul.io/logman"
)

func Example() {
	// new log manager
	manager := logman.Manager{
		Path:     "./path/to/dir",
		MaxFiles: 10,
	}

	// cleanup old log files for a specific app name
	err := manager.Flush("my-app")
	checkErr(err)

	// cleanup old log files for any app sharing this log directory
	err = manager.FlushAll()
	checkErr(err)

	// list existing log files
	files, err := manager.Files()
	checkErr(err)
	fmt.Println(files)

	// - create an WriteCloser
	// - automatically delete old log files if it hits a limit
	writer, err := manager.New("my-app")
	checkErr(err)
	defer writer.Close()
	writer.Write([]byte("hello world!\n"))
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

Usage

TYPES

type File struct {
	// Full path.
	Path string

	// Size in bytes.
	Size int64

	// Provided name when creating the file.
	Name string

	// Creation date of the file.
	Time time.Time

	// Whether it is the most recent log file for the provided app name or not.
	Latest bool

	// If there were errors when trying to get info about this file.
	Errs error `json:"Errs,omitempty"`
}
    File defines a log file with metadata.

func (f File) String() string
    String implements Stringer.

type Manager struct {
	// Path is the target directory containing the log files.
	// Default is '.'.
	Path string

	// MaxFiles is the maximum number of log files in the directory.
	// If 0, won't automatically GC based on this criteria.
	MaxFiles int
}
    Manager is a configuration object used to create log files with automatic GC
    rules.

func (m Manager) Files() ([]File, error)
    Files returns a list of existing log files.

func (m Manager) Flush(name string) error
    Flush deletes old log files for the specified app name.

func (m Manager) FlushAll() error
    FlushAll deletes old log files for any app name.

func (m Manager) New(name string) (io.WriteCloser, error)
    Create a new log file and perform automatic GC of the old log files if
    needed.

    The created log file will looks like:

        <path/to/log/dir>/<name>-<time>.log

    Depending on the provided configuration of Manager, an automatic GC will be
    run automatically.

Install

Using go

go get moul.io/logman

Releases

See https://github.com/moul/logman/releases

Contribute

Contribute <3

I really welcome contributions. Your input is the most precious material. I'm well aware of that and I thank you in advance. Everyone is encouraged to look at what they can do on their own scale; no effort is too small.

Everything on contribution is sum up here: CONTRIBUTING.md

Dev helpers

Pre-commit script for install: https://pre-commit.com

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Manfred Touron

🚧 📖 ⚠️ 💻

moul-bot

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Stargazers over time

Stargazers over time

License

© 2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)