logman

package module
Version: v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: Apache-2.0, MIT Imports: 10 Imported by: 0

README

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

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)

Documentation

Overview

message from the author:

+--------------------------------------------------------------+
| * * * ░░░░░░░░░░░░░░░░░░░░  Hello  ░░░░░░░░░░░░░░░░░░░░░░░░░░|
+--------------------------------------------------------------+
|                                                              |
|     ++              ______________________________________   |
|     ++++           /                                      \  |
|      ++++          |                                      |  |
|    ++++++++++      |   Feel free to contribute to this    |  |
|   +++       |      |       project or contact me on       |  |
|   ++         |     |    manfred.life if you like this     |  |
|   +  -==   ==|     |               project!               |  |
|  (   <*>   <*>     |                                      |  |
|   |          |    /|                  :)                  |  |
|   |         _)   / |                                      |  |
|   |      +++    /  \______________________________________/  |
|    \      =+   /                                             |
|     \      +                                                 |
|     |\++++++                                                 |
|     |  ++++      ||//                                        |
|  ___|   |___    _||/__                                     __|
| /    ---    \   \|  |||                   __ _  ___  __ __/ /|
|/  |       |  \    \ /                    /  ' \/ _ \/ // / / |
||  |       |  |    | |                   /_/_/_/\___/\_,_/_/  |
+--------------------------------------------------------------+
Example
package main

import (
	"fmt"

	"moul.io/logman"
)

func main() {
	// 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)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File added in v1.1.0

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 (File) String added in v1.1.0

func (f File) String() string

String implements Stringer.

type Manager added in v1.1.0

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 (Manager) Files added in v1.1.0

func (m Manager) Files() ([]File, error)

Files returns a list of existing log files.

func (Manager) Flush added in v1.2.0

func (m Manager) Flush(name string) error

Flush deletes old log files for the specified app name.

func (Manager) FlushAll added in v1.2.0

func (m Manager) FlushAll() error

FlushAll deletes old log files for any app name.

func (Manager) New added in v1.1.0

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.

Source Files

Jump to

Keyboard shortcuts

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